Writing S-Functions    

Example - Hybrid System S-Functions

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 mdlDerivative 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.

Since 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 - Discrete State S-Function Example - Variable Step S-Function