Writing S-Functions    

Example - Variable Step S-Function

The example S-function, vsfunc.c uses a variable step sample time. Variable step-size functions require a call to mdlGetTimeOfNextVarHit, which is an S-function routine that calculates the time of the next sample hit. S-functions that use the variable step sample time can only be used with variable step solvers. vsfunc is a discrete S-function that delays its first input by an amount of time determined by the second input.

This example demonstrates how to correctly work with the fixed and variable step solvers when the equations (functions) that are being integrated change during the simulation. In the transfer function used in this example, the parameters of the transfer function vary with time.

The output of vsfunc is simply the input u delayed by a variable amount of time. mdlOutputs sets the output y equal to state x. mdlUpdate sets the state vector x equal to u, the input vector. This example calls mdlGetTimeOfNextVarHit, an S-function routine that calculates and sets the "time of next hit," that is, the time when is vsfunc is next called. In mdlGetTimeOfNextVarHit the macro ssGetU is used to get a pointer to the input u. Then this call is made.

The macro ssGetT gets the simulation time t. The second input to the block, (*u[1]), is added to t, and the macro ssSetTNext sets the time of next hit equal to t+(*u[1]), delaying the output by the amount of time set in (*u[1]).

matlabroot/simulink/src/vsfunc.c


 Example - Hybrid System S-Functions Example - Zero Crossing S-Function