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

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

k = 10;

num = k;
den = [1 1 0];

[m,p] = bode(num,den,w);

[OLGM,OLPM,OLWcg,OLWcp] = margin(m,p,w);
data0 = [OLGM,OLPM,OLWcg,OLWcp]

num = conv(num,[10 1]);
den = conv(den,[100 1]);

[mc,pc] = bode(num,den,w);

[OLGM,OLPM,OLWcg,OLWcp] = margin(mc,pc,w);
data1 = [OLGM,OLPM,OLWcg,OLWcp]

[data0; data1]


w2 = [.001 10];
m2 = [1 1];

figure(1)

subplot(2,1,1)

loglog(w,m,w2,m2,'k',w2,m2,'k','LineWidth',2);

text( 5*0.01, 1e3, "K = 10")

axis( [1e-3, 10, 1e-2, 1e4 ])
grid on;
%xlabel('\omega (rad/sec)');
ylabel('Magnitude');
title('Frequency response of lag-comp. design, Ex 6.19: magnitude.');

subplot(2,1,2)

semilogx(w,p,'LineWidth',2);

axis( [1e-3, 10, -180, -80 ])

grid  on;
xlabel('\omega (rad/sec)');
ylabel('Phase (deg)');
title('Frequency response of lag comp. design: phase.');




figure(2)

subplot(2,1,1)

w2 = [.001 10];
m2 = [1 1];

loglog(w2,m2,'k',w,mc,w2,m2,'k','LineWidth',2);

text( 5*0.001, 10*1, "D_c = [10 1]/[100 1]")


grid on;
%xlabel('\omega (rad/sec)');
ylabel('Magnitude');
title('Frequency response of lag-comp. design, Ex 6.19: magnitude.');

subplot(2,1,2)

semilogx(w,pc,w,pc,'LineWidth',2);

grid  on;
xlabel('\omega (rad/sec)');
ylabel('Phase (deg)');
title('Frequency response of lag comp. design: phase.');



figure(3)

subplot(2,1,1)

w2 = [.001 10];
m2 = [1 1];

loglog(w,m,w,mc,w2,m2,'k','LineWidth',2);

text( 5*0.001, 10*1, "D_c = [10 1]/[100 1]")

text( 5*0.01, 1e3, "K = 10")

grid on;
%xlabel('\omega (rad/sec)');
ylabel('Magnitude');
title('Frequency response of lag-comp. design, Ex 6.19: magnitude.');

subplot(2,1,2)

semilogx(w,p,w,pc,'LineWidth',2);

grid  on;
xlabel('\omega (rad/sec)');
ylabel('Phase (deg)');
title('Frequency response of lag comp. design: phase.');



%return;
%numcl=[1 0.1];
%dencl=[1 1.01 1.01 0.1];
%t=0:.01:20;
%y=step(numcl,dencl,t);
%plot(t,y);
%grid;
%xlabel('sec');
%ylabel('y');
