G=zpk([],[-0.1,-20,-20],8000);
Gd=zpk([],[-0.1],10);
GiGd=zpk([-20,-20],[],0.5*0.05*0.05);
K0=zpk([-0.1,-10],[0,-100],5);
K1=zpk([-2],[0],1)
K2=zpk([-2,-10],[0,-50],5);

L0=series(K0,G);
L1=series(K1,G);
L2=series(K2,G);
Unit=zpk([],[],1);

T1=feedback(L1,Unit);
T0=feedback(L0,Unit);
T2=feedback(L2,Unit);

S1=Unit-T1;
S0=Unit-T0;
S2=Unit-T2;

Sgd1=series(S1,Gd);
Sgd0=series(S0,Gd);
Sgd2=series(S2,Gd);

Kr=zpk([-90],[-9],1/10);
T2kr=series(Kr,T2);

figure(1);
step(Sgd0);
title('Step disturbance response K0');

figure(2);
bode(K0,'r');
hold on;
bode(K1,'g');
bode(K2,'b');
bode(GiGd,'k');
hold off
title('Controllers freq responses vs G^{-1}Gd')

 
figure(3);
step(Sgd1,'g');
hold on
step(Sgd2,'b');
hold off;
title('Step disturbance responses with K1 and K2')

 
figure(4);
step(T1,'g');
hold on;
step(T2,'b');
hold off;
title('Step reference responses with K1 and K2')



figure(5);
bode(T1,'g');
hold on
bode(T2,'b');
hold off;
title('Comp sensitivity functions for K1 and K2')

figure(6);
margin(L1);
% Stability margins with K1

figure(7);
margin(L2);
% Stability margins with K2

figure(8);
bode(Kr);
title('Freq response of Kr')

figure(9);
bode(T2kr,'b');
hold on;
bode(T2,'g');
hold off
title('Freq response before and after implementation of Kr')

figure(10);
step(T2kr,'b');
hold on;
step(T2,'g');
hold off
title('Step response before and after implementation of Kr')




