matlab s-function 测试例子

摘自  Simulink仿真及代码生成技术入门到精髓  一书

matlab <wbr>s-function <wbr>测试例子

看完美女,开始吧~~~~


一:编写m文件

function[sys,x0,str,ts,simStateCompliance]=sfun_AI_000(t,x,u,flag,A,B,C)

switch flag,
    case 0,
        [sys,x0,str,ts,simStateCompliance]=mdlInitializeSizes;
    case 1,
        sys = mdlDerivatives(t,x,u);
    case 2,
        sys = mdlUpdate(t,x,u,A,B);
    case 3,
        sys = mdlOutputs(t,x,u,C);
    case 4,
        sys = mdlGetTimeOfNextVarHit(t,x,u);
    case 9,
        sys = mdlTerminate(t,x,u);
    otherwise
        DAStudio.error('Dimulink:Blocks:unhandledFalg', num2str(flag));
end

function [sys,x0,str,ts,simStateCompliance]=mdlInitializeSizes
sizes = simsizes;
sizes.NumContStates = 0;
sizes.NumDiscStates = 2;
sizes.NumOutputs    = 1;
sizes.NumInputs     = 2;
sizes.DirFeedthrough = 0;
sizes.NumSampleTimes = 1;
sys = simsizes(sizes);

x0 = [0,0]';
str = [];
ts = [0,0];
simStateCompliance = 'UnknownSimState';
function sys = mdlDerivatives(t,x,u)

sys = [];
function sys = mdlUpdate(t,x,u,A,B)
%update state variable
sys = A * x + B  *u;

function sys = mdlOutputs(t,x,u,C)
%Update output
sys = C * x;

function sys = mdlGetTimeOfNextVarHit(t,x,u)
%this function is not necessary
sampleTime = 1;
sys = t + sampleTime;
function sys = mdlTerminate(t,x,u)
sys = [];


二:搭建仿真模型,配置参数

matlab <wbr>s-function <wbr>测试例子

三:输入仿真参数

>> A=[-0.5572 -0.7814;0.7814 0];
>> B=[1 -1;0 2];
>> C=[1.9691 6.4493];


四:运行仿真,查看波形


matlab <wbr>s-function <wbr>测试例子

你可能感兴趣的:(matlab s-function 测试例子)