C:\Users\zqm\Desktop
对于这个状态方程。s函数是不是可以这样写?
#define S_FUNCTION_NAME 1
#define S_FUNCTION_LEVEL 2
#include "simstruc.h"
#include "math.h"
#define NUM_INPORTs 1
#define NUM_INPORTWIDTH 1
#define NUM_OUTPORTs 1
#define NUM_OUTPORTWIDTH 4
#define NUM_PARAMs 0
#define NUM_RWORK 4
/*=============================*
* S-function routines *
*=============================*/
static void mdlInitializeSizes(SimStruct *S)
{
ssSetNumContStates(S, 4);
ssSetNumSFcnParams(S, NUM_PARAMs);
if (ssGetNumSFcnParams(S) != ssGetSFcnParamsCount(S))
{
return;
}
if (!ssSetNumInputPorts(S, NUM_INPORTs)) return;
ssSetInputPortWidth (S, 0, NUM_INPORTWIDTH);
ssSetInputPortDirectFeedThrough (S, 0, 1);
ssSetInputPortSampleTime(S, 0, CONTINUOUS_SAMPLE_TIME);
ssSetInputPortOffsetTime(S, 0, 0.0);
if (!ssSetNumOutputPorts(S, NUM_OUTPORTs)) return;
ssSetOutputPortWidth (S, 0, NUM_OUTPORTWIDTH);
ssSetNumSampleTimes (S, 1);
ssSetNumRWork (S, NUM_RWORK); /* Specify the width of the real work vector */
ssSetOptions(S, SS_OPTION_EXCEPTION_FREE_CODE); /* General options (SS_OPTION_xx) */
}
/********************/
static void mdlInitializeSampleTimes(SimStruct *S)
{
ssSetSampleTime(S, 0, CONTINUOUS_SAMPLE_TIME);
ssSetOffsetTime(S, 0, 0.0);
}
/* *********************************************** */
#define MDL_INITIALIZE_CONDITIONS /* Define Initial Conditions */
static void mdlInitializeConditions(SimStruct *S)
{ real_T *RWV = ssGetRWork (S);//real work vector
real_T *x0 = ssGetContStates(S);
int_T i;
for (i=1; i<4; i++)
{
*x1++ = 1.0;
}
RWV[0] = 0.0; //x1 Initial Value
RWV[1] = 0.0; //x2 Initial Value
RWV[2] = 1.0*4.0e6; //x3 Initial Value
RWV[3] = 1.0*4.0e6; //x4 Initial Value
}
#define MDL_DERIVATIVES
static void mdlDerivatives(SimStruct *S)
{
real_T *dx = ssGetdX(S);
real_T *x0 = ssGetContStates(S);
real_T *RWV = ssGetRWork (S); //pointer to work vector
InputRealPtrsType uPtrs = ssGetInputPortRealSignalPtrs(S,0);
u = *uPtrs[0];
x0[0] = RWV[0];
x0[1] = RWV[1];
x0[2] = RWV[2];
x0[3] = RWV[3];
dx0[0]=x0[1];
dx0[0]=x0[1];
dx0[1]=x0[1]+x0[0]+x0[2]+x0[3];
dx0[2]=x0[1]-x0[2]+u;
dx0[3]=x0[1]-x0[3]+u;
}
/* *********************************************** */
static void mdlOutputs(SimStruct *S, int_T tid)
{
real_T *out = ssGetOutputPortRealSignal (S,0); //pointer to output port
real_T *RWV = ssGetRWork (S);//pointer to work vector
real_T *x0 = ssGetContStates(S);
/* *********************************************** */
/* Store parameters */
RWV[0]=x0[1];
RWV[1]=x0[2];
RWV[2]=x0[3];
RWV[3]=x0[4];
/*Output */
out[0] = x0[1];
out[1] = x0[2];
out[2] = x0[3];
out[3] = x0[4]
}
/* *********************************************** */
static void mdlTerminate(SimStruct *S)
{
}
/*=============================*
* Required S-function trailer *
*=============================*/
#ifdef MATLAB_MEX_FILE
#include "simulink.c"
#else
#include "cg_sfun.h"
#endif
对于图片的状态方程,要输出各个状态的值,s函数是不是这样写?