|
发表于 2017-2-28 09:44:23
|
显示全部楼层
function [sys,x0,str,ts] = fcn(t,x,u,flag)
%DSFUNC An example MATLAB file S-function for defining a discrete system.
% Example MATLAB file S-function implementing discrete equations:
% x(n+1) = Ax(n) + Bu(n)
% y(n) = Cx(n) + Du(n)
%
% See sfuntmpl.m for a general S-function template.
%
% See also SFUNTMPL.
% Copyright 1990-2009 The MathWorks, Inc.
% Generate a discrete linear system:
A=[-0.09 -0.01
1.0000 0];
B=[1 -7
0 -2];
C=[ 0 2
1 -5];
D=[ -3 0
1 0];
switch flag,
%%%%%%%%%%%%%%%%%%
% Initialization %
%%%%%%%%%%%%%%%%%%
case 0,
[sys,x0,str,ts] = mdlInitializeSizes(A,B,C,D);
%%%%%%%%%%
% Update %
%%%%%%%%%%
case 1,
sys = mdlDerivatives(t,x,u,A,B,C,D);
%%%%%%%%%%
% Output %
%%%%%%%%%%
case 3,
sys = mdlOutputs(t,x,u,A,B,C,D);
%%%%%%%%%%%%%
% Terminate %
%%%%%%%%%%%%%
case {2,4,9}
sys = []; % do nothing
%%%%%%%%%%%%%%%%%%%%
% Unexpected flags %
%%%%%%%%%%%%%%%%%%%%
otherwise
DAStudio.error('Simulink:blocks:unhandledFlag', num2str(flag));
end
%end dsfunc
%
%=======================================================================
% mdlInitializeSizes
% Return the sizes, initial conditions, and sample times for the S-function.
%=======================================================================
%
function [sys,x0,str,ts] = mdlInitializeSizes(A,~,~,D)
sizes = simsizes;
sizes.NumContStates = 2;
sizes.NumDiscStates = 0;
sizes.NumOutputs = 2;
sizes.NumInputs = 2;
sizes.DirFeedthrough = 1;
sizes.NumSampleTimes = 1;
sys = simsizes(sizes);
x0 = zeros(2,1);
str = [];
ts = [0 0];
% end mdlInitializeSizes
%
%=======================================================================
% mdlUpdate
% Handle discrete state updates, sample time hits, and major time step
% requirements.
%=======================================================================
%
function sys = mdlDerivatives(~,x,u,A,B,~,~)
sys = A*x+B*u;
%end mdlUpdate
%
%=======================================================================
% mdlOutputs
% Return the output vector for the S-function
%=======================================================================
%
function sys = mdlOutputs(~,x,u,~,~,C,D)
sys |
|