echo on
%
% EEE202BodePlt.m Keith Holbert October 2006
%
clear all
% Initialize a frequency vector (Hz) using linspace(a,b,n)
% which generates an n point vector from a to b inclusive.
f = linspace(0.0001,10,100);
% Encode the desired transfer function (Prob. P9-3).
xfer = 2 ./ (1 + j*4* (2*pi.*f));
% We will plot both the magnitude and phase characteristics
% in various forms using this MATLAB program.
% First Bode plot uses linear frequency and magnitude axes.
subplot(2,1,1);
plot(f,abs(xfer));
title('Transfer Function Magnitude (Linear Data; Linear Freq.)');
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 information
% at lower frequencies 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 explicitly
% specified by the textbook, but they are likely volts/amp.
subplot(2,1,1);
semilogx(f,abs(xfer));
title('Transfer Function Magnitude (Linear Data; Log Freq.)');
ylabel('Gain (A/V)');
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(-4,1,100);
% Next, re-create the transfer function.
xfer = 2 ./ (1 + j*4* (2*pi.*f));
subplot(2,1,1);
semilogx(f,abs(xfer));
title('Transfer Function Magnitude (Log Data; Log Freq.)');
ylabel('Gain (A/V)');
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 (Log Data; Log Scales)');
ylabel('Gain (A/V)');
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 a 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 plotting function.
semilogx(f,20*log10(abs(xfer)));
title('Transfer Function Magnitude (Log Data; dB scale)');
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 fourth/fifth Bode
% magnitude plots to one another to appreciate the different views.
% Also, plotting other functions may require an adjustment to
% the selected frequency range over which the function is generated.