|
楼主 |
发表于 2010-3-24 10:18
|
显示全部楼层
代码
function [sys,x0,str,ts] = han_dcdc(t,x,u,flag)
switch flag,
%%%%%%%%%%
% 初始化 %
%%%%%%%%%%
case 0,
[sys,x0,str,ts]=mdlInitializeSizes;
%%%%%%%%%%%%%%%
% 离散状态更新 %
%%%%%%%%%%%%%%%
case 1,
sys=mdlDerivatives(t,x,u);
%%%%%%%%%%%%%
% 输出量计算 %
%%%%%%%%%%%&&
case 3,
sys=mdlOutputs(x);
%%%%%%%%%%
% 未使用 %
%%%%%%%%%%
case {2,4,9},
sys=[];
%%%%%%%%%%%%
% 处理错误 %
%%%%%%%%%%%%
otherwise
error(['Unhandled flag = ',num2str(flag)]);
end
% end sfuntmpl
%
%=============================================================================
% mdlInitializeSizes
% 整个系统初始化
%=============================================================================
%
function [sys,x0,str,ts]=mdlInitializeSizes(t,x,u);
%
% call simsizes for a sizes structure, fill it in and convert it to a
% sizes array.
%
% Note that in this example, the values are hard coded. This is not a
% recommended practice as the characteristics of the block are typically
% defined by the S-function parameters.
%
sizes = simsizes;
sizes.NumContStates = 0;
sizes.NumDiscStates = 2;
sizes.NumOutputs = 2;
sizes.NumInputs = 1;
sizes.DirFeedthrough = 1;
sizes.NumSampleTimes = 1; % at least one sample time is needed
sys = simsizes(sizes);
%
% initialize the initial conditions
%
x0 = [0;0];
%
% str is always an empty matrix
%
str = [];
%
% initialize the array of sample times
%
ts = [0 0];
% end mdlInitializeSizes
%
%=============================================================================
% mdlUpdate
% 更新离散系统状态变量
%=============================================================================
%
function sys=mdlDerivatives(t,x,u);
if u==1
sys(1,1) = 10/2.7e-3;
sys(2,1) = -1*x(2)/(10*270e-6);
else if u==0
sys(1,1) = -1*x(2)/2.7e-3;
sys(2,1) = 1*x(1)/270e-6-1*x(2)/(10*270e-6);
else if 0 < u <1
sys(1,1) = -1*x(2)/2.7e-3+u*(1*x(2)/2.7e-3+10/2.7e-3);
sys(2,1) = -1*x(1)/270e-6-1*x(2)/(10*270e-6)-u*1*x(1)/270e-6;
end
end
end
% end mdlUpdate
%
%=============================================================================
% mdlOutputs
% 计算系统输出变量:返回两个状态
%=============================================================================
%
function sys=mdlOutputs(x)
sys = x; |
|