Bode Plot Design Example #2

MATLAB Code



%      ***** MATLAB Code Starts Here *****
%

%DSGN_421_BODE_02_MAT

%

fig_size = [232 84 774 624];

num_p = 12*[1 0.585]; % open-loop transfer function

den_p = conv([1 0.9 0],[1 10.4 36]);

%

olp = roots(den_p); % open-loop poles & zeros

olz = roots(num_p);

%

ess_spec = 0.2; % ess specification

PM_spec = 90; % PM specification

Ts_spec = 10; % settling time specification

PO_spec = 10; % percent overshoot specification

%

[GM_p,PM_p,wphi_p,wx_p] = margin(num_p,den_p); % uncompensated stability margins

GMdb_p = 20*log10(GM_p);

%

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

%

[mag_p,ph_p] = bode(num_p,den_p,w); % Uncompensated Bode Plots

figure(1),clf,semilogx(w,20*log10(mag_p),w,ph_p,...

[min(w) max(w)],[-180 -180],'r--'),grid,...

xlabel('Frequency (r/s)'),ylabel('Magnitude (db) & Phase (deg)'),...

title('Uncompensated System'),set(gcf,'Position',fig_size)

%

Kv_p = num_p(length(num_p)) / den_p(length(den_p)-1);

ess_p = 1/Kv_p; % ess for original system

%

Kc = ess_p / ess_spec; % gain to satisfy ess spec

%

figure(2),clf,semilogx(w,20*log10(Kc*mag_p),w,ph_p,...

w,20*log10(mag_p),'k--',[min(w) max(w)],[-180 -180],'r--'),grid,...

xlabel('Frequency (r/s)'),ylabel('Magnitude (db) & Phase (deg)'),...

title(['Uncompensated System with K_c = ' num2str(Kc)]),set(gcf,'Position',fig_size)

%

% Compute step and ramp responses of the closed-loop

% system with Kc1 = 1 (original system) and Kc1 computed

% to satisfy ess specification.

%

t = linspace(0,30,1001); % time vector

%

[ncl_p,dcl_p] = cloop(num_p,den_p,-1); % closed-loop systems

[ncl_pk,dcl_pk] = cloop(Kc*num_p,den_p,-1);

%

ys_p = step(ncl_p,dcl_p,t); % step responses

ys_pk = step(ncl_pk,dcl_pk,t);

%

PO_p = 100*(max(ys_p)-1);

Ts_p = timesetl(t,ys_p);

%

PO_pk = 100*(max(ys_pk)-1);

Ts_pk = timesetl(t,ys_pk);

%

yr_p = step(ncl_p,[dcl_p 0],t); % ramp response

yr_pk = step(ncl_pk,[dcl_pk 0],t);

%

figure(3),clf,plot(t,ys_pk,t,ys_p),grid,...

xlabel('Time (s)'),ylabel('Step Responses'),...

title(['Uncompensated System and System with K_c = ' num2str(Kc)]),...

text(6,0.7,'Uncompensated'),set(gcf,'Position',fig_size)

%

figure(4),clf,plot(t,yr_pk,t,yr_p,[0 30],[0 30],'k--'),grid,...

xlabel('Time (s)'),ylabel('Ramp Responses'),...

title(['Uncompensated System and System with K_c = ' num2str(Kc)]),...

text(21.5,16,'Uncompensated'),set(gcf,'Position',fig_size)

%

% A two-stage phase lead compensator will be designed first to

% raise the phase curve to provide the necessary phase margin.

%

[GM_pk,PM_pk,wphi_pk,wx_pk] = margin(Kc*num_p,den_p); % stability margins with Kc1

GMdb_pk = 20*log10(GM_pk);

%

phimax = (PM_spec + 10 - PM_pk) / 2; % needed phase shift per stage

%

alfa_d = (1 - sin(phimax*pi/180)) / (1 + sin(phimax*pi/180));

%

zc_d = wx_pk * sqrt(alfa_d); % lead compensator pole & zero

pc_d = zc_d / alfa_d;

%

num_c_d = conv([1/zc_d 1],[1/zc_d 1]); % lead compensator transfer function

den_c_d = conv([1/pc_d 1],[1/pc_d 1]);

%

[nf_d,df_d] = series(Kc*num_c_d,den_c_d,num_p,den_p); % compensated forward transfer function

%

%[GM_d,PM_d,wphi_d,wx_d] = margin(nf_d,df_d); % stability margins with lead comp

%GMdb_d = 20*log10(GM_d);

%

[mag_d,ph_d] = bode(nf_d,df_d,w);

figure(5),clf,semilogx(w,20*log10(mag_d),w,ph_d,...

w,20*log10(Kc*mag_p),'k--',w,ph_p,'k--',[min(w) max(w)],[-180 -180],'r--'),grid,...

xlabel('Frequency (r/s)'),ylabel('Magnitude (db) & Phase (deg)'),...

title('Two-Stage Lead Compensated System'),set(gcf,'Position',fig_size)

%

% A one-stage lag compensator will now be designed to lower the

% magnitude curve to 0 db at the chosen value of gain

% crossover frequency.

%

[i0,i1] = min(abs(w-wx_pk)); % locate wx in frequency vector

%

alfa_g = Kc*mag_p(i1) / alfa_d;

%

zc_g = wx_pk / 10; % lag compensator pole & zero

pc_g = zc_g / alfa_g;

%

num_c_g = [1/zc_g 1]; % lag compensator transfer function

den_c_g = [1/pc_g 1];

%

[nf_dg,df_dg] = series(num_c_g,den_c_g,nf_d,df_d); % final compensated forward transfer

%

[GM_dg,PM_dg,wphi_dg,wx_dg] = margin(nf_dg,df_dg); % final stability margins

GMdb_dg = 20*log10(GM_dg);

%

[mag_dg,ph_dg] = bode(nf_dg,df_dg,w);

figure(6),clf,semilogx(w,20*log10(mag_dg),w,ph_dg,...

w,20*log10(mag_d),'k--',w,ph_d,'k--',[min(w) max(w)],[-180 -180],'r--'),grid,...

xlabel('Frequency (r/s)'),ylabel('Magnitude (db) & Phase (deg)'),...

title('Final Lag-Lead Compensated System'),set(gcf,'Position',fig_size)

%

[ncl_dg,dcl_dg] = cloop(nf_dg,df_dg,-1);

ys_dg = step(ncl_dg,dcl_dg,t); % final step response

yr_dg = step(ncl_dg,[dcl_dg 0],t); % final ramp response

%

PO_dg = 100*(max(ys_dg)-1);

Ts_dg = timesetl(t,ys_dg);

%

figure(7),clf,plot(t,ys_dg,t,ys_pk,t,ys_p),grid,...

xlabel('Time (s)'),ylabel('Step Responses'),title('Final Compensated Step Response'),...

text(6,0.7,'Uncompensated'),text(1.25,1.45,['K_c = ' num2str(Kc)]),set(gcf,'Position',fig_size)

%

figure(8),clf,plot(t,yr_dg,t,yr_pk,t,yr_p,[0 30],[0 30],'k--'),grid,...

xlabel('Time (s)'),ylabel('Ramp Responses'),title('Final Compensated Ramp Response'),...

text(21.5,16,'Uncompensated'),set(gcf,'Position',fig_size)

clear i0 i1

%
%      ***** MATLAB Code Stops Here *****

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