User`s guide

Examples of M-File S-Functions
2-11
% End of mdlDerivatives.
%
%==============================================================
% mdlOutputs
% Return the block 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 this
chapter. Unlike
timestwo.m, this example invokes mdlDerivatives to
calculate the derivatives of the continuous state variables when
flag = 1.The
system state equations are of the form
x'= Ax + Bu
y = Cx + Du
so t hat very general sets of continuous differential equations can be modeled
using
csfunc.m.Notethatcsfunc.m issimilartothebuilt-inState-Spaceblock.
This S-function can be used as a starting point for a block that models a
state-space system with time-varying coefficients.
Each time the
mdlDerivatives routine is called it must explicitly set the value
of all derivatives. The derivative vector does not maintain the values from the
last ca ll to this routine. The memory allocated to the derivative vector changes
during execution.
Example - Discrete State S-Function
Simulinkincludes afunction calleddsfunc.m, whichis anexample ofa discrete
statesystemmodeledinanS-function.Thisfunctionissimilarto
csfunc.m,the
continuous state S-function example. The only difference is that
mdlUpdate is
called instead of
mdlDerivative. mdlUpdate updates the discrete states when
the
flag = 2. Notethat for a single-ratediscrete S-function, Simulink calls the
mdlUpdate, mdlOutput,andmdlGetTimeOfNextVarHit (if needed)routinesonly
on sample hits. Here is the code for the M-file S-function: