📄 switchuk.m
字号:
%参数e0和de0是设定的相平面误差允许范围。
function [sys,x0,str,ts] = switchUK(t,x,u,flag,K1,K3,tempi)
switch flag,
%%%%%%%%%%%%%%%%%%
% Initialization %
%%%%%%%%%%%%%%%%%%
% Initialize the states, sample times, and state ordering strings.
case 0
[sys,x0,str,ts]=mdlInitializeSizes;
%%%%%%%%%%%
% Outputs %
%%%%%%%%%%%
% Return the outputs of the S-function block.
case 3
sys=mdlOutputs(t,x,u,K1,K3,tempi);
%%%%%%%%%%%%%%%%%%%
% Unhandled flags %
%%%%%%%%%%%%%%%%%%%
% There are no termination tasks (flag=9) to be handled.
% Also, there are no continuous or discrete states,
% so flags 1,2, and 4 are not used, so return an emptyu
% matrix
case { 1, 2, 4, 9 }
sys=[];
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Unexpected flags (error handling)%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Return an error message for unhandled flag values.
otherwise
error(['Unhandled flag = ',num2str(flag)]);
end
% end timestwo
%
%=============================================================================
% mdlInitializeSizes
% Return the sizes, initial conditions, and sample times for the S-function.
%=============================================================================
%
function [sys,x0,str,ts] = mdlInitializeSizes()
sizes = simsizes;
sizes.NumContStates = 0;
sizes.NumDiscStates = 0;
sizes.NumOutputs = 1; % dynamically sized
sizes.NumInputs = 2; % dynamically sized
sizes.DirFeedthrough = 1; % has direct feedthrough
sizes.NumSampleTimes = 1;
sys = simsizes(sizes);
str = [];
x0 = [];
ts = [-1 0]; % inherited sample time
% end mdlInitializeSizes
%
%=============================================================================
% mdlOutputs
% Return the output vector for the S-function
%=============================================================================
%u(1,1)为输入控制量K(t)
%u(2,1)为输入控制量U(t)
function sys = mdlOutputs(t,x,u,K1,K3,tempi)
if abs(u(1,1))>K3
sys=u(1,1);%如果输入控制量的绝对值比较大,即误差太大。则切换到K(t)逻辑控制
tempi=1;
elseif abs(u(1,1))<K1
sys=u(2,1);%如果输入控制量的绝对值比较小,则切换到U(t)预测控制
tempi=0;
else
if tempi==0 %如果已经是预测控制,则保持,此处保持预测控制优先
sys=u(2,1);
else
sys=u(1,1);%如果已经是逻辑控制,则保持
end
end
% end mdlOutputs
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -