echo on
%
% BodePlt.m Keith Holbert July 2001
%
% Initialize a frequency vector (Hz) using
% linspace(a,b,n) which generates an n point vector from a to b inclusive.
f = linspace(0.01,1000,100);
% Create the transfer function for Extension Exercise E12.3.
xfer = (1e4 * (2 + 2j*pi.*f)) ./ ((10 + 2j*pi.*f) .* (100 + 2j*pi.*f));
% We will plot both the magnitude and phase characteristics in various forms.
% The first Bode plot uses a linear frequency axis and linear magnitude plot.
subplot(2,1,1);
plot(f,abs(xfer));
title('Transfer Function Magnitude');
ylabel('Gain (units)');
subplot(2,1,2);
% Don't forget to convert the phase angle from radians to degrees.
plot(f,180/pi*angle(xfer));
title('Transfer Function Phase');
ylabel('Angle (degrees)');
xlabel('Frequency (Hz)');
% Unfortunately the linear frequency axis compresses the information
% at lower frequencys such that a log frequency axis is needed.
pause
figure;
% The second Bode plot uses a log frequency axis (semilog plot).
% The gain magnitude has units but they were not specified by the textbook.
subplot(2,1,1);
semilogx(f,abs(xfer));
title('Transfer Function Magnitude');
ylabel('Gain (units)');
subplot(2,1,2);
semilogx(f,180/pi*angle(xfer));
title('Transfer Function Phase');
ylabel('Angle (degrees)');
xlabel('Frequency (Hz)');
% Here the lower frequency information is visible; however, the transfer
% function was computed with a linear-spaced frequency vector such that
% low frequency resolution is lost. So, we need to compute the transfer
% function at logarithmically spaced intervals.
pause
figure;
% The remaining Bode plots use a log frequency axis, but
% we first re-generate the frequency vector with log spacing using
% logspace(a,b,n) which generates an n-point vector from decades 10^a to 10^b inclusive.
f = logspace(-2,3,100);
% Next, re-create the transfer function from Extension Exercise E12.3.
xfer = (1e4 * (2 + 2j*pi.*f)) ./ ((10 + 2j*pi.*f) .* (100 + 2j*pi.*f));
subplot(2,1,1);
semilogx(f,abs(xfer));
title('Transfer Function Magnitude');
ylabel('Gain (units)');
subplot(2,1,2);
semilogx(f,180/pi*angle(xfer));
title('Transfer Function Phase');
ylabel('Angle (degrees)');
xlabel('Frequency (Hz)');
% This is much better.
pause
figure;
% The fourth plot changes the gain magnitude scale to a log axis.
subplot(2,1,1);
loglog(f,abs(xfer));
title('Transfer Function Magnitude');
ylabel('Gain (units)');
subplot(2,1,2);
semilogx(f,180/pi*angle(xfer));
title('Transfer Function Phase');
ylabel('Angle (degrees)');
xlabel('Frequency (Hz)');
% This is very similar to our hand-drawn straight-line approximation.
pause
figure;
% The fifth and last plot changes the gain magnitude units into dBs.
subplot(2,1,1);
% Since the log is taken while the dB gain value of the xfer function is computed,
% we should not use MATLAB loglog plot function
semilogx(f,20*log10(abs(xfer)));
title('Transfer Function Magnitude');
ylabel('Gain (dB)');
subplot(2,1,2);
semilogx(f,180/pi*angle(xfer));
title('Transfer Function Phase');
ylabel('Angle (degrees)');
xlabel('Frequency (Hz)');
% It is worthwhile to compare the third and fifth Bode plots to one another.