This is machine translation

Translated by Microsoft
Mouseover text to see original. Click the button below to return to the English version of the page.

Note: This page has been translated by MathWorks. Click here to see
To view all translated materials including this page, select Country from the country navigator on the bottom of this page.


Compute the C MEX S-function's derivatives




C, C++


void mdlDerivatives(SimStruct *S)



SimStruct representing an S-Function block.


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.


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:

#if defined(MDL_DERIVATIVES) && defined(MATLAB_MEX_FILE) 
static void mdlDerivatives(SimStruct *S) 
   /* Add mdlDerivatives code here *
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.


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

Introduced before R2006a