% ExplX2_1.m, EXTRA example, 
% PI control of unstable plant

% G(s) = 1/(s-1) (0.01s+1)^2
%
G=nd2sys([1],[0.0001 0.0199 0.98 -1]);   %Plant

K = nd2sys([1.8 1],[1 0],2.4);   % PI-controller (which is almost identical to 
		% the S/KS H-infinity controller obtained with the weight in Table 2.3:
        % M=1.5; wb=10; Wp = nd2sys([1/M wb], [1 1.e-6]); Wu=1; ) 

L = mmult(G,K);
s = minv(madd(1,L));
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(K,s)); [u1,x]=step(A,B,C,D,1,ty);
figure(1);clf;
plot(ty,y1,ty,u1,'--')   
% Note that there is an inverse response in the input for an unstable plant

% Bode plots of L, S, T
omega=logspace(-1,3,41);
L_f = frsp(L,omega);
s_f = frsp(s,omega);
t_f = frsp(t,omega);
figure(2);clf;
vplot('bode_l',[.1 1000,.1,5],[.1,1000,-300,300],L_f,s_f,t_f,1,'w:')

% Find GM, PM and the peak values of $s$ and $t$ 
[A,B,C,D]=unpck(L); [Gm,Pm,W180,Wc]=margin(A,B,C,D)
hinfnorm(s)  % no peak in S
hinfnorm(t)  % peak in T of 1.4 at frequency 1.26 rad/s

% Summary:
% For unstable plants the peak in T is usually larger and at a lower
% frequency than that for S, and this is confirmed in this example. 
% Note that the opposite is usually true for a stable plant.  
