Writing S-Functions    

Example of a 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.

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 the next hit, that is, the time when 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 the 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 of a Hybrid System S-Function Example of a Zero Crossing S-Function