Info
This question is closed. Reopen it to edit or answer.
I still have problems after this code , Not enough input arguments.Error in switch flag. Anyone please could check the Kalman Filter code and check why it is not working.
1 view (last 30 days)
Show older comments
function [sys,x0,str,ts] = SimuKalmanFilter(t,x,u,flag)
switch flag
case 0
[sys, x0, str, ts]=mdlInitializeSizes;
case 1
sys=mdlDerivatives(t, x, u);
case 2
sys=mdlUpdate(t, x, u);
case 3
sys=mdlOutputs(t, x, u);
case 4
sys=mdlGetTimeOfNextVarHit(t, x, u);
case 9
sys=mdlTerminate(t, x, u);
otherwise
error(['Unhandled flag=',num2str(flag)]);
end
function [sys, x0, str, ts]=mdlInitializeSizes
sizes=simsizes;
sizes.NumContStates=0;
sizes.NumDiscStates=1;
sizes.NumOutputs=1;
sizes.NumInputs=1;
sizes.DirFeedthrough=1;
sizes.NumSampleTimes=1;
sys=simsizes(sizes);
x0=5000+sqrt(5)*randn;
str=[];
ts=[-1 0];
global P;
P=5;
function sys=mdlDerivatives(t, x, u)
sys=[];
function sys=mdlUpdate(t, x, u)
global P;
F=1;
B=0;
H=1;
Q=0;
R=5;
xpre=F*x+B*u;
Ppre=F*P*F'+Q;
K=Ppre*H'*inv(H*Ppre*H'+R);
e=u-H*xpre;
xnew=xpre+K*e;
P=(eye(1)-K*H)*Ppre;
sys=xnew;
function sys=mdlOutputs(t, x, u)
sys=x;
function sys=mdlGetTimeOfNextVarHit(t, x, u)
sampleTime=1;
sys=t+sampleTime
function sys=mdlTerminate(t, x, u)
sys=[];
if true
% code
end
2 Comments
Nicolas Schmit
on 18 Oct 2017
Post the code calling SimuKalmanFilter(t,x,u,flag) and the complete error message.
Answers (1)
Robert Henson
on 18 Oct 2017
Are you running the function by pressing the "Run" button? If so, as this function requires inputs you will have to override the default behavior of the Run command. The Run Functions in the Editor section of the MATLAB Programming Guidedescribes how to do this.
Alternatively you can use the Command Window. Assuming that you have loaded variables t,x,u, and flag into your workspace, then in the Command Window type:
[sys,x0,str,ts] = SimuKalmanFilter(t,x,u,flag)
Pressing the Run button without modifying the default behavior is equivalent to typing
SimuKalmanFilter
in the Command Window. That is, none of the inputs are passed to the function.
2 Comments
Robert Henson
on 20 Oct 2017
If x,t,u and flag are not in the workspace then you will continue to get the error. I'm still not quite sure how you intend to use this but looking at your function it looks like when flag is 0 you generate some data, then in further iterations when flag is another value you do something with this data. If that is the case you could add this to the top of the file
if nargin == 0
flag = 0;
end
That will solve the "Not enough input" problem but that may just push the issue to the case when flag=1.
The documentation has plenty of examples to help you get familiar with how functions are called from the Command Window, from the Editor, and from within Simulink. This may be a good place to start: https://www.mathworks.com/help/simulink/sfg/s-function-examples.html
This question is closed.
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!