|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有账号?我要加入
x
跟这段程序较劲好几天了,还是没有解决,哪位帮看看,指点一下迷津,感激涕零
switch flag,
case 0,
[sys,x0,str,ts]=mdlInitializeSizes(u,lamata,theta0); %
case 2,
sys=mdlUpdate(x,u,lamata);
case 3,
sys=mdlOutputs(x);
case 4,
sys=mdlGetTimeOfNextVarHit(t,x,u);
case {1,4,9}
sys=[];
otherwise
error(['Unhandled flag = ',num2str(flag)]);
end
function [sys,x0,str,ts]=mdlInitializeSizes(u,lamata,theta0)
sizes = simsizes;
sizes.NumContStates = 0;
sizes.NumDiscStates = 36;
sizes.NumOutputs = 1;
sizes.NumInputs = 3;
sizes.DirFeedthrough = 0;
sizes.NumSampleTimes = 1; % at least one sample time is needed
sys = simsizes(sizes);
theta = theta0;
P = eye(4);
H = [0 0 1 0]';
K = P*H*(H'*P*H+lamata);
U = [1 0 0 0]';
Y = [0 0 0 0]';
E = [1 0 0 0]';
x0 = [U;Y;E;P(:,1);P(:,2);P(:,3);P(:,4);K;theta];
ts = [0.1 0];
function sys=mdlUpdate(x,u,lamata)
U = x(1:4);
Y = x(5:8);
E = x(9:12);
i = 4;
while i>1
U(i) = U(i-1);%U(3) = U(2);U(2) = U(1);
Y(i) = Y(i-1);%Y(3) = Y(2);Y(2) = Y(1);
E(i) = E(i-1);%e(k-1)e(k-2)...e(k-4)
i = i-1;
end
U(1) = u(1);
Y(1) = u(2);
E(1) = u(3)-u(2);
H = [-Y(1:2);U(2:3)];
P = [x(13:16),x(17:20),x(21:24),x(25:28)];
K = x(29:32);
theta = x(33:36);
if norm(K)<0.0001
sys = x;
return
else
P = (1-K*H')*P/lamata;
theta = theta + K*(Y(1)-H'*theta);
K = P*H*(H'*P*H+lamata);
sys = [U;Y;E;P(:,1);P(:,2);P(:,3);P(:,4);K;theta];
%sys = [U Y E P K theta];
end
function sys = mdlOutputs(x)
theta = x(33:36);
U = x(1:4);
E = x(9:12);
sys = pid(U,E,theta);
function sys=mdlGetTimeOfNextVarHit(t,x,u)
sampleTime = 1; % Example, set the next hit to be one second later.
sys = t + sampleTime;
function sys=mdlTerminate(t,x,u)
sys = [];
function y = pid(u,e,theta)
y = (e(1:3)'*[1;theta(1);theta(2)])*0.194/theta(3)+u(1)+theta(4)/theta(3)*(u(2)-u(1));
上面的程序总是报错 Output returned by S-function 'mys_fcn' in 'untitled1/Subsystem/S-Function' during flag=3 call must be a real vector of length 1,但是照理说这么算出来的输出应该为实数,不可能出现复数的啊,哪位能帮忙看一下,不胜感激!! |
|