Contents

mdlDerivatives

Compute the C MEX S-function's derivatives

Required

No

Languages

C, C++

Syntax

#define MDL_DERIVATIVES
void mdlDerivatives(SimStruct *S)

Arguments

S

SimStruct representing an S-Function block.

Description

The Simulink® engine invokes this optional method at each time step to compute the derivatives of the S-function's continuous states. This method should store the derivatives in the S-function's state derivatives vector. In a C MEX S-function, use ssGetdX to get a pointer to the derivatives vector.

Each time the mdlDerivatives routine is called, it must explicitly set the values of all derivatives. The derivative vector does not maintain the values from the last call to this routine. The memory allocated to the derivative vector changes during execution.

    Note   If you have Simulink Coder™, when generating code for a noninlined C MEX S-function that contains this method, make sure the method is not wrapped in a #if defined(MATLAB_MEX_FILE) statement. For example:

    #define MDL_DERIVATIVES 
    #if defined(MDL_DERIVATIVES) && defined(MATLAB_MEX_FILE) 
    static void mdlDerivatives(SimStruct *S) 
    { 
       /* Add mdlDerivatives code here *
    } 
    #endif 

    The define statement makes the mdlDerivatives method available only to a MATLAB® MEX file. If the S-function is not inlined, the Simulink Coder product cannot use this method, resulting in link or run-time errors.

Example

For a C MEX S-function example, see csfunc.ccsfunc.c.

Was this topic helpful?