📄 ch2example16sfun.c
字号:
mlfAssign(&C, _mxarray4_);
/*
* D=0;
*/
mlfAssign(&D, _mxarray6_);
/*
* switch flag,
*/
{
mxArray * v_ = mclInitialize(mclVa(flag, "flag"));
if (mclSwitchCompare(v_, _mxarray6_)) {
/*
* case 0 % flag=0 初始化
* [sys,x0,str,ts]=mdlInitializeSizes(A,B,C,D);
*/
mlfAssign(
&sys,
mlfCh2example16sfun_mdlInitializeSizes(
x0,
str,
ts,
mclVv(A, "A"),
mclVv(B, "B"),
mclVv(C, "C"),
mclVv(D, "D")));
/*
* % 可将A,B,C,D矩阵送入初始化函数
* case 1 % flag=1 计算连续系统状态方程(导数)
*/
} else if (mclSwitchCompare(v_, _mxarray7_)) {
/*
* sys=mdlDerivatives(t,x,u,A,B,C,D);
*/
mlfAssign(
&sys,
mlfCh2example16sfun_mdlDerivatives(
mclVa(t, "t"),
mclVa(x, "x"),
mclVa(u, "u"),
mclVv(A, "A"),
mclVv(B, "B"),
mclVv(C, "C"),
mclVv(D, "D")));
/*
* case 3 % flag=3 计算输出
*/
} else if (mclSwitchCompare(v_, _mxarray8_)) {
/*
* sys=mdlOutputs(t,x,u,A,B,C,D);
*/
mlfAssign(
&sys,
mlfCh2example16sfun_mdlOutputs(
mclVa(t, "t"),
mclVa(x, "x"),
mclVa(u, "u"),
mclVv(A, "A"),
mclVv(B, "B"),
mclVv(C, "C"),
mclVv(D, "D")));
/*
* case { 2, 4, 9 } % 其它作不处理的flag
*/
} else if (mclSwitchCompare(v_, _mxarray9_)) {
/*
* sys=[]; % 无用的flag时返回sys为空矩阵
*/
mlfAssign(&sys, _mxarray14_);
/*
* otherwise % 异常处理
*/
} else {
/*
* error(['Unhandled flag = ',num2str(flag)]);
*/
mlfError(
mlfHorzcat(
_mxarray15_, mlfNum2str(mclVa(flag, "flag"), NULL), NULL),
NULL);
/*
* end
*/
}
mxDestroyArray(v_);
}
mclValidateOutput(sys, 1, nargout_, "sys", "ch2example16sfun");
mclValidateOutput(*x0, 2, nargout_, "x0", "ch2example16sfun");
mclValidateOutput(*str, 3, nargout_, "str", "ch2example16sfun");
mclValidateOutput(*ts, 4, nargout_, "ts", "ch2example16sfun");
mxDestroyArray(A);
mxDestroyArray(B);
mxDestroyArray(C);
mxDestroyArray(D);
mxDestroyArray(ans);
mxDestroyArray(flag);
mxDestroyArray(u);
mxDestroyArray(x);
mxDestroyArray(t);
mclSetCurrentLocalFunctionTable(save_local_function_table_);
return sys;
/*
* % 主函数结束
* % 子函数实现(1)初始化函数----------------------------------
*/
}
/*
* The function "Mch2example16sfun_mdlInitializeSizes" is the implementation
* version of the "ch2example16sfun/mdlInitializeSizes" M-function from file
* "d:\syb\after20060609\清华大学出版社约稿\写作初稿\prog\ch2example16sfun.m"
* (lines 25-40). It contains the actual compiled code for that M-function. It
* is a static function and must only be called from one of the interface
* functions, appearing below.
*/
/*
* function [sys,x0,str,ts] = mdlInitializeSizes(A,B,C,D) %
*/
static mxArray * Mch2example16sfun_mdlInitializeSizes(mxArray * * x0,
mxArray * * str,
mxArray * * ts,
int nargout_,
mxArray * A,
mxArray * B,
mxArray * C,
mxArray * D) {
mexLocalFunctionTable save_local_function_table_
= mclSetCurrentLocalFunctionTable(
&_local_function_table_ch2example16sfun);
mxArray * sys = NULL;
mxArray * sizes = NULL;
mclCopyArray(&A);
mclCopyArray(&B);
mclCopyArray(&C);
mclCopyArray(&D);
/*
* sizes = simsizes; % 获取
*/
mlfAssign(&sizes, mlfSimsizes(NULL));
/*
* sizes.NumContStates = 3; % 连续系统的状态数为3
*/
mlfIndexAssign(&sizes, ".NumContStates", _mxarray8_);
/*
* sizes.NumDiscStates = 0; % 离散系统的状态数,对于本系统此句可不用
*/
mlfIndexAssign(&sizes, ".NumDiscStates", _mxarray6_);
/*
* sizes.NumOutputs = 1; % 输入信号数目是1
*/
mlfIndexAssign(&sizes, ".NumOutputs", _mxarray7_);
/*
* sizes.NumInputs = 1; % 输出信号数目是1
*/
mlfIndexAssign(&sizes, ".NumInputs", _mxarray7_);
/*
* sizes.DirFeedthrough = 0; % 因为该系统不是直通的
*/
mlfIndexAssign(&sizes, ".DirFeedthrough", _mxarray6_);
/*
* sizes.NumSampleTimes = 1; % 这里必须为1
*/
mlfIndexAssign(&sizes, ".NumSampleTimes", _mxarray7_);
/*
* sys = simsizes(sizes);
*/
mlfAssign(&sys, mlfSimsizes(mclVv(sizes, "sizes")));
/*
* str = []; % 通常为空矩阵
*/
mlfAssign(str, _mxarray14_);
/*
* x0 = [0;0;0]; % 初始状态矩阵x0 (零状态情况)
*/
mlfAssign(x0, _mxarray17_);
/*
* ts = [0 0]; % 表示连续取样时间的仿真
*/
mlfAssign(ts, _mxarray19_);
mclValidateOutput(
sys, 1, nargout_, "sys", "ch2example16sfun/mdlInitializeSizes");
mclValidateOutput(
*x0, 2, nargout_, "x0", "ch2example16sfun/mdlInitializeSizes");
mclValidateOutput(
*str, 3, nargout_, "str", "ch2example16sfun/mdlInitializeSizes");
mclValidateOutput(
*ts, 4, nargout_, "ts", "ch2example16sfun/mdlInitializeSizes");
mxDestroyArray(sizes);
mxDestroyArray(D);
mxDestroyArray(C);
mxDestroyArray(B);
mxDestroyArray(A);
mclSetCurrentLocalFunctionTable(save_local_function_table_);
return sys;
/*
* % 初始化函数结束
*
* % 子函数实现(2)系统状态方程函数-----------------------------
*/
}
/*
* The function "Mch2example16sfun_mdlDerivatives" is the implementation
* version of the "ch2example16sfun/mdlDerivatives" M-function from file
* "d:\syb\after20060609\清华大学出版社约稿\写作初稿\prog\ch2example16sfun.m"
* (lines 40-45). It contains the actual compiled code for that M-function. It
* is a static function and must only be called from one of the interface
* functions, appearing below.
*/
/*
* function sys=mdlDerivatives(t,x,u,A,B,C,D) % 系统状态方程函数
*/
static mxArray * Mch2example16sfun_mdlDerivatives(int nargout_,
mxArray * t,
mxArray * x,
mxArray * u,
mxArray * A,
mxArray * B,
mxArray * C,
mxArray * D) {
mexLocalFunctionTable save_local_function_table_
= mclSetCurrentLocalFunctionTable(
&_local_function_table_ch2example16sfun);
mxArray * sys = NULL;
mclCopyArray(&t);
mclCopyArray(&x);
mclCopyArray(&u);
mclCopyArray(&A);
mclCopyArray(&B);
mclCopyArray(&C);
mclCopyArray(&D);
/*
* sys = A*x+B*u ; % 这里写入系统的状态方程矩阵形式即可
*/
mlfAssign(
&sys,
mclPlus(
mclMtimes(mclVa(A, "A"), mclVa(x, "x")),
mclMtimes(mclVa(B, "B"), mclVa(u, "u"))));
mclValidateOutput(
sys, 1, nargout_, "sys", "ch2example16sfun/mdlDerivatives");
mxDestroyArray(D);
mxDestroyArray(C);
mxDestroyArray(B);
mxDestroyArray(A);
mxDestroyArray(u);
mxDestroyArray(x);
mxDestroyArray(t);
mclSetCurrentLocalFunctionTable(save_local_function_table_);
return sys;
/*
* % 系统状态函数结束
*
* % 子函数实现(3)系统输出方程函数-----------------------------
*/
}
/*
* The function "Mch2example16sfun_mdlOutputs" is the implementation version of
* the "ch2example16sfun/mdlOutputs" M-function from file
* "d:\syb\after20060609\清华大学出版社约稿\写作初稿\prog\ch2example16sfun.m"
* (lines 45-50). It contains the actual compiled code for that M-function. It
* is a static function and must only be called from one of the interface
* functions, appearing below.
*/
/*
* function sys = mdlOutputs(t,x,u,A,B,C,D)
*/
static mxArray * Mch2example16sfun_mdlOutputs(int nargout_,
mxArray * t,
mxArray * x,
mxArray * u,
mxArray * A,
mxArray * B,
mxArray * C,
mxArray * D) {
mexLocalFunctionTable save_local_function_table_
= mclSetCurrentLocalFunctionTable(
&_local_function_table_ch2example16sfun);
mxArray * sys = NULL;
mclCopyArray(&t);
mclCopyArray(&x);
mclCopyArray(&u);
mclCopyArray(&A);
mclCopyArray(&B);
mclCopyArray(&C);
mclCopyArray(&D);
/*
* sys = C*x; % 这里写入系统的输出方程矩阵形式即可
*/
mlfAssign(&sys, mclMtimes(mclVa(C, "C"), mclVa(x, "x")));
mclValidateOutput(sys, 1, nargout_, "sys", "ch2example16sfun/mdlOutputs");
mxDestroyArray(D);
mxDestroyArray(C);
mxDestroyArray(B);
mxDestroyArray(A);
mxDestroyArray(u);
mxDestroyArray(x);
mxDestroyArray(t);
mclSetCurrentLocalFunctionTable(save_local_function_table_);
return sys;
/*
* % 注意,如果使用语句sys = C*x+D*u ;代替上句,即使D=0,
* % 也要将初始化函数中的sizes.DirFeedthrough 设为1
* % 即系统存在输入输出之间的直通项, 否则执行将出现错误
* % 系统输出方程函数结束
*/
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -