Question

I am trying to get an output from my function but the first iteration is being skipped. I keep getting a ZETA = 1.000 instead of ZETA = -18.7026 Please help!

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
Was it helpful?

Solution

You forgot to include indexes in the else clause. Try this instead:

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

OTHER TIPS

This might be a better version in terms of readability and mathematical rigorousness:

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
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top