Search code examples
matlabcontrolsevolutionary-algorithm

My first iteration is being skipped - MATLAB


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

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