声振论坛

 找回密码
 我要加入

QQ登录

只需一步,快速开始

查看: 1415|回复: 2

[综合讨论] s-函数出错求助

[复制链接]
发表于 2007-10-15 13:19 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?我要加入

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,但是照理说这么算出来的输出应该为实数,不可能出现复数的啊,哪位能帮忙看一下,不胜感激!!
回复
分享到:

使用道具 举报

发表于 2007-10-15 16:37 | 显示全部楼层

回复 #1 ly012659 的帖子

不要照理说是什么样子,出错了自然不会按照常规了。根据提示看看出现的到底是什么数,并查找出现的原因
发表于 2007-10-21 14:43 | 显示全部楼层
sizes.DirFeedthrough = 0;改成1试试
您需要登录后才可以回帖 登录 | 我要加入

本版积分规则

QQ|小黑屋|Archiver|手机版|联系我们|声振论坛

GMT+8, 2024-12-25 22:01 , Processed in 0.066487 second(s), 17 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表