用MATLAB改写状态空间表达式,c语言编写s函数时,状态空间表达式怎么表达?

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函数是不是这样写?

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值