MATLAB Answers

Info

This question is closed.

0

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.

Asked by Mohamed Chaouechi on 18 Oct 2017
Latest activity Closed by Mohamed Chaouechi on 15 May 2018
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

Post the code calling SimuKalmanFilter(t,x,u,flag) and the complete error message.
>> SimuKalmanFilter Not enough input arguments. Error in SimuKalmanFilter (line 2) switch flag OR >>SimuKalmanFilter(t,x,u,flag) Undefined function or variable 't'.

1 Answer

Answer by 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

Well, the problem is that I am using an S-Function block with SIMULINK, and the S function has the code that I mentioned above to try to visualize the signal before and after filtering. So do you recommend entering x,t,u and flag at the workspace and then run it? I just need it to run and to be able to visualize the result as I am using the filter as a block in future application with a PLC, so the code has to be correct , compilation should be done , then visualization can also be correct, in this case I can say the system is working , in this case I am still trying a general case for the filter , in other words , I am trying to write a code where I can maybe add an input at the SIMULINK as a constant or a random number, summed together then passing through the filter , then scope the result before filtering and after filtering. So for the matrice values of x,y,t and flag , is it going to be the values I should enter at the SIMULINK input? I appreciate your help , thank you
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