Frequency Response Analysis and Design

## The Nyquist Diagram

The Nyquist plot allows us to predict the stability and performance of a closed-loop system by observing its open-loop behavior. The Nyquist criterion can be used for design purposes regardless of open-loop stability (remember that the Bode design methods assume that the system is stable in open loop). Therefore, we use this criterion to determine closed-loop stability when the Bode plots display confusing information. The following movie will help you visualize the relationship between the Bode plot and the Nyquist diagram.

#### Note: The MATLABnyquist command does not provide an adequate representation for systems that have open-loop poles in the jw-axis. Therefore, we suggest that you copy the nyquist1.m file as a new m-file. This m-file creates more accurate Nyquist plots, since it correctly deals with poles and zeros on the jw-axis.

The Nyquist diagram is basically a plot of G(j* w) where G(s) is the open-loop transfer function and w is a vector of frequencies which encloses the entire right-half plane. In drawing the Nyquist diagram, both positive and negative frequencies (from zero to infinity) are taken into account. We will represent positive frequencies in red and negative frequencies in green. The frequency vector used in plotting the Nyquist diagram usually looks like this (if you can imagine the plot stretching out to infinity):

In order to see how the frequency vector contributes to the Nyquist diagram more clearly, you can view our movie.

However, if we have open-loop poles or zeros on the jw axis, G(s) will not be defined at those points, and we must loop around them when we are plotting the contour. Such a contour would look as follows:

Please note that the contour loops around the pole on the jw axis. As we mentioned before, the MATLAB nyquist command does not take poles or zeros on the jw axis into account and therefore produces an incorrect plot. To correct this, please download and use nyquist1.m. If we have a pole on the jw axis, we have to use nyquist1. If there are no poles or zeros on the jw-axis, or if we have pole-zero cancellation, we can use either the nyquist command or nyquist1.m.

### The Cauchy criterion

The Cauchy criterion (from complex analysis) states that when taking a closed contour in the complex plane, and mapping it through a complex function G(s), the number of times that the plot of G(s) encircles the origin is equal to the number of zeros of G(s) enclosed by the frequency contour minus the number of poles of G(s) enclosed by the frequency contour. Encirclements of the origin are counted as positive if they are in the same direction as the original closed contour or negative if they are in the opposite direction.

When studying feedback controls, we are not as interested in G(s) as in the closed-loop transfer function:

```G(s)
---------
1 + G(s)
```
If 1+ G(s) encircles the origin, then G(s) will enclose the point -1. Since we are interested in the closed-loop stability, we want to know if there are any closed-loop poles (zeros of 1 + G(s)) in the right-half plane. More details on how to determine this will come later.

Therefore, the behavior of the Nyquist diagram around the -1 point in the real axis is very important; however, the axis on the standard nyquist diagram might make it hard to see what's happening around this point. To correct this, you can add the lnyquist.m function to your files. The lnyquist.m command plots the Nyquist diagram using a logarithmic scale and preserves the characteristics of the -1 point.

To view a simple Nyquist plot using MATLAB, we will define the following transfer function and view the Nyquist plot:

```0.5
-------
s - 0.5
```
```	sys=tf(0.5,[1 -0.5]);
nyquist(sys)
```

Now we will look at the Nyquist diagram for the following transfer function:
```   s + 2
-----
s^2
```
Note that this function has a pole at the origin. We will see the difference between using the nyquist, nyquist1, and lnyquist commands with this particular function.
```	num = [1 2];
den = [1 0 0];
sys = tf(num,den);

nyquist(sys)
```

```	nyquist1(sys)
```

```	lnyquist(sys)
```

Note that the nyquist plot is not the correct one, the nyquist1 plot is correct, but it's hard to see what happens close to the -1 point, and the lnyquist plot is correct and has an appropriate scale.

### Closed Loop Stability

