% Example Problem 7-4 p 557 Ogata
% Lag Compensation
disp(' '); disp('Start here'); disp(' ')
disp('G')
G=zpk([],[0 -1 -2],2)
ifig=0
ifig=ifig+1; figure(ifig)
step(G)
title('Open-loop step response')
% [EssStep,Kp,EssRamp,Kv,EssParabola,Ka,type]=ErrorConst(SISOsys)
K=5
[EssStep,Kp,EssRamp,Kv,EssParabola,Ka,type]=ErrorConst(K*G)
ifig=ifig+1; figure(ifig)
bode(K*G)
title('Fig 7-30, Bode Diagram of G1=5*G(s)')
grid on
[GmG1,PmG1,WcgG1,WcpG1]=margin(K*G)
GmG1_dB = 20*log10(GmG1)
disp('G1=K*G')
K*G


disp('Gc')
Gc=zpk([-.1],[-.01],0.5)
ifig=ifig+1; figure(ifig)
bode(Gc)
grid on
title('Bode plot of Gc(s)=0.5(s+0.1)/(s+0.01)')

ifig=ifig+1; figure(ifig)
bode(Gc*G)
grid on
title('GcG')
%[Gm,Pm,Wcg,Wcp] = MARGIN(SYS)
[GmGcG,PmGcG,WcgGcG,WcpGcG]=margin(Gc*G)
GmGcG_dB = 20*log10(GmGcG)

ifig=ifig+1; figure(ifig)
cl=feedback(Gc*G,1)
step(cl)
title('Closed-loop Step response')

ifig=ifig+1; figure(ifig)
bode(cl)
grid on
title('Closed loop = GcG/(1+GcG)')

bw=bandwidth(cl)