0

I write an algorithm that checks the stability of the closed system of the Nyquist criterion (http://en.wikipedia.org/wiki/Nyquist_stability_criterion)

    function answear=stability(re,im)
%% Function check stability of system
%re is real part of transmitation
%im is imagine part of transmitation

%% Check number of vectors elements
re(end +1:5) = 0;
im(end +1:5) = 0;

if( length(re) > length(im))
    root = length(re);
else
    root = length(im);
end

for w=1:root
    tran(w) = re(1) + re(2)*w.^1 + re(3)*w.^2 + re(4)*w.^3 + re(5)*w.^4 +1i*(...
     im(1) + im(2)*w.^1 + im(3)*w.^2 + im(4)*w.^3 +im(5)*w.^4);
end

%% Algorithm
switch root
    case 0
        exist('Write nonzero numbers', 'var')
    case 1
    for w=1:length(w)
        if( real(tran(w)) > 0 && imag(tran(w)) > 0) 
            answear=1;
        else
            answear=0;
        end
    end
    case 2
    for w=1:length(w)
        if( real(tran(w)) > 0 && imag(tran(w)) > 0) 
            if( real(tran(w)) < 0 && imag(tran(w)) > 0) 
                answear=1;
            else
                answear=0;
            end
        end
        end
    case 3
    for w=1:length(w)
        if( real(tran(w)) > 0 && imag(tran(w)) > 0) 
            if( real(tran(w)) < 0 && imag(tran(w)) > 0) 
                if( real(tran(w)) < 0 && imag(tran(w)) < 0) 
                    answear=1;
                else
                    answear=0;
                end
            end
        end
    end
    case 4
    for w=1:length(w)
         if( real(tran(w)) > 0 && imag(tran(w)) > 0) 
            if( real(tran(w)) < 0 && imag(tran(w)) > 0) 
                if( real(tran(w)) < 0 && imag(tran(w)) < 0)
                    if( real(tran(w)) > 0 && imag(tran(w)) < 0) 
                        answear=1;
                    else
                        answear=0;
                    end
                end
            end
         end
    end
end

%% Answear
if answear==1
    disp('System unstable')
else
    disp('System stable')
end
plot(real(tran),imag(tran)) 
grid on

end

Function returns

Undefined function or variable "answear".

Error in stability (line 87) if answear==1

So the algorithm is badly written?

Lundin
  • 195,001
  • 40
  • 254
  • 396
Kulis
  • 988
  • 3
  • 11
  • 25

1 Answers1

2

Your code could use a lot of cleanup:

  1. Instead of an if-statement such as this one:

    if( real(tran(w)) > 0 && imag(tran(w)) > 0) 
        answear=1;
    else
        answear=0;
    end
    

    you could write a boolean assignment:

    answear = real(tran(w)) > 0 & imag(tran(w)) > 0);
    
  2. Why do you have three (almost) identical nested if-statements at all?

    if( real(tran(w)) > 0 && imag(tran(w)) > 0) 
        if( real(tran(w)) < 0 && imag(tran(w)) > 0) 
            if( real(tran(w)) < 0 && imag(tran(w)) < 0) 
    

    First of all, you could replace everything with one if-statement. But what are you actually testing with this? It seems that those nested if statements are never executed. For instance, real(tran(w)) cannot be both positive and negative at the same time (unless it is a vector you are working on, in which case you shouldn't be using the operator &&).

    Also, this is probably your code triggers the error regarding variable answear. Accessing it is impossible since it hasn't been assigned a value (none of the if-statements have been executed).

  3. What is tran, and what is w? Are they global variables? If they are, pass them as input parameters. Your function is probably poorly designed if it relies on external states and variables.

I haven't actually run your code, but these suggestions should make it easier for you to debug it.

P.S:
Please fix the annoying spelling error (it is "answer" and not "answear") :)

Eitan T
  • 32,660
  • 14
  • 72
  • 109
  • 1
    2.To check the stability of the Nyquist criterion - graph must go through as many quadrants of the coordinate system, which is a row transfer function (equation). For example, if the expression is a three-degree, graph passes successively through I, II, III quarter system. That's why I try to check in a loop each character real and imaginary parts of the expression. Tran is transmittance, w is pulsation(frequency). – Kulis Sep 23 '13 at 12:55
  • @Kulis Again, read my comment carefully: the expressions `tran(w) > 0` and `tran(w) < 0` cannot be true simultaneously. – Eitan T Sep 23 '13 at 13:01