% Chapter 12: Robust Control Systems
% CP12.3
%
J=28; a=1; b=2;
num=[1 a b]; den=J*[1 0 0 0];
sys = tf(num,den);
figure(1)
rlocus(sys)
k2=rlocfind(sys) % k2=79.2 meets the specs very closely

a=1; b=2;J=28;
k1=a*k2; k0=b*k2;
numc=[k2 k1 k0]; denc=[J 0 0 0]; sysc = tf(numc,denc)
sys_cl = feedback(sysc,[1]);
wbw=bandwidth(sys_cl);
[gm,pm0,wg,wc]=margin(sysc); 
pm0, wbw


Ji=[10:1:40];
for i=1:length(Ji)
 numc=[k2 k1 k0]; denc=[Ji(i) 0 0 0]; sysc = tf(numc,denc); 
 [mag,phase,w]=bode(sysc);
 [gm,pm]=margin(mag,phase,w);
 Pm(i)=pm;
end
figure(2)
hold off
clf
plot(Ji,Pm), grid
xlabel('J'), ylabel('Phase Margin (deg)')


hold on
plot([0 40],[45 45],'--')
plot([28],[pm0],'o')
