Writing S-Functions | ![]() ![]() |
Examples of M-File S-Functions
The simple example discussed above (timestwo
) has no states. Most S-Function blocks require the handling of states, whether continuous or discrete. The sections that follow discuss four common types of systems you can model in Simulink using S-functions:
All examples are based on the M-file S-function template found in sfuntmpl.m
.
Example - Continuous State S-Function
Simulink includes a function called csfunc.m
, which is an example of a continuous state system modeled in an S-function. Here is the code for the M-file S-function.
function [sys,x0,str,ts] = csfunc(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.09 -0.01 1 0]; B=[ 1 -7 0 -2]; C=[ 0 2 1 -5]; D=[-3 0 1 0]; % % Dispatch the flag. % switch flag, case 0 [sys,x0,str,ts]=mdlInitializeSizes(A,B,C,D); % Initialization case 1 sys = mdlDerivatives(t,x,u,A,B,C,D); % Calculate derivatives case 3 sys = mdlOutputs(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 % End of csfunc. %============================================================== % mdlInitializeSizes % Return the sizes, initial conditions, and sample times for the % S-function. %============================================================== % function [sys,x0,str,ts] = mdlInitializeSizes(A,B,C,D) % % Call simsizes for a sizes structure, fill it in and convert it % to a sizes array. % sizes = simsizes; sizes.NumContStates = 2; sizes.NumDiscStates = 0; sizes.NumOutputs = 2; sizes.NumInputs = 2; sizes.DirFeedthrough = 1; % Matrix D is nonempty. sizes.NumSampleTimes = 1; sys = simsizes(sizes); % % Initialize the initial conditions. % x0 = zeros(2,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 of mdlInitializeSizes. % %============================================================== % mdlDerivatives % Return the derivatives for the continuous states. %============================================================== function sys = mdlDerivatives(t,x,u,A,B,C,D) sys = A*
x + B*
u; % 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 that very general sets of continuous differential equations can be modeled using csfunc.m
. Note that csfunc.m
is similar to the built-in State-Space block. 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 call to this routine. The memory allocated to the derivative vector changes during execution.
![]() | A Simple M-File S-Function Example | Example - Discrete State S-Function | ![]() |