User`s guide
Examples of C MEX-File S-Function Blocks
3-73
Example - Zero Crossing S-Function
The example S-function, sfun_zc_sat demonstrates how to implement a
saturation block. This S-function is designed to work with either fixed or
variable step solvers. When this S-function inherits a continuous sample time,
and a variable step solver is being used, a zero crossings algorithm is used to
locate the exact points at which the saturation occurs.
matlabroot/simulink/src/sfun_zc_sat.c
/* File : sfun_zc_sat.c
* Abstract:
*
* Example of an S-function that has nonsampled zero crossings to
* implement a saturation function. This S-function is designed to be
* used with a variable or fixed step solver.
*
* A saturation is described by three equations
*
* (1) y = UpperLimit
* (2) y = u
* (3) y = LowerLimit
*
* and a set of inequalities that specify which equation to use
*
* if UpperLimit < u then use (1)
* if LowerLimit <= u <= UpperLimit then use (2)
* if u < LowerLimit then use (3)
*
* A key fact is that the valid equation 1, 2, or 3, can change at
* any instant. Nonsampled zero crossing (ZC)support helps the variable step
* solvers locate the exact instants when behavior switches from one equation
* to another.
*
* Copyright (c) 1990-1998 by The MathWorks, Inc. All Rights Reserved.
* $Revision: 1.5 $
*/
#define S_FUNCTION_NAME sfun_zc_sat
#define S_FUNCTION_LEVEL 2
#include "tmwtypes.h"
#include "simstruc.h"
#ifdef MATLAB_MEX_FILE
# include "mex.h"
#endif