0

I am calling a m-file in s-function, for the continuous state space model. It is giving error in using flag with switch at line no 24.

function [sys,x0,str,ts] = latitude(t,x,u,flag)
% CSFUNC An example M-file S-function for defining a system of
% continuous state equations:
% x' = Ax + Bu
% y = Cx + Du
%
% Generate a continuous linear system:
A=[0.08774    7.315    81.13    6.589
   0.01938   0.4011    15.58    1.182
   0.0986     2.04    73.46  0.01816
   0        1  -0.1425        0];
B=[  0.5264
     0.1163
     0.5916
     0];
C=[1      0      0      0
   0      1      0      0
   0      0      1      0
   0      0      0      1];
D=[0
   0
   0
   0];
switch flag,
case 0,
[sys,x0,str,ts]=latInitializeSizes(A,B,C,D); % Initialization
case 1,
sys = latDerivatives(t,x,u,A,B,C,D); % Calculate derivatives
case 3,
sys = latOutputs(t,x,u,A,B,C,D); % Calculate outputs
case { 2, 4, 9 } % Unused flags
sys = [];
otherwise
error(['Unhandled flag = ',num2str(flag)]); % Error handling
end

The error is:

"??? Input argument "flag" is undefined. Error in ==> latitude at 24 switch flag,"

The sub functions for the main m-file are:

01:

function [sys,x0,str,ts] = latInitializeSizes(A,B,C,D)
%
% Call simsizes for a sizes structure, fill it in and convert it
% to a sizes array.
%
sizes = simsizes;
sizes.NumContStates = 4;
sizes.NumDiscStates = 0;
sizes.NumOutputs = 4;
sizes.NumInputs = 1;
sizes.DirFeedthrough = 0; % Matrix D is nonempty.
sizes.NumSampleTimes = 1;
sys = simsizes(sizes);
%
% Initialize the initial conditions.
%
x0 = zeros(4,1);
%
% str is an empty matrix.
%
str = [];
%
% Initialize the array of sample times; in this example the sample
% time is continuous, so set ts to 0 and its offset to 0.
%
ts = [0 0];
end
% End of mdlInitializeSizes.

02:

function sys = latDerivatives(t,x,u,A,B,C,D)
A=[0.08774    7.315    81.13    6.589
   0.01938   0.4011    15.58    1.182
   0.0986     2.04    73.46  0.01816
   0        1  -0.1425        0];
B=[  0.5264
     0.1163
     0.5916
     0];
u=[1];
 x=[0
  0
   0
   0];
sys = A*x + B*u;
end
% End of mdlDerivatives.

03:

function sys = latOutputs(t,x,u,A,B,C,D)

C=[1      0      0      0
   0      1      0      0
   0      0      1      0
   0      0      0      1];
D=[0
   0
   0
   0];
u=[1]; 
x=[0
   0
   0
   0];
sys = C*x + D*u;
end

The sub functions are not giving any error.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
  • Is there a particular reason why you don't just use a State Space block in Simulink? Also, if you are just writing this to learn how to use s-functions, you should be using [Level 2 s-functions](http://www.mathworks.com/help/simulink/sfg/maintaining-level-1-matlab-s-functions.html) and it maybe that your function is not being used as a Level 1. – chthonicdaemon Nov 30 '13 at 12:02
  • It is important to use the m-file called in simulink. Further I have to do collision avoidance in state flow. – user3051822 Dec 12 '13 at 06:16
  • Why is it important? I don't see anything in your code that cannot be done with the [State Space](http://www.mathworks.com/help/simulink/slref/statespace.html) block. Also, the form of s-function you are using is deprecated, as explained in the [Matlab documentation](http://www.mathworks.com/help/simulink/sfg/maintaining-level-1-matlab-s-functions.html). If you are developing this as a new work, you should first not use s-functions where you don't need to, and if there is something more to the calculation than you show here, use level 2 s-functions. – chthonicdaemon Dec 12 '13 at 07:55
  • Ok thanks alot, now i am using level 2 s-functions – user3051822 Jan 02 '14 at 03:08

0 Answers0