%  Example 10.4.  Hinf design (trading off performance and robustness)
clear
%  Define the plant model.
A=[ 0  1
   0.5 0];
Bu=[0 0.1]';
Bw=[ 0  0
    0.1 0];
Cm=[1 0];
Cy=[1 0
    0 0];
Dyw=zeros(2);
Dyu=[0 4]';
Dmw=[0 sqrt(10)];
Dmu=0;
Plant=pck(A,[Bw Bu],[Cy;Cm],[Dyw Dyu;Dmw Dmu]);
%  Generate the controller for this model.
K=hinfsyn(Plant,1,1,275,275,1);
[Ac,Bc,Cc,Dc]=unpck(K);
%  Evaluate the robustness of the H-infinity controller.
%  Generate the state model of the loop transfer function.
Al=[  A   zeros(2)
    Bc*Cm    Ac   ];
Bl=[    Bu
    zeros(2,1)];
Cl=[zeros(1,2) -Cc];
%  Generate Nyquist plot.
set(0,'DefaultAxesFontName','times')
set(0,'DefaultAxesFontSize',16)
set(0,'DefaultTextFontName','times')
figure(1)
clf
axes('Position',[.15 .2 .75 .75]);
w=logspace(-3,1,100);
[re,im]=nyquist(Al,Bl,Cl,0,1,w);
plot(re,im,'r-',re,-im,'r-',-1,0,'r+',re(70),im(70),...
   'r>',re(70),-im(70),'r<')
xlabel('Real axis')
ylabel('Imaginary axis')
axis([-2 2 -2 2])
axis('square')
grid
%  Generate the stability margins.
[Gm,Pm,Wcg,Wcp] = MARGIN(Al,Bl,Cl,0);
wh=logspace(0,3);
[mag,pha]=bode(Al,Bl,Cl,0,1,wh);
Gmp = MARGIN(mag,pha,wh);
'GM+      Gm-       PM'
[Gmp Gm Pm]
%  Generate the closed loop model.
Acl=[  A   Bu*Cc
     Bc*Cm   Ac ];
Bcl=[  Bw
     Bc*Dmw];
Ccl=[Cy Dyu*Cc];
Dcl=Dyw;
Syscl=pck(Acl,Bcl,Ccl,Dcl);
%  Generate performance parameters.
hinfnorm(Syscl)
%
%  Generate a second controller with robustness to input multiplicative 
%  perturbations.
%
%  Define weights.
Ww=275/4;
Wy=2/275;
%  Generate plant model with weighting.
Bw1=[ 0  0   0
     0.1 0 Ww*0.1];
Cy1=[1/275 0
       0   0
	   0   0];
Dyw1=zeros(3);
Dyu1=[0 4/275 Wy]';
Dmw1=[0 sqrt(10) 0];
Plant=pck(A,[Bw1 Bu],[Cy1;Cm],[Dyw1 Dyu1;Dmw1 Dmu]);
%  Generate the controller for the weighted model.
K1=hinfsyn(Plant,1,1,2.45,2.45,0.01);
[Ac1,Bc1,Cc1,Dc1]=unpck(K1);
%  Evaluate the robustness of this controller.
%  Generate the state model of the loop transfer function.
Al1=[  A     zeros(2)
     Bc1*Cm    Ac1   ];
Bl1=[    Bu
    zeros(2,1)];
Cl1=[zeros(1,2) -Cc1];
%  Generate Nyquist plot.
figure(2)
clf
axes('Position',[.15 .2 .75 .75]);
w=logspace(-3,1,100);
[re,im]=nyquist(Al1,Bl1,Cl1,0,1,w);
plot(re,im,'r-',re,-im,'r-',-1,0,'r+',re(70),im(70),...
   'r>',re(70),-im(70),'r<')
xlabel('Real axis')
ylabel('Imaginary axis')
axis([-2 2 -2 2])
axis('square')
grid
%  Generate stability margins.
[Gm,Pm,Wcg,Wcp] = MARGIN(Al1,Bl1,Cl1,0);
wh=logspace(0,3);
[mag,pha]=bode(Al1,Bl1,Cl1,0,1,wh);
Gmp = MARGIN(mag,pha,wh);
'GM+      Gm-       PM'
[Gmp Gm Pm]
%  Generate the closed loop model and closed loop system norm.
Acl1=[   A   Bu*Cc1
      Bc1*Cm  Ac1  ];
Bcl1=[   Bw
      Bc1*Dmw];
Ccl1=[Cy Dyu*Cc1];
Dcl1=Dyw;
Syscl=pck(Acl1,Bcl1,Ccl1,Dcl1);
%  Generate performance parameters.
hinfnorm(Syscl)
