Writing S-Functions    

Example of a Continuous State S-Function

The matlabroot/simulink/src/csfunc.c example shows how to model a continuous system with states in a C MEX S-function. In continuous state integration, there is a set of states that Simulink's solvers integrate using the following equations.

S-functions that contain continuous states implement a state-space equation. The output portion is placed in mdlOutputs and the derivative portion in mdlDerivatives. To visualize how the integration works, refer to the flowchart in How Simulink Interacts with C S-Functions. The output equation above corresponds to the mdlOutputs in the major time step. Next, the example enters the integration section of the flowchart. Here Simulink performs a number of minor time steps during which it calls mdlOutputs and mdlDerivatives. Each of these pairs of calls is referred to as an integration stage. The integration returns with the continuous states updated and the simulation time moved forward. Time is moved forward as far as possible, providing that error tolerances in the state are met. The maximum time step is subject to constraints of discrete events such as the actual simulation stop time and the user-imposed limit.

Note that csfunc.c specifies that the input port has direct feedthrough. This is because matrix D is initialized to a nonzero matrix. If D is set equal to a zero matrix in the state-space representation, the input signal isn't used in mdlOutputs. In this case, the direct feedthrough can be set to 0, which indicates that csfunc.c does not require the input signal when executing mdlOutputs.

matlabroot/simulink/src/csfunc.c


  S-Function Examples Example of a Discrete State S-Function