% Fig. 6.15   Feedback Control of Dynamic Systems, 8e 
%             Franklin, Powell, Emami
% 

clear all;
%close all;
clf

w = logspace(-2,2,100);

k = 10;
num = k;
den = conv([1 0],[1 2 1]);
[m10,p10] = bode(num,den,w);

k = 2;
num = k;
den = conv([1 0],[1 2 1]);
[m2,p2] = bode(num,den,w);

k=0.1;
num = k;
den = conv([1 0],[1 2 1]);
[mp1,pp1] = bode(num,den,w);



figure(1)

subplot(2,1,1)

loglog(w,mp1,w,ones(size(w)),w,ones(size(w)),w,ones(size(w)),'LineWidth',2);

text( 2e-2, 1e1, "K = 0.1")


axis( [ 0.01, 100, 1e-3, 100 ])

grid on;
xlabel('\omega (rad/sec)');
ylabel('Magnitude');
title('Frequency Response (a) magnitude');
%pause;

%figure(2)
subplot(2,1,2)
semilogx(w,pp1,w,-180*ones(size(w)),w,-180*ones(size(w)),w,-180*ones(size(w)),'-','LineWidth',2);

axis( [ 0.01, 100, -280, -80 ])

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


figure(2)

subplot(2,1,1)

loglog(w,ones(size(w)),w,m2,w,ones(size(w)),w,ones(size(w)),'LineWidth',2);

text( 1e-1, 5e0, "K = 2")


axis( [ 0.01, 100, 1e-3, 100 ])

grid on;
xlabel('\omega (rad/sec)');
ylabel('Magnitude');
title('Frequency Response (a) magnitude');
%pause;

%figure(2)
subplot(2,1,2)
semilogx(w,-180*ones(size(w)),w,p2,w,-180*ones(size(w)),w,-180*ones(size(w)),'-','LineWidth',2);

axis( [ 0.01, 100, -280, -80 ])

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



figure(3)

subplot(2,1,1)

loglog(w,ones(size(w)),w,ones(size(w)),w,m10,w,ones(size(w)),'LineWidth',2);

text( 1e0, 2e1, "K = 10")

axis( [ 0.01, 100, 1e-3, 100 ])

grid on;
xlabel('\omega (rad/sec)');
ylabel('Magnitude');
title('Frequency Response (a) magnitude');
%pause;

%figure(2)
subplot(2,1,2)
semilogx(w,-180*ones(size(w)), w,-180*ones(size(w)), w,p10,w,-180*ones(size(w)),'-','LineWidth',2);

axis( [ 0.01, 100, -280, -80 ])

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



figure(4)

subplot(2,1,1)

loglog(w,mp1,w,m2,w,m10,w,ones(size(w)),'LineWidth',2);

text( 2e-2, 1e1, "K = 0.1")
text( 1e-1, 5e0, "K = 2")
text( 1e0, 2e1, "K = 10")


axis( [ 0.01, 100, 1e-3, 100 ])

grid on;
xlabel('\omega (rad/sec)');
ylabel('Magnitude');
title('Frequency Response (a) magnitude');
%pause;

%figure(2)
subplot(2,1,2)
semilogx(w,pp1,w,p2,w,p10,w,-180*ones(size(w)),'-','LineWidth',2);

axis( [ 0.01, 100, -280, -80 ])

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