Consider the negative feedback system:
Remember from the Cauchy criterion that the number N of times that the plot of G(s)H(s) encircles -1 is equal to the number Z of zeros of 1 + G(s)H(s) enclosed by the frequency contour minus the number P of poles of 1 + G(s)H(s) enclosed by the frequency contour (N = Z - P). Keeping careful track of open- and closed-loop transfer functions, as well as numerators and denominators, you should convince yourself that:

• the zeros of 1 + G(s)H(s) are the poles of the closed-loop transfer function
• the poles of 1 + G(s)H(s) are the poles of the open-loop transfer function.

The Nyquist criterion then states that:

• P = the number of open-loop (unstable) poles of G(s)H(s)
• N = the number of times the Nyquist diagram encircles -1
• clockwise encirclements of -1 count as positive encirclements
• counter-clockwise (or anti-clockwise) encirclements of -1 count as negative encirclements
• Z = the number of right half-plane (positive, real) poles of the closed-loop system
The important equation which relates these three quantities is:
```Z = P + N
```

Note: This is only one convention for the Nyquist criterion. Another convention states that a positive N counts the counter-clockwise or anti-clockwise encirclements of -1. The P and Z variab remain the same. In this case the equation becomes Z = P - N. Throughout these tutorials, we will use a positive sign for clockwise encirclements.

It is very important (and somewhat tricky) to learn how to count the number of times that the diagram encircles -1. Therefore, we will go into some detail to help you visualize this. You can view this movie as an example.

Another way of looking at it is to imagine you are standing on top of the -1 point and are following the diagram from beginning to end. Now ask yourself: How many times did I turn my head a full 360 degrees? Again, if the motion was clockwise, N is positive, and if the motion is anti-clockwise, N is negative.

Knowing the number of right-half plane (unstable) poles in open loop (P), and the number of encirclements of -1 made by the Nyquist diagram (N), we can determine the closed-loop stability of the system. If Z = P + N is a positive, nonzero number, the closed-loop system is unstable.

We can also use the Nyquist diagram to find the range of gains for a closed-loop unity feedback system to be stable. The system we will test looks like this:

where G(s) is :
```   s^2 + 10 s + 24
---------------
s^2 - 8 s + 15
```
This system has a gain K which can be varied in order to modify the response of the closed-loop system. However, we will see that we can only vary this gain within certain limits, since we have to make sure that our closed-loop system will be stable. This is what we will be looking for: the range of gains that will make this system stable in the closed loop.

The first thing we need to do is find the number of positive real poles in our open-loop transfer function:

```	  roots([1 -8 15])

ans =
5
3
```
The poles of the open-loop transfer function are both positive. Therefore, we need two anti-clockwise (N = -2) encirclements of the Nyquist diagram in order to have a stable closed-loop system (Z = P + N). If the number of encirclements is less than two or the encirclements are not anti-clockwise, our system will be unstable.

Let's look at our Nyquist diagram for a gain of 1:

```sys = tf([ 1 10 24], [ 1 -8 15]);
nyquist(sys)
```

There are two anti-clockwise encirclements of -1. Therefore, the system is stable for a gain of 1. Now we will see how the system behaves if we increase the gain to 20:

```nyquist(20*sys)
```

The diagram expanded. Therefore, we know that the system will be stable no matter how much we increase the gain. However, if we decrease the gain, the diagram will contract and the system might become unstable. Let's see what happens for a gain of 0.5:

```nyquist(0.5*sys)
```

The system is now unstable. By trial and error we find that this system will become unstable for gains less than 0.80. We can verify our answers by zooming in on the Nyquist plots as well as by looking at the closed-loop steps responses for gains of 0.79, 0.80, and 0.81.

### Gain Margin

We already defined the gain margin as the change in open-loop gain expressed in decibels (dB), required at 180 degrees of phase shift to make the system unstable. Now we are going to find out where this comes from. First of all, let's say that we have a system that is stable if there are no Nyquist encirclements of -1, such as :
```  50
-----------------------
s^3 + 9 s^2 + 30 s + 40
```
Looking at the roots, we find that we have no open loop poles in the right half plane and therefore no closed-loop poles in the right half plane if there are no Nyquist encirclements of -1. Now, how much can we vary the gain before this system becomes unstable in closed loop? Let's look at the following figure:

