|

楼主 |
发表于 2005-7-28 17:41
|
显示全部楼层
之三、有关离散系统 <BR><BR>关于离散系统定义的书写在此举一例: <BR><BR>function [sys,x0,str,ts] = dsfunc(t,x,u,flag) <BR>%DSFUNC An example M-file S-function for defining a discrete system. <BR>% Example M-file S-function implementing discrete equations: <BR>% x(n+1) = Ax(n) + Bu(n) <BR>% y(n) = Cx(n) + Du(n) <BR>% <BR>% See sfuntmpl.m for a general S-function template. <BR>% <BR>% See also SFUNTMPL. % Copyright (c) 1990-1998 by The MathWorks, Inc. All Rights Reserved. <BR>% Revision:1.13 <BR>% Generate a discrete linear system: <BR>A=[-1.3839 -0.5097 1.0000 0]; B=[-2.5559 0 0 4.2382]; <BR>C=[ 0 2.0761 0 7.7891]; D=[ -0.8141 -2.9334 1.2426 0]; <BR>switch flag, <BR>% Initialization <BR>case 0,[sys,x0,str,ts] = mdlInitializeSizes(A,B,C,D); <BR>% Update % %%%%%%%%%% <BR>case 2, sys = mdlUpdate(t,x,u,A,B,C,D); %%%%%%%%%% <BR>% Output % %%%%%%%%%% <BR>case 3, sys = mdlOutputs(t,x,u,A,C,D); %%%%%%%%%%%%% <BR>% Terminate % %%%%%%%%%%%%% <BR>case 9, sys = []; % do nothing %%%%%%%%%%%%%%%%%%%% <BR>% Unexpected flags % %%%%%%%%%%%%%%%%%%%% <BR>otherwise, error(['unhandled flag = ',num2str(flag)]); <BR>end %end dsfunc <BR>% <BR>%======================================================================= <BR>% mdlInitializeSizes <BR>% Return the sizes, initial conditions, and sample times for the S-function. <BR>%======================================================================= <BR>% <BR>function [sys,x0,str,ts] = mdlInitializeSizes(A,B,C,D) <BR>sizes = simsizes; <BR>sizes.NumContStates = 0; sizes.NumDiscStates = size(A,1); sizes.NumOutputs = size(D,1); <BR>sizes.NumInputs = size(D,2); sizes.DirFeedthrough = 1; sizes.NumSampleTimes = 1; <BR>sys = simsizes(sizes); x0 = ones(sizes.NumDiscStates,1); str = []; ts = [1 0]; <BR>% end mdlInitializeSizes <BR>% <BR>%======================================================================= <BR>% mdlUpdate <BR>% Handle discrete state updates, sample time hits, and major time step <BR>% requirements. <BR>%======================================================================= <BR>% function sys = mdlUpdate(t,x,u,A,B,C,D) <BR>sys = A*x+B*u; %end mdlUpdate <BR>% <BR>%======================================================================= <BR>% mdlOutputs <BR>% Return Return the output vector for the S-function <BR>%======================================================================= <BR>% <BR>function sys = mdlOutputs(t,x,u,A,C,D) <BR>sys = C*x+D*u; %end mdlUpdate <BR><BR>S-FUNCTIONS的书写之四(离散和连续的混合型) <BR><BR> <BR><BR>function [sys,x0,str,ts] = mixedm(t,x,u,flag) <BR>%MIXEDM An example integrator followed by unit delay M-file S-function <BR>% Example M-file S-function implementing a hybrid system consisting <BR>% of a continuous integrator (1/s) in series with a unit delay (1/z). <BR>% Sampling period and offset for unit delay. <BR>dperiod = 1; doffset = 0; <BR>switch flag <BR>%%%%%%%%%%%%%%%%%% <BR>% Initialization % <BR>%%%%%%%%%%%%%%%%%% <BR>case 0, [sys,x0,str,ts]=mdlInitializeSizes(dperiod,doffset); <BR>%%%%%%%%%%%%%%% <BR>% Derivatives % <BR>%%%%%%%%%%%%%%% <BR>case 1, sys=mdlDerivatives(t,x,u); <BR>%%%%%%%%%% <BR>% Update % <BR>%%%%%%%%%% <BR>case 2, sys=mdlUpdate(t,x,u,dperiod,doffset); <BR>%%%%%%%%%% <BR>% Output % <BR>%%%%%%%%%% <BR>case 3, sys=mdlOutputs(t,x,u,doffset,dperiod); <BR>%%%%%%%%%%%%% <BR>% Terminate % <BR>%%%%%%%%%%%%% <BR>case 9, sys = []; <BR>% do nothing <BR>otherwise, error(['unhandled flag = ',num2str(flag)]); <BR>end % end mixedm % <BR>%============================================================================= <BR>% mdlInitializeSizes <BR>% Return the sizes, initial conditions, and sample times for the S-function. <BR>%============================================================================= <BR>% <BR>function [sys,x0,str,ts]=mdlInitializeSizes(dperiod,doffset) <BR>sizes = simsizes; sizes.NumContStates = 1; sizes.NumDiscStates = 1; <BR>sizes.NumOutputs = 1; sizes.NumInputs = 1; sizes.DirFeedthrough = 0; <BR>sizes.NumSampleTimes = 2; sys = simsizes(sizes); x0 = ones(2,1); str = []; <BR>ts = [0 0; % sample time <BR> dperiod doffset]; <BR>% end mdlInitializeSizes <BR>% <BR>%============================================================================= <BR>% mdlDerivatives % Compute derivatives for continuous states. <BR>%============================================================================= <BR>% <BR>function sys=mdlDerivatives(t,x,u) <BR>sys = u; % end mdlDerivatives <BR>% <BR>%============================================================================= <BR>% mdlUpdate <BR>% Handle discrete state updates, sample time hits, and major time step % requirements. <BR>%============================================================================= <BR>% <BR>function sys=mdlUpdate(t,x,u,dperiod,doffset) <BR>% next discrete state is output of the integrator <BR>if abs(round((t - doffset)/dperiod) - (t - doffset)/dperiod) < 1e-8, sys = x(1); <BR>else sys = []; end % end mdlUpdate <BR>% <BR>%============================================================================= <BR>% mdlOutputs % Return the output vector for the S-function <BR>%============================================================================= <BR>% <BR>function sys=mdlOutputs(t,x,u,doffset,dperiod) <BR>% Return output of the unit delay if we have a <BR>% sample hit within a tolerance of 1e-8. If we <BR>% don't have a sample hit then return [] indicating <BR>% that the output shouldn't change. <BR>if abs(round((t - doffset)/dperiod) - (t - doffset)/dperiod) < 1e-8, sys = x(2); else sys = []; end % end mdlOutputs 关于这些程序的说明,我将在后面讲解。 <BR><BR>S-Function书写之五 <BR><BR>在本帖中我对前面列举的关于系统是连续,离散,连续与离散混合的三个例子加以说明,以至于大家在看下面的例子时能更好的理解。 <BR><BR>一.函数mdlInitializeSizes <BR><BR>对于描述连续型的函数csfunc.m <BR><BR>sizes.NumContStates = 2; sizes.NumDiscStates = 0; <BR><BR>表明本函数是描述连续型的<BR><BR>sizes.NumOutputs=2;sizes.NumInputs=2;<BR>sizes.DirFeedthrough=1;sizes.NumSampleTimes=1;<BR><BR>对于描述离散型的函数dsfunc.m<BR><BR>sizes.NumContStates=0;sizes.NumDiscStates=size(A,1);<BR><BR>表明本函数是描述离散型的 <BR><BR>sizes.NumOutputs = size(D,1); sizes.NumInputs = size(D,2); <BR>sizes.DirFeedthrough = 1; sizes.NumSampleTimes = 1; <BR><BR>对于描述混合型的函数mixedm.m <BR><BR>sizes.NumContStates = 1; sizes.NumDiscStates = 1; <BR><BR>$表明本函数是描述混合型的 <BR><BR>sizes.NumOutputs = 1; sizes.NumInputs = 1; <BR>sizes.DirFeedthrough = 0; sizes.NumSampleTimes = 2; <BR><BR>关于变量sizes.numoutputs和sizes.numinputs则由描述的系统的输出,输入而定。 <BR>关于变量sizes.DirFeedthrough,则可以查看矩阵D,如果D是非空,则为1,否则为0 <BR>关于变量sizes.NumSampleTimes,即是ts这个矩阵的行数。 <BR><BR>二.函数mdlDerivatives <BR><BR>仅仅出现在含有连续型的系统中;这部分应该根据系统状态来决定。 <BR><BR>三.函数mdlUpdate 仅仅出现在含有离散型的系统中;该函数应该根据系统状态来决定 <BR><BR>四.函数mdlOutputs 该函数在各种类型中都有,应该根据描述系统的输出来决定; <BR><BR>五.关于书写s-functions函数的注意之点 <BR><BR>(1)我想大家都会觉得上述三个函数都不利于维护,因此在写s-functions函数时不妨把矩阵 A,B,C,D也考虑成变量,作为输入参数; <BR>(2)关于在csfuncs.m中的型如case(2,4,9)这种形式,大家最好不要采用,不利于维护,可以分 开来写 <BR>(3)关于结构sizes中的各个变量的值,最好从输入参量中得到,而不是通过人为判断来输入 <BR>(4)因此,其实上述三种形式,可以变化成一种形式. <BR>(5)可以通过对相应于你所要求的系统对上述三个函数加以相应的改进来到达自己的要求,因此可以把上述函数当成模板函数. <BR> |
|