我的第一次迭代被跳过 - MATLAB

时间:2013-09-23 13:48:25

标签: matlab controls evolutionary-algorithm

我试图从我的函数中获取输出,但是第一次迭代被跳过了。 我一直得到ZETA = 1.000而不是ZETA = -18.7026 请帮忙!

EIG = [
 -18.7026;          
  -4.6179 + 7.4827j;
  -4.6179 - 7.4827j;
  -1.1268 + 4.3335j;
  -1.1268 - 4.3335j;
  -0.3372 ]

for i = 1:6;

    SIG(i) = real(EIG(i));
    OMG(i) = imag(EIG(i));

    if  OMG(i) == 0;
        ZETA(i) = SIG(i);   
    else
        ZETA = -SIG ./(sqrt(SIG.^2 + OMG.^2));  
    end

end

2 个答案:

答案 0 :(得分:2)

您忘记在else子句中包含索引。试试这个:

EIG = [
 -18.7026;          
  -4.6179 + 7.4827j;
  -4.6179 - 7.4827j;
  -1.1268 + 4.3335j;
  -1.1268 - 4.3335j;
  -0.3372 ]

for i = 1:6;

    SIG(i) = real(EIG(i));
    OMG(i) = imag(EIG(i));

    if  OMG(i) == 0;
        ZETA(i) = SIG(i);   
    else
        ZETA(i) = -SIG(i) ./(sqrt(SIG(i).^2 + OMG(i).^2));  
    end

end

答案 1 :(得分:1)

在可读性和数学严谨性方面,这可能是一个更好的版本:

EIG = [...
  -18.7026;
  -4.6179 + 7.4827j;
  -4.6179 - 7.4827j;
  -1.1268 + 4.3335j;
  -1.1268 - 4.3335j;
  -0.3372...
 ];

ZETA = nan(size(EIG(:),1),1);
for i = 1:size(EIG(:),1)
   if ~imag(EIG(i))
      ZETA(i) = real(EIG(i));   
   else
      ZETA(i) = -real(EIG(i)) /abs(EIG(i));  
   end
end