s函数编写
教程地址:https://www.bilibili.com/video/BV1bv411C7Y7?p=1
edit sfuntmpl 命令打开官方自带的s函数例程
下面是s函数的一个模版
%S-function for continuous state equation function [sys,x0,str,ts]=s_function(t,x,u,flag) switch flag, %Initialization case 0, [sys,x0,str,ts]=mdlInitializeSizes; case 1, sys=mdlDerivatives(t,x,u); %Outputs case 3, sys=mdlOutputs(t,x,u); %Unhandled flags case {2, 4, 9 } sys = []; %Unexpected flags otherwise error(['Unhandled flag = ',num2str(flag)]); end %mdlInitializeSizes function [sys,x0,str,ts]=mdlInitializeSizes sizes = simsizes; sizes.NumContStates = 2; sizes.NumDiscStates = 0; sizes.NumOutputs = 1; sizes.NumInputs = 1; sizes.DirFeedthrough = 0; sizes.NumSampleTimes = 0; sys=simsizes(sizes); x0=[0,0]; str=[]; ts=[]; function sys=mdlDerivatives(t,x,u) sys(1)=x(2); %sys(2)=-(25+5*sin(t))*x(2)+(133+10*sin(t))*u; sys(2)=-(25+10*rands(1))*x(2)+(133+30*rands(1))*u; function sys=mdlOutputs(t,x,u) sys(1)=x(1);