1

我正在尝试从我的函数中获取输出,但正在跳过第一次迭代。我不断得到 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
4

2 回答 2

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
于 2013-09-23T13:56:45.653 回答
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
于 2013-09-23T14:52:33.877 回答