Writing S-Functions    

Example of a Hybrid System S-Function

The S-function matlabroot/simulink/src/mixedm.c is an example of a hybrid (a combination of continuous and discrete states) system. mixedm.c combines elements of csfunc.c and dsfunc.c. If you have a hybrid system, place your continuous equations in mdlDerivatives and your discrete equations in mdlUpdate. In addition, you need to check for sample hits to determine at what point your S-function is being called.

In Simulink block diagram form, the S-function mixedm.c looks like

which implements a continuous integrator followed by a discrete unit delay.

Because there are no tasks to complete at termination, mdlTerminate is an empty function. mdlDerivatives calculates the derivatives of the continuous states of the state vector, x, and mdlUpdate contains the equations used to update the discrete state vector, x.

matlabroot/simulink/src/mixedm.c


  Example of a Discrete State S-Function Example of a Variable-Step S-Function