Writing S-Functions | ![]() ![]() |
Example - Hybrid System S-Function
Simulink includes a function called mixedm.m
, which is an example of a hybrid system (a combination of continuous and discrete states) modeled in an S-function. Handling hybrid systems is fairly straightforward; the flag
parameter forces the calls to the correct S-function subroutine for the continuous and discrete parts of the system. One subtlety of hybrid S-functions (or any multirate S-function) is that Simulink calls the mdlUpdate
, mdlOutputs
, and mdlGetTimeOfNextVarHit
routines at all sample times. This means that in these routines you must test to determine which sample hit is being processed and only perform updates that correspond to that sample hit.
mixed.m
models a continuous Integrator followed by a discrete Unit Delay. In Simulink block diagram form, the model looks like this.
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; sizes.NumOutputs = 1; sizes.NumInputs = 1; sizes.DirFeedthrough = 0; sizes.NumSampleTimes = 2; sys = simsizes(sizes); x0 = ones(2,1); str = []; ts = [0, 0 % sample time dperiod, doffset]; % End of mdlInitializeSizes. % %============================================================== % mdlDerivatives % Compute derivatives for continuous states. %============================================================== % function sys = mdlDerivatives(t,x,u) sys = u; % end of mdlDerivatives. % %============================================================== % mdlUpdate % Handle discrete state updates, sample time hits, and major time % step requirements. %============================================================== % function sys = mdlUpdate(t,x,u,dperiod,doffset) % Next discrete state is output of the integrator. % Return next discrete state if we have a sample hit within a % tolerance of 1e-8. If we don't have a sample hit, return [] to % indicate that the discrete state shouldn't change. % if abs(round((t-doffset)/dperiod)-(t-doffset)/dperiod) < 1e-8 sys = x(1); % mdlUpdate is "latching" the value of the % continuous state, x(1), thus introducing a delay. else sys = []; % This is not a sample hit, so return an empty end % matrix to indicate that the states have not % changed. % End of mdlUpdate. % %============================================================== % mdlOutputs % Return the output vector for the S-function. %============================================================== % function sys = mdlOutputs(t,x,u,doffset,dperiod) % Return output of the unit delay if we have a % sample hit within a tolerance of 1e-8. If we % don't have a sample hit then return [] indicating % that the output shouldn't change. % if abs(round((t-doffset)/dperiod)-(t-doffset)/dperiod) < 1e-8 sys = x(2); else sys = []; % This is not a sample hit, so return an empty end % matrix to indicate that the output has not changed % End of mdlOutputs.
![]() | Example - Discrete State S-Function | Example - Variable Sample Time S-Function | ![]() |