问一下关于simulink中的S函数的问题
发布网友
发布时间:2022-12-03 20:53
我来回答
共1个回答
热心网友
时间:2023-11-18 23:56
改后的S函数见下。不过对于这种简单的系统,似乎没必要使用S函数,直接用几种基本模块搭建更方便。
function [sys,x0,str,ts] = sfun_mengyongye123(t,x,u,flag)
switch flag,
case 0,
[sys,x0,str,ts]=mdlInitializeSizes;
case 1,
sys=mdlDerivatives(t,x,u);
case 2,
sys=mdlUpdate(t,x,u);
case 3,
sys=mdlOutputs(t,x,u);
case 4,
sys=mdlGetTimeOfNextVarHit(t,x,u);
case 9,
sys=mdlTerminate(t,x,u);
otherwise
error(['Unhandled flag = ',num2str(flag)]);
end
function [sys,x0,str,ts]=mdlInitializeSizes
sizes = simsizes;
sizes.NumContStates = 2;
sizes.NumDiscStates = 0;
sizes.NumOutputs = 2;
sizes.NumInputs = 0;
sizes.DirFeedthrough = 0;
sizes.NumSampleTimes = 1; % at least one sample time is needed
sys = simsizes(sizes);
x0 = [25 2];
str = [];
ts = [0 0];
function sys=mdlDerivatives(t,x,u)
sys(1) = (1-0.1*x(2))*x(1);
sys(2) = (-0.5+0.02*x(1))*x(2);
function sys=mdlUpdate(t,x,u)
sys = [];
function sys=mdlOutputs(t,x,u)
sys = x;
function sys=mdlGetTimeOfNextVarHit(t,x,u)
sampleTime = 1; % Example, set the next hit to be one second later.
sys = t + sampleTime;
function sys=mdlTerminate(t,x,u)
sys = [];追问谢啦 ,这是作业,我们都没学过老师突然叫做的!~没办法 看来好久都看不懂
追答S函数有点像八股文,有固定的套路,主要是搞明白模板文件中的各子函数调用时机及作用,以及相关参数的作用(尤其sizes结构体和ts)。