%Example 2.3, PI-control

% Plant (2.26), G(s)=3(-2s+1)/(5s+1)(10s+1)
Eq2_26

[A,B,C,D]=unpck(g);
[Ku,pm,Wu,wcp]=margin(A,B,C,D); % Ku=2.5 and Wu=15.2
Pu=2*pi/Wu;Kc=Ku/2.2;ti=Pu/1.2; %1.136,12.7, Ziegler-Nichols' tuning
c=nd2sys([ti,1],[ti,1.e-4],Kc); % PI-Controller
gc = mmult(g,c);
s = minv(madd(1,gc));
t = msub(1,s);
% Step response
[A,B,C,D]=unpck(t);[y1,x,ty]=step(A,B,C,D);
%Input signal
[A,B,C,D]=unpck(mmult(c,s)); [u1,x,tu]=step(A,B,C,D);

%Figure 2.7
figure(1);clf;
plot(ty,y1,tu,u1,'--',[0,50],[1,1],':')
axis([0 50 -0.5 2.5]);
text(40,1.3,'OUTPUT (y)');text(40,0.0,'INPUT (u)');
xlabel('Time');%ylabel('y');

%Figure 2.8
% Frequency responses of L, S, T
omega=logspace(-2,1,41);
gc_f = frsp(gc,omega);
s_f = frsp(s,omega);
t_f = frsp(t,omega);
figure(2);clf;
vplot('bode',gc_f,s_f,t_f,1,'w:')
axis([.01,10,-300,120])

% Find GM, PM and the peak values of $s$ and $t$ 
[A,B,C,D]=unpck(gc); [Gm,Pm,W180,Wc]=margin(A,B,C,D)
hinfnorm(s)  % find peak is 3.92 at w=0.27
hinfnorm(t)  % find peak is 3.35 at w=0.26
