User`s guide
Examples of M-File S-Functions
2-13
% Call simsizes for a sizes structure, fill it in, and convert it
% to a sizes array.
sizes = simsizes;
sizes.NumContStates = 0;
sizes.NumDiscStates = 2;
sizes.NumOutputs = 2;
sizes.NumInputs = 2;
sizes.DirFeedthrough = 1; % Matrix D is non-empty.
sizes.NumSampleTimes = 1;
sys = simsizes(sizes);
x0 = ones(2,1); % Initialize the discrete states.
str = []; % Set str to an empty matrix.
ts = [1 0]; % sample time: [period, offset]
% End of mdlInitializeSizes.
%==============================================================
% Update the discrete states
%==============================================================
function sys = mdlUpdates(t,x,u,A,B,C,D)
sys = A*x + B*u;
% End of mdlUpdate.
%==============================================================
% Calculate outputs
%==============================================================
function sys = mdlOutputs(t,x,u,A,B,C,D)
sys = C*x + D*u;
% End of mdlOutputs.
The above example conforms to the simulation stages discussed earlier in
chapter 1. The system discrete state equations are of the form
x(n+1) = Ax(n) + Bu(n)
y(n) = Cx(n) + Du(n)
so that very general sets of difference equations can be modeled using
dsfunc.m. This is similar to the built-in Discrete State-Space block. You can
use
dsfunc.m asastarting pointfor modeling discrete state-space systems with
time-varying coefficients.