dx as output of Level 2 C-Code S-Function

1 view (last 30 days)
Marco Schmid
Marco Schmid on 3 May 2017
Answered: Michael Proff on 24 Jan 2020
I'd like to have all my derivatives in my output-port. Does anybody know a trick to do that? I tried to access the dx Variables with the function ssGetdX(S) but it doesn't work. Maybe i have to create a initialize condition?
/* Function: mdlOutputs =======================================================
* Abstract:
* Define output of S-function
*/
static void mdlOutputs(SimStruct *S, int_T tid)
{
real_T *y = ssGetOutputPortRealSignal(S,0);
real_T *x = ssGetContStates(S);
real_T *dx = ssGetdX(S);
InputRealPtrsType uPtrs = ssGetInputPortRealSignalPtrs(S,0);
UNUSED_ARG(tid); /* not used in single tasking mode */
y[0]=dx[0];
y[1]=x[1];
y[2]=x[2];
y[3]=x[3];
}

Answers (1)

Michael Proff
Michael Proff on 24 Jan 2020
You can but not for the first time (initial conditions) the mdlOutputs function is called.
Regarding to the documentation the mandatory mdlOutputs function is called before the optional mdlDerivatives function.
You can ignore the first time by checking the dx for NULL pointer, which it is for the first time mdlOutputs is called. However, you would have zeros for the initial conditions. To avoid that, you would have to evaluate the derivatives also for the initial conditions inside the mdlOutputs function.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!