在下面我将写几个具体的实例。
之二、连续系统例子:
function [sys,x0,str,ts] = csfunc(t,x,u,flag)
%CSFUNC An example M-file S-function for defining a continuous system.
% Example M-file S-function implementing continuous equations:
% x' = Ax + Bu
% y = Cx + Du
%
% See sfuntmpl.m for a general S-function template.
%
% See also SFUNTMPL.
% Copyright (c) 1990-1998 by The MathWorks, Inc. All Rights Reserved.
% Revision:1.5
A=[-0.09 -0.01; 1 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);
%%%%%%%%%%%%%%%
% Derivatives %
%%%%%%%%%%%%%%%
case 1,sys=mdlDerivatives(t,x,u,A,B,C,D);
%%%%%%%%%%%
% Outputs %
%%%%%%%%%%%
case 3,sys=mdlOutputs(t,x,u,A,B,C,D);
%%%%%%%%%%%%%%%%%%%
% Unhandled flags %
%%%%%%%%%%%%%%%%%%%
case { 2, 4, 9 },sys = [];
%%%%%%%%%%%%%%%%%%%%
% Unexpected flags %
%%%%%%%%%%%%%%%%%%%%
otherwise, error(['Unhandled flag = ',num2str(flag)]);
end
% end csfunc
%
%=============================================================================
% mdlInitializeSizes
% Return the sizes, initial conditions, and sample times for the S-function.
%=============================================================================
%
function [sys,x0,str,ts]=mdlInitializeSizes(A,B,C,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
%
%=============================================================================
% mdlDerivatives
% Return the derivatives for the continuous states.
%=============================================================================
%
function sys=mdlDerivatives(t,x,u,A,B,C,D)
sys = A*x + B*u; % end mdlDerivatives
%
%=============================================================================
% mdlOutputs
% Return the block outputs.
%=============================================================================
%
function sys=mdlOutputs(t,x,u,A,B,C,D)
sys = C*x + D*u; % end mdlOutputs
之三、有关离散系统
关于离散系统定义的书写在此举一例:
function [sys,x0,str,ts] = dsfunc(t,x,u,flag)
%DSFUNC An example M-file S-function for defining a discrete system.
% Example M-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 (c) 1990-1998 by The MathWorks, Inc. All Rights Reserved.
% Revision:1.13
% Generate a discrete linear system:
A=[-1.3839 -0.5097 1.0000 0]; B=[-2.5559 0 0 4.2382];
C=[ 0 2.0761 0 7.7891]; D=[ -0.8141 -2.9334 1.2426 0];
switch flag,
% Initialization
case 0,[sys,x0,str,ts] = mdlInitializeSizes(A,B,C,D);
% Update % %%%%%%%%%%
case 2, sys = mdlUpdate(t,x,u,A,B,C,D); %%%%%%%%%%
% Output % %%%%%%%%%%
case 3, sys = mdlOutputs(t,x,u,A,C,D); %%%%%%%%%%%%%
% Terminate % %%%%%%%%%%%%%
case 9, sys = []; % do nothing %%%%%%%%%%%%%%%%%%%%
% Unexpected flags % %%%%%%%%%%%%%%%%%%%%
otherwise, error(['unhandled flag = ',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,B,C,D)
sizes = simsizes;
sizes.NumContStates = 0; sizes.NumDiscStates = size(A,1); sizes.NumOutputs = size(D,1);
sizes.NumInputs = size(D,2); sizes.DirFeedthrough = 1; sizes.NumSampleTimes = 1;
sys = simsizes(sizes); x0 = ones(sizes.NumDiscStates,1); str = []; ts = [1 0];
% end mdlInitializeSizes
%
%=======================================================================
% mdlUpdate
% Handle discrete state updates, sample time hits, and major time step
% requirements.
%=======================================================================
% function sys = mdlUpdate(t,x,u,A,B,C,D)
sys = A*x+B*u; %end mdlUpdate
%
%=======================================================================
% mdlOutputs
% Return Return the output vector for the S-function
%=======================================================================
%
function sys = mdlOutputs(t,x,u,A,C,D)
sys = C*x+D*u; %end mdlUpdate
S-FUNCTIONS的书写之四(离散和连续的混合型)
function [sys,x0,str,ts] = mixedm(t,x,u,flag)
%MIXEDM An example integrator followed by unit delay M-file S-function
% Example M-file S-function implementing a hybrid system consisting
% of a continuous integrator (1/s) in series with a unit delay (1/z).
% Sampling period and offset for unit delay.
dperiod = 1; doffset = 0;
switch flag
%%%%%%%%%%%%%%%%%%
% Initialization %
%%%%%%%%%%%%%%%%%%
case 0, [sys,x0,str,ts]=mdlInitializeSizes(dperiod,doffset);
%%%%%%%%%%%%%%%
% Derivatives %
%%%%%%%%%%%%%%%
case 1, sys=mdlDerivatives(t,x,u);
%%%%%%%%%%
% Update %
%%%%%%%%%%
case 2, sys=mdlUpdate(t,x,u,dperiod,doffset);
%%%%%%%%%%
% Output %
%%%%%%%%%%
case 3, sys=mdlOutputs(t,x,u,doffset,dperiod);
%%%%%%%%%%%%%
% Terminate %
%%%%%%%%%%%%%
case 9, sys = [];
% do nothing
otherwise, error(['unhandled flag = ',num2str(flag)]);
end % end mixedm %
%=============================================================================
% mdlInitializeSizes
% Return the sizes, initial conditions, and sample times for the S-function.
%=============================================================================
%
function [sys,x0,str,ts]=mdlInitializeSizes(dperiod,doffset)
sizes = simsizes; sizes.NumContStates = 1; sizes.NumDiscStates = 1;
sizes.NumOutputs = 1; sizes.NumInputs = 1; sizes.DirFeedthrough = 0;
sizes.NumSampleTimes = 2; sys = simsizes(sizes); x0 = ones(2,1); str = [];
ts = [0 0; % sample time
dperiod doffset];
% end mdlInitializeSizes
%
%=============================================================================
% mdlDerivatives % Compute derivatives for continuous states.
%=============================================================================
%
function sys=mdlDerivatives(t,x,u)
sys = u; % end mdlDerivatives
%
%=============================================================================
% mdlUpdate
% Handle discrete state updates, sample time hits, and major time step % requirements.
%=============================================================================
%
function sys=mdlUpdate(t,x,u,dperiod,doffset)
% next discrete state is output of the integrator
if abs(round((t - doffset)/dperiod) - (t - doffset)/dperiod) < 1e-8, sys = x(1);
else sys = []; end % end mdlUpdate
%
%=============================================================================
% mdlOutputs % Return the output vector for the S-function
%=============================================================================
%
function sys=mdlOutputs(t,x,u,doffset,dperiod)
% Return output of the unit delay if we have a
% sample hit within a tolerance of 1e-8. If we
% don't have a sample hit then return [] indicating
% that the output shouldn't change.
if abs(round((t - doffset)/dperiod) - (t - doffset)/dperiod) < 1e-8, sys = x(2); else sys = []; end % end mdlOutputs
关于这些程序的说明,我将在后面讲解。 |