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

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

num = 0.9*2;
den = [1 2 0 0];

Nw = size(w);

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

n1 = conv([1/10 1],[1 0.5]);
num1 = conv(num,n1);
den1 = conv(den,[1 0]);
[mc1,pc1] = bode( num1, den1, w );

n2 = conv([1 1],[1 0.05]);
num2 = conv(num,n2);
den2 = conv(den,[1 0]);
[mc2,pc2] = bode( num2, den2, w );

n3 = conv([10 1],[1 0.005]);
num3 = conv(num,n3);
den3 = conv(den,[1 0]);
[mc3,pc3] = bode( num3, den3, w );


figure(1)

subplot(2,1,1)

loglog(w,m,w,ones(Nw),w,ones(Nw),'LineWidth',2);

text( 1e-3, 1e2, "G = 0.9/s^2, H = 2/(s+2)")


axis([.0001 100 .01 1000])
grid on;
%xlabel('\omega (rad/sec)');
ylabel('Magnitude');
title('Compensation for PID design Example 6.20: (a) magnitude.');

subplot(2,1,2)
if p>0, p=p-360; end      % Matlab quadrant control varies in different versions
if pc1>0, pc1=pc1-360; end

semilogx(w,p,w,-179*ones(Nw),w,-179*ones(Nw),'LineWidth',2);

axis([.0001 100 -270 -90])

grid on
xlabel('\omega (rad/sec)');
ylabel('Phase (deg)');
title('Compensation design for PID Example 6.20: (b)phase.');



figure(2)


subplot(2,1,1)

loglog(w,m,w,mc1,w,ones(Nw),'LineWidth',2);

text( 1e-3, 1e2, "G = 0.9/s^2, H = 2/(s+2)")

text( 1e0, 1e2, " 1/T_I = 0.5, 1/T_D = 10")


axis([.0001 100 .01 1000])
grid on;
%xlabel('\omega (rad/sec)');
ylabel('Magnitude');
title('Compensation for PID design Example 6.20: (a) magnitude.');

subplot(2,1,2)
if p>0, p=p-360; end      % Matlab quadrant control varies in different versions
if pc1>0, pc1=pc1-360; end

semilogx(w,p,w,pc1,w,-179*ones(Nw),'LineWidth',2);

axis([.0001 100 -270 -90])

grid on
xlabel('\omega (rad/sec)');
ylabel('Phase (deg)');
title('Compensation design for PID Example 6.20: (b)phase.');


figure(3)


subplot(2,1,1)

loglog(w,m,w,mc2,w,ones(Nw),'LineWidth',2);

text( 1e-3, 1e2, "G = 0.9/s^2, H = 2/(s+2)")

text( 1e0, 1e2, " 1/T_I = 0.05, 1/T_D = 1")


axis([.0001 100 .01 1000])
grid on;
%xlabel('\omega (rad/sec)');
ylabel('Magnitude');
title('Compensation for PID design Example 6.20: (a) magnitude.');

subplot(2,1,2)
if p>0, p=p-360; end      % Matlab quadrant control varies in different versions
if pc2>0, pc2=pc2-360; end

semilogx(w,p,w,pc2,w,-179*ones(Nw),'LineWidth',2);

axis([.0001 100 -270 -90])

grid on
xlabel('\omega (rad/sec)');
ylabel('Phase (deg)');
title('Compensation design for PID Example 6.20: (b)phase.');



figure(4)


subplot(2,1,1)

loglog(w,m,w,mc3,w,ones(Nw),'LineWidth',2);

text( 1e-3, 1e2, "G = 0.9/s^2, H = 2/(s+2)")

text( 1e0, 1e2, " 1/T_I = 0.005, 1/T_D = 0.1")


axis([.0001 100 .01 1000])
grid on;
%xlabel('\omega (rad/sec)');
ylabel('Magnitude');
title('Compensation for PID design Example 6.20: (a) magnitude.');

subplot(2,1,2)
if p>0, p=p-360; end      % Matlab quadrant control varies in different versions
if pc3>0, pc3 = pc3-360; end

semilogx(w,p,w,pc3,w,-179*ones(Nw),'LineWidth',2);

axis([.0001 100 -270 -90])

grid on
xlabel('\omega (rad/sec)');
ylabel('Phase (deg)');
title('Compensation design for PID Example 6.20: (b)phase.');





figure(5)


subplot(2,1,1)

loglog(w,m,w,mc1,w,ones(Nw),w,mc2,w,mc3,'LineWidth',2);

text( 1e-3, 1e2, "G = 0.9/s^2, H = 2/(s+2)")

text( 1e0, 1e2, " 1/T_I = 0.5, 1/T_D = 10")
text( 1e0, 2e1, " 1/T_I = 0.05, 1/T_D = 1")
text( 1e0, 5e0, " 1/T_I = 0.005, 1/T_D = 0.1")


axis([.0001 100 .01 1000])
grid on;
%xlabel('\omega (rad/sec)');
ylabel('Magnitude');
title('Compensation for PID design Example 6.20: (a) magnitude.');

subplot(2,1,2)
if p>0, p=p-360; end      % Matlab quadrant control varies in different versions
if pc3>0, pc3 = pc3-360; end

semilogx(w,p,w,pc1,w,-179*ones(Nw),w,pc2,w,pc3,'LineWidth',2);

axis([.0001 100 -270 -90])

grid on
xlabel('\omega (rad/sec)');
ylabel('Phase (deg)');
title('Compensation design for PID Example 6.20: (b)phase.');
