%  Example 10.6 (continuation). Generate Robust Performance for the 
%  closed loop system.
%  Define the aerodynamics model.
%    States: alpha, theta_dot
%    Inputs: delta, wZa, wMa, wMtd
%    Outputs:  eta, alpha, d*theta_2dot, theta_dot
Za=-5.24;Zd=-0.4;Ma=-47;Mtd=-4.69;Md=-1134;V=1.82;d=8.55e-4;
Aa=[Za  1
    Ma Mtd];
Ba=[Zd 1 0 0
    Md 0 1 1];
Ca=[V*Za   0
     1     0
    Ma*d Mtd*d
     0     1  ];
Da=[V*Zd V 0 0
      0  0 0 0
    Md*d 0 d d
      0  0 0 0];
Aero=pck(Aa,Ba,Ca,Da);
%  Define the servo model.
wn=189;z=0.7;
As=[   0      1
    -wn*wn -2*z*wn];
Bs=[  0
    wn*wn];
Cs=eye(2);
Ds=[0 0]';
Servo=pck(As,Bs,Cs,Ds);
%  Define the Flexible Body Dynamics Weight.
nufb=1.75e-4*[1 1]'*conv([1/0.0025 1],[1/25 1]);
defb=conv([1/250 1],[1/250 1]);
[Afb,Bfb,Cfb,Dfb]=tf2ss(nufb,defb);
[Afbb,Bfbb,Cfbb]=balreal(Afb,Bfb,Cfb);
Wfb=pck(Afbb,Bfbb,Cfbb,Dfb);
%  Define the Accelerometer Error Weight.
nuer=(50/30)*[0.025 1];deer=[10 1];
[Aer,Ber,Cer,Der]=tf2ss(nuer,deer);
[Aerb,Berb,Cerb]=balreal(Aer,Ber,Cer);
We=pck(Aerb,Berb,Cerb,Der);
%  Perform the system interconnection to yield the nominal closed loop system.
%    Inputs: wZa, wMa, wMtd, wi, wfb, etac, GyroErr, deltac.
%    Outputs:  yZa, yMa, yMtd, yi, yfb, We(etac-eta), Wc*delta, Wcd*delta_dot,
%              (etac-eta), Pitch Rate
systemnames = 'Aero Servo Wfb We K';
inputvar = '[wZa; wMa; wMtd; wi; wfb; etac; e_gyro]';
outputvar = ['[1.5*Aero(2); 4.7*Aero(2); 1*Aero(4); 0.5*K;', ...
             'Servo(1); We; 1.4*Servo(1); 0.1*Servo(2)]'];
input_to_Aero='[Servo(1); wZa; wMa; wMtd]';
input_to_Servo='[K+wi]';
input_to_Wfb='[wfb]';
input_to_We='[30*etac-Aero(1)]';
input_to_K='[Aero(1)+Aero(3)+Wfb(1);Aero(4)+Wfb(2)+0.001*e_gyro;etac]';
sysoutname='M';
cleanupsysic='yes';
sysic
%  Define a frequency vector.
omega=logspace(-1,4);
%  Generate the frequency response.
frM=frsp(M,omega);
%  Define the block structure for evaluating robust performance.
blk=ones(6,2);
blk(6,:)=[2 3];
%  Generate the structured singular value.
MUofM=mu(frM,blk);
%
%  Generate Robust Stability for the closed loop system.
%
%  Select Mydwd.
Mydwd=sel(M,1:5,1:5);
%  Generate the frequency response.
frMydwd=frsp(Mydwd,omega);
%  Define the block structure for evaluating robust stability.
blk=ones(5,2);
%  Generate the structured singular value.
MUofMydwd=mu(frMydwd,blk);
%
%  Generate Nominal Performance for the closed loop system.
%
%  Select Myd. 
Myd=sel(M,6:8,6:7);
%  Generate the frequency response.
frMyd=frsp(Myd,omega);
%  Generate the principle gains.
SIGofMyd=vsvd(frMyd);
%  Plot Robust performance, robust stability, and nominal performance results.
set(0,'DefaultAxesFontName','times')
set(0,'DefaultAxesFontSize',16)
set(0,'DefaultTextFontName','times')
figure(3)
clf
vplot('liv,m',sel(MUofM,1,1),'r-',sel(MUofMydwd,1,1),'r--',sel(SIGofMyd,1,1),'r-.')
axis([0.1 10000 0 5])
xlabel('Frequency (rad/sec)')
ylabel('SSV')
grid
legend('Robust performance','Robust stability','Nominal performance')