The open-loop system represented by this plot will become unstable in closed loop if the gain is increased past a certain boundary. The negative real axis area between -1/a (defined as the point where the 180 degree phase shift occurs...that is, where the diagram crosses the real axis) and -1 represents the amount of increase in gain that can be tolerated before closed-loop instability.

If we think about it, we realize that if the gain is equal to a, the diagram will touch the -1 point:

Therefore, we say that the gain margin is 'a' units. However, we mentioned before that the gain margin is usually measured in decibels. Hence, the gain margin is :
```GM = 20*log10(a) [dB]
```
We will now find the gain margin of the stable, open-loop transfer function we viewed before. Recall that the function is:
```  50
-----------------------
s^3 + 9 s^2 + 30 s + 40
```
and that the Nyquist diagram can be viewed by typing:
```sys = tf(50, [1 9 30 40 ]);
nyquist(sys)
```

As we discussed before, all that we need to do to find the gain margin is find 'a', as defined in the preceding figure. To do this, we need to find the point where there is exactly 180 degrees of phase shift. This means that the transfer function at this point is real (has no imaginary part). The numerator is already real, so we just need to look at the denominator. When s = j*w, the only terms in the denominator that will have imaginary parts are those which are odd powers of s. Therefore, for G(j*w) to be real, we must have:

```-j w^3 + 30 j w = 0
```
which means w=0 (this is the rightmost point in the Nyquist diagram) or w=sqrt(30). We can then find the value of G(j*w) at this point using polyval:
```w=sqrt(30);
polyval(50,j*w)/polyval([1 9 30 40],j*w)
```
You should get the following output from MATLAB
```ans =

-0.2174
```
The answer is: -0.2174 + 0i. The imaginary part is zero, so we know that our answer is correct. We can also verify by looking at the Nyquist plot again. The real part also makes sense. Now we can proceed to find the gain margin.

We found that the 180 degrees phase shift occurs at -0.2174 + 0i. This point was previously defined as -1/a. Therefore, we now have 'a', which is the gain margin. However, we need to express the gain margin in decibels,
```-1/a = -0.2174
=> a = 4.6
=> GM = 20*log10( 4.6) = 13.26 dB
```
We now have our gain margin. Let's see how accurate it is by using a gain of a = 4.6 and zooming in on the Nyquist plot:

```a = 4.6
nyquist(a*sys)```

The plot appears to go right through the -1 point. We will now verify the accuracy of our results by viewing the zoomed Nyquist diagrams and step responses for gains of 4.5, 4.6, and 4.7.

### Phase Margin

We have already discussed the importance of the phase margin. Therefore, we will only talk about where this concept comes from. We have defined the phase margin as the change in open-loop phase shift required at unity gain to make a closed-loop system unstable. Let's look at the following graphical definition of this concept to get a better idea of what we are talking about.

Let's analyze the previous plot and think about what is happening. From our previous example we know that this particular system will be unstable in closed loop if the Nyquist diagram encircles the -1 point. However, we must also realize that if the diagram is shifted by theta degrees, it will then touch the -1 point at the negative real axis, making the system marginally stable in closed loop. Therefore, the angle required to make this system marginally stable in closed loop is called the phase margin (measured in degrees). In order to find the point we measure this angle from, we draw a circle with radius of 1, find the point in the Nyquist diagram with a magnitude of 1 (gain of zero dB), and measure the phase shift needed for this point to be at an angle of 180 deg.

Frequency Response Examples
Cruise Control | Motor Speed | Motor Position | Bus Suspension | Inverted Pendulum | Pitch Controller | Ball and Beam

Tutorials
MATLAB Basics | MATLAB Modeling | PID | Root Locus | Frequency Response | State Space | Digital Control | Simulink Basics | Simulink Modeling | Examples