%  Figure 6.57      Feedback Control of Dynamic Systems, 8e
%                   Franklin, Powell, Emami
%   

clear all
close all
clf
 
w = logspace(-1,1,500);

num = 9;
den = conv([1 0.5],[1 1]);
den = conv(den,[1 2]);

[mag,phas]=bode(num,den,w);

[OLGM,OLPM,OLWcg,OLWcp] = margin(mag,phas,w);
data0 = [OLGM,OLPM,OLWcg,OLWcp]

%Lead compensator, first guess
numl = [1 1];
denl = 0.333*[1 3];

numc = conv(num,numl);
denc = conv(den,denl);

[magc,phasc] = bode(numc,denc,w);
[D1GM,D1PM,D1Wcg,D1Wcp] = margin(magc,phasc,w);
data1 = [D1GM,D1PM,D1Wcg,D1Wcp]

dencl = denc + [0 0 0 numc];

t = 0:.1:20;
y = step(numc,dencl,t);

%Design Iteration, next guess
numl = [1 1.5];
denl = 0.1*[1 15];

numcc = conv(num,numl);
dencc = conv(den,denl);

[magcc,phascc] = bode(numcc,dencc,w);
[D2GM,D2PM,D2Wcg,D2Wcp] = margin(magcc,phascc,w);
data2 = [D2GM,D2PM,D2Wcg,D2Wcp]

[data0; data1; data2]


figure(1)
subplot(2,1,1)

loglog(w,mag,'-',w,ones(500,1),'-',w,ones(500,1),'-',w,ones(500,1),'-','LineWidth',2);

text( 1.5*0.1, 5, "K = 9")


axis([0.1, 10, 0.01, 10])
grid on;
%xlabel('w (rad/sec)');
ylabel('Magnitude');
title('Bode Plot for lead-compensation design (a) magnitude');

subplot(2,1,2)

semilogx(w,phas,w,-180*ones(500,1),w,-180*ones(500,1),w,-180*ones(500,1),'-','LineWidth',2);

axis([0.1, 10, -270, 0])
grid on;
xlabel('\omega (rad/sec)');
ylabel('Phase (deg)');
title('(b) phase');


figure(2)
subplot(2,1,1)

loglog(w,ones(500,1),'-',w,magc,'--',w,ones(500,1),'-',w,ones(500,1),'-','LineWidth',2);

text( 2.2*0.1, 3, "D_c = [1 1]/0.333*[1 3]")

axis([0.1, 10, 0.01, 10])
grid on;
%xlabel('w (rad/sec)');
ylabel('Magnitude');
title('Bode Plot for lead-compensation design (a) magnitude');

subplot(2,1,2)

semilogx(w,-180*ones(500,1),w,phasc,'--',w,-180*ones(500,1),w,-180*ones(500,1),'LineWidth',2);

axis([0.1, 10, -270, 0])

grid on;
xlabel('\omega (rad/sec)');
ylabel('Phase (deg)');
title('(b) phase');


figure(3)

subplot(2,1,1)

loglog(w,ones(500,1),'-',w,ones(500,1),'-',w,magcc,'-.',w,ones(500,1),'-','LineWidth',2);

text( 3*0.1, 1.5, "D_c = [1 1.5]/0.1*[1 15]")


axis([0.1, 10, 0.01, 10])
grid on;
%xlabel('w (rad/sec)');
ylabel('Magnitude');
title('Bode Plot for lead-compensation design (a) magnitude');

subplot(2,1,2)

semilogx(w,-180*ones(500,1),w,-180*ones(500,1),w,phascc,'-.',w,-180*ones(500,1),'LineWidth',2);


axis([0.1, 10, -270, 0])
grid on;
xlabel('\omega (rad/sec)');
ylabel('Phase (deg)');
title('(b) phase');




figure(4)



subplot(2,1,1)

loglog(w,mag,'-',w,magc,'--',w,magcc,'-.',w,ones(500,1),'-','LineWidth',2);

text( 1.5*0.1, 5, "K = 9")
text( 2.2*0.1, 3, "D_c = [1 1]/0.333*[1 3]")
text( 3*0.1, 1.5, "D_c = [1 1.5]/0.1*[1 15]")

axis([0.1, 10, 0.01, 10])
grid on;
%xlabel('w (rad/sec)');
ylabel('Magnitude');
title('Bode Plot for lead-compensation design (a) magnitude');

subplot(2,1,2)

semilogx(w,phas,'-',w,phasc,'--',w,phascc,'-.',w,-180*ones(500,1),'LineWidth',2);

axis([0.1, 10, -270, 0])
grid on;
xlabel('\omega (rad/sec)');
ylabel('Phase (deg)');
title('(b) phase');


