Bode Example #2, Relative Stability

MATLAB Code




%         ***** MATLAB Code Starts Here
%

%BODE_MARGINS_MAT

%

fig_size = [232 84 774 624];

olz = [-1;-2]; % Transfer function zeros

olp = [0;-.3;-.7+j*2;-.7-j*2;-15]; % Transfer function poles

%

num = real(poly(olz)); % Numerator polynomial

den = real(poly(olp)); % Denominator polynomial

%

w = logspace(-3,3,601); % frequency vector

[mag1,ph1] = bode(num,den,w); % open-loop frequency response for K=1

%

K1 = 2; % 1st Transfer function gain

%

% Magnitude & Phase plots for K = 2

%

figure(1),clf,semilogx(w,20*log10(K1*mag1),w,ph1),grid,xlabel('Frequency (r/s)'),...

ylabel('Magnitude (db) & Phase (deg)'),...

title('Bode Magnitude and Phase Plots, K = 2'),set(gcf,'Position',fig_size)

%

% Magnitude & Phase plots for K = 2

% with gain and phase margins and crossover frequencies shown

%

figure(2),clf,semilogx(w,20*log10(K1*mag1),w,ph1),grid,xlabel('Frequency (r/s)'),...

ylabel('Magnitude (db) & Phase (deg)'),...

hold on,semilogx([min(w) max(w)],[-180 -180],'r--',[min(w) max(w)],[0 0],'r--'),hold off,...

title('Gain and Phase Margins, K = 2'),set(gcf,'Position',fig_size)

%

[gm1,pm1,wphi1,wx1] = margin(K1*num,den); % gain and phase margins

dcl1 = den + K1 * [0 0 0 num];

clp1 = roots(dcl1); % closed-loop poles

%

hold on,semilogx([wphi1 wphi1],[-20*log10(gm1) 0],'m--'),hold off

hold on,semilogx([wx1 wx1],[-180 -180+pm1],'m--'),hold off

%

K2 = 25; % 2nd Transfer function gain

%

% Magnitude & Phase plots for K = 25

% with gain and phase margins and crossover frequencies shown

%

figure(3),clf,semilogx(w,20*log10(K2*mag1),w,ph1),grid,xlabel('Frequency (r/s)'),...

ylabel('Magnitude (db) & Phase (deg)'),...

hold on,semilogx([min(w) max(w)],[-180 -180],'r--',[min(w) max(w)],[0 0],'r--'),hold off,...

title('Gain and Phase Margins, K = 25'),set(gcf,'Position',fig_size)

%

[gm2,pm2,wphi2,wx2] = margin(K2*num,den); % gain and phase margins

dcl2 = den + K2 * [0 0 0 num];

clp2 = roots(dcl2); % closed-loop poles

%

hold on,semilogx([wphi2 wphi2],[-20*log10(gm2) 0],'m--'),hold off

hold on,semilogx([wx2 wx2],[-180 -180+pm2],'m--'),hold off

%

K3 = 400; % 3rd Transfer function gain

%

% Magnitude & Phase plots for K = 400

% with gain and phase margins and crossover frequencies shown

%

figure(4),clf,semilogx(w,20*log10(K3*mag1),w,ph1),grid,xlabel('Frequency (r/s)'),...

ylabel('Magnitude (db) & Phase (deg)'),...

hold on,semilogx([min(w) max(w)],[-180 -180],'r--',[min(w) max(w)],[0 0],'r--'),hold off,...

title('Gain and Phase Margins, K = 400'),set(gcf,'Position',fig_size)

%

wphi3 = wphi1;

gm3 = gm1/200;

wx3 = 5.5123;

pm3 = -30.6922;

dcl3 = den + K3 * [0 0 0 num];

clp3 = roots(dcl3); % closed-loop poles

%

hold on,semilogx([wphi3 wphi3],[-20*log10(gm3) 0],'m--'),hold off

hold on,semilogx([wx3 wx3],[-180 -180+pm3],'m--'),hold off

%

%
%         ***** MATLAB Code Stops Here

Click the icon to return to the Dr. Beale's home page