Writing S-Functions    

Example - Discrete State S-Function

Simulink includes a function called dsfunc.m, which is an example of a discrete state system modeled in an S-function. This function is similar to csfunc.m, the continuous state S-function example. The only difference is that mdlUpdate is called instead of mdlDerivatives. mdlUpdate updates the discrete states when flag = 2. Note that for a single-rate discrete S-function, Simulink calls the mdlUpdate, mdlOutputs, and mdlGetTimeOfNextVarHit (if needed) routines only on sample hits. Here is the code for the M-file S-function.

The above example conforms to the simulation stages discussed earlier in chapter 1. The system discrete state equations are of the form

so that very general sets of difference equations can be modeled using dsfunc.m. This is similar to the built-in Discrete State-Space block. You can use dsfunc.m as a starting point for modeling discrete state-space systems with time-varying coefficients.


  Simple M-File S-Function Example Example - Hybrid System S-Function