Plotting Frequency Response

In order to illustrate the plotting of G(jw), we will generate our own plot of G(j*w)and compare it with the Bode plots and the Nyquist diagram. If we run the following m-file, we will see the points that comprise G(jw), in particular the point corresponding to G(j*3).

```%define the transfer function
num = conv([1,4],[1 6]);
den = conv([1,-3],[1 -5]);

%create a frequency vector
clf
w = logspace(-1,2,100);
hold on

%plot a '+' for each point corresponding to G(jw)
for i = 1:100
plot(polyval(num,j*w(1,i))/polyval(den, j*w (1,i)), '+')
end
%plot a green 'X' for frequency of  3rad/s
plot(polyval(num,j*3)/polyval(den, j*3), 'xg')

%plot vector from origin to this point
n= polyval(num,j*3)/polyval(den, j*3);
x = [0,real(n)];
y = [0,imag(n)];
plot(x,y, '-g')

%find magnitude and phase
Mag = abs(n);
Phase = 180 +360*atan(imag(n)/real(n))/(2*pi);

%find gain
Gain = 20*log10(Mag);
grid
Mag, Phase, Gain
```
The image should look similar to this:

Please note that each yellow cross represents the transfer function evaluated at a specific frequency and that only positive frequencies are used. Also note the point and vector associated with a frequency of 3 rad/s (green). In the MATLAB command window, you should see the Magnitude, Phase, and Gain (in dB) associated with this vector:

### Note: a decibel is defined as 20*log10 ( |G(j*w| )

```		Mag =
1.3558

Phase =
139.3987

Gain =
2.6440
```

## Bode representation

The Bode plots take each one of the points on the above plot and breaks it down into magnitude and phase. The magnitude is then plotted as gain in decibels and the phase is plotted in degrees. The frequency (on the independent axis) is plotted on a logarithmic scale. Let's take a look at the Bode plots for this function and see if our answers match.
```
bode(num,den)
```

If you look at the plots at a frequency of 3rad/s you will see the magnitude is a little over 2.5 dB and the phase is around 140 degrees. This agrees with our previous results.

## Nyquist Representation

Now that we know where Bode plots come form, let's see where Nyquist plots come from. The Nyquist diagram is basically a plot of G(j*w). However, both positive and negative frequencies of the contour we showed previously are taken into account. Let's plot the Nyquist diagram on top of our G(j*w) plot (this time, we will include the negative frequencies as well).
```%define the transfer function
num = conv([1,4],[1 6]);
den = conv([1,-3],[1 -5]);

%create a frequency vector similar to the one described above
w = logspace(-1,2,100);
negw = -1*w;
clf
hold on

%plot a '+' for each point corresponding to G(jw)
for i = 1:100
plot(polyval(num,j*w(1,i))/polyval(den, j*w (1,i)), '+')
plot(polyval(num,j*negw(1,i))/polyval(den, j*negw (1,i)), '+')
end

%draw Nyquist diagram on top to compare
nyquist(num,den)
hold off
```

A perfect match! Also note that the negative frequencies produce a mirror image across the real axis of the positive frequency response.