User`s guide
2 Writing S-Functions As M-Files
2-2
Introduction
An M-file that definesan S-Function blockmust provide informationabout the
model; Simulink needs this information during simulation. As t he simulation
proceeds, Simulink, the ODE solver, and the M-file interact to perform specific
tasks. These tasks include defining initial conditionsandb lockcharacteristics,
and computing derivatives, discrete states, and outputs.
Simulink p rovides a template M-file S-function that includes statements that
define necessary functions, as well as comments to help you write the code
needed for your S-function block. This template file,
sfuntmpl.m,isinthe
directory
toolbox/simulink/blocks under the MATLAB root directory.
M-file S-functions work by making a sequence of calls to S-function routines,
which a re M-code functions that perform tasks required by your S-function.
This table lists the S-function routines available to M-file S-functions.
Table 2-1: M-File S-Function Routines
S-Function Routine Description
mdlInitializesizes
Defines basic S-Function block
characteristics, including sample times,
initial conditions of continuous and discrete
states, and the
sizes array.
mdlDerivatives
Calculat esthe derivativesof the continuous
state variables.
mdlUpdate
Updates discrete states, sample times, and
major time step requirements.
mdlOutputs
Calculates the outputs of the S-function.
mdlGetTimeOfNextVarHit
Calculates the time of the next hit in
absolute time. This routine is used only
when you specify a variable discrete-time
sample time in
mdlInitializeSizes.
mdlTerminate
P erforms any necessary end of simulation
tasks .