cruz007 发表于 2008-3-25 20:56

电机转速比较大时,s函数运行出错

function=motor(t,x,u,flag,Pp,Pc,rps,rcs,rpr,rcr,lps,lcs,lpr,lcr,lpm,lcm,J)
Pp=8;Pc=2;rps=0.435;rcs=0.435;rpr=0.816;rcr=0.816;lps=0.07131;lcs=0.07131;lpr=0.07131;lcr=0.07131;lpm=0.06931;lcm=0.06931;J=0.089;
switch flag,
case 0,
    =mdlInitializeSizes(Pp,Pc,rps,rcs,rpr,rcr,lps,lcs,lpr,lcr,lpm,lcm,J);
case 1,
    sys=mdlDerivatives(t,x,u,Pp,Pc,rps,rcs,rpr,rcr,lps,lcs,lpr,lcr,lpm,lcm,J);
case 3,
    sys=mdlOutputs(t,x,u,Pp,Pc,rps,rcs,rpr,rcr,lps,lcs,lpr,lcr,lpm,lcm,J);
case {2,4,9},
    sys = [];
otherwise
    error(['Unhandled flag = ',num2str(flag)]);
end

function =mdlInitializeSizes(Pp,Pc,rps,rcs,rpr,rcr,lps,lcs,lpr,lcr,lpm,lcm,J);
sizes = simsizes;
sizes.NumContStates= 6;
sizes.NumDiscStates= 0;
sizes.NumOutputs   = 7;
sizes.NumInputs      = 5;
sizes.DirFeedthrough = 1;
sizes.NumSampleTimes = 1;
sys = simsizes(sizes);
x0 = ;
str = [];
ts = ;

function sys=mdlDerivatives(t,x,u,Pp,Pc,rps,rcs,rpr,rcr,lps,lcs,lpr,lcr,lpm,lcm,J);
Kd1=[lps,0,0,0,lpm,0;
         0,lps,0,0,0,lpm;
         0,0,lcs,0,-lcm,0;
         0,0,0,lcs,0,lcm;
         lpm,0,-lcm,0,lpr+lcr,0;
         0,lpm,0,lcm,0,(lpr+lcr)];
Kx1=[-rps,u(5)*Pp*lps,0,0,0,-u(5)*Pp*lpm;
            u(5)*Pp*lps,-rps,0,0,u(5)*Pp*lpm,0;
            0,0,-rcs,-u(5)*Pc*lcs,0,-u(5)*Pc*lcm;
            0,0,u(5)*Pc*lcs,-rcs,-u(5)*Pc*lcm,0;
            0,0,0,0,-(rpr+rcr),0;
            0,0,0,0,0,-(rpr+rcr)];
Ku1=[1,0,0,0;
          0,1,0,0;
          0,0,1,0;
          0,0,0,1;
          0,0,0,0;
          0,0,0,0];
A=inv(Kd1)*Kx1;
B=inv(Kd1)*Ku1;
sys=A*x(1:6)+B*u(1:4);

function sys=mdlOutputs(t,x,u,Pp,Pc,rps,rcs,rpr,rcr,lps,lcs,lpr,lcr,lpm,lcm,J);
x(7)=0.5*Pp*lpm*+0.5*Pc*lcm*;
sys=x;

我试了下,当u(5)给定为1时,运行时不会出错;但是修改的比较大时,比如20,运行出错。错误提示:State derivatives returned by S-function 'cdfm_three' in 'cdfm/Cdfm/S-Function' during flag=1 call must be a real vector of length 6.
可能是那边求逆矩阵时出错的,当把逆矩阵去掉就没有问题了,看了其他的帖子,到现在也不清楚该怎么修改才能解决问题,请各位大侠抽空帮忙。不胜感激!

cruz007 发表于 2008-3-25 22:19

各位大哥帮忙看看阿
页: [1]
查看完整版本: 电机转速比较大时,s函数运行出错