User`s guide
Examples of M-File S-Functions
2-15
Here is the code for the M-file S-function:
function [sys,x0,str,ts] = mixedm(t,x,u,flag)
% A hybrid system example that implements a hybrid system
% consisting of a continuous integrator (1/s) in series with a
% unit delay (1/z).
%
% Set the sampling period and offset for unit delay.
dperiod = 1;
doffset = 0;
switch flag,
case 0 % Initialization
[sys,x0,str,ts] = mdlInitializeSizes(dperiod,doffset);
case 1
sys = mdlDerivatives(t,x,u); % Calculate derivatives
case 2
sys = mdlUpdate(t,x,u,dperiod,doffset); % Update disc states
case 3
sys = mdlOutputs(t,x,u,doffset,dperiod); % Calculate outputs
case {4, 9}
sys = []; % Unused flags
otherwise
error(['unhandled flag = ',num2str(flag)]); % Error handling
end
% End of mixedm.
%
%==============================================================
% mdlInitializeSizes
% Return the sizes, initial conditions, and sample times for the
% S-function.
%==============================================================
function [sys,x0,str,ts] = mdlInitializeSizes(dperiod,doffset)
sizes = simsizes;
sizes.NumContStates = 1;
sizes.NumDiscStates = 1;