%  Example 10.2.  Hinf controller synthesis with frequency weighting.
clear
%  Define the plant without weighting functions for use in closed loop modeling.
A1=-1;Bw1=[0 0];Bu1=1;
Cm1=-1;
Cy1=[-1 0]';
Dyw1=[1 0
      0 0];
Dyu1=[0 0.001]';
Dmw1=[1 -1];
Dmu1=0;
%  Define the plant with weighting functions for control design.
A=diag([-1 -0.1 -10]);
Bw=[ 0   0
    0.1  0
	 0  10];
Bu=[1 0 0]';
Cm=[-1 1 -1];
Cy=[-1 1 0
     0 0 0];
Dyw=[0 0
     0 0];
Dyu=[0 0.001]';
Dmw=[0 1];
Dmu=0;
%  Generate the controller for the augmented model.
Plant=pck(A,[Bw Bu],[Cy;Cm],[Dyw Dyu;Dmw Dmu]);
%  Generate the steady-state H-infinity suboptimal controller.
[K,Gcl]=hinfsyn(Plant,1,1,0.1,0.1,0.01);
[Ac,Bc,Cc,Dc]=unpck(K);
%  Generate the closed loop model.
Acl=[  A1   Bu1*Cc
     Bc*Cm1   Ac  ];
Bcl=[  Bw1
     Bc*Dmw1];
Ccl=[Cy1 Dyu1*Cc];
Dcl=Dyw1;
%  Define the time vector for simulation.
dt=0.01;
t=0:dt:10;
%  Define the simulation inputs.
r=ones(length(t),1);
w2=(2*rand(length(t),1)-1);
v=lsim(-10,10,-1,1,w2,t);
w=[r v];
%  Simulate the closed loop system.
y=lsim(Acl,Bcl,Ccl,Dcl,w,t);
%  Plot the results.
set(0,'DefaultAxesFontName','times')
set(0,'DefaultAxesFontSize',16)
set(0,'DefaultTextFontName','times')
figure(1)
clf
subplot(211),plot(t,r-y(:,1))
xlabel('Time (sec)')
ylabel('Motor velocity')
grid
subplot(212),plot(t,1000*y(:,2))
xlabel('Time (sec)')
ylabel('Control')
grid
