 echo on
%
%	HIMAT_X2
%
% It is assumed that you have just run 'himat_x1'
%
% This MATLAB script file is the second iteration of mu-synthesis 
%   control design for the HIMAT pitch axis control problem.
 
% Second iteration of the mu-synthesis procedure. Design a 
%   H-infinity control law for the new interconnection structure
%   which includes the frequency varying d-scales.

[k2,g2,gf2]=hinfsyn(himat_ic2,2,2,.9,1.3,.05,2);
 
% k2 should have the same number of states as HIMAT_IC2

minfo(k2)

% A H-infinity control law has been designed which achieves an
%  infinity norm of 1.10 for the new interconnection structure. 
%  The new system includes the frequency varying d-scales 
%  from the first iteration. To verify the gamma value, a frequency 
%  response of the closed loop is done and the maximum singular value 
%  of the closed-loop response across frequency is plotted. We will 
%  also check that the closed-loop poles are stable. 
 
 pause;   % strike any key to continue
 format short e
 g2 =starp(himat_ic2,k2,2,2);
 omega = logspace(0,4,16);
 g2g = frsp(g2,omega);
 rifd(spoles(g2))
 pause;   % strike any key to continue
 g2pk=pkvnorm(g2g);
 echo off
 fprintf( '\n Final gamma %g, \n max singular value %g \n \n',gf2,g2pk)
 echo on

%  Plot the maximum singular value across frequency

 g2gs=vsvd(g2g);
 vplot('liv,m',g2gs)
 echo off
 title('Singular values plot of Second Iteration in mu-synthesis');
 xlabel('Frequency (rad/s)');
 ylabel('Magnitude');
 grid;
 pause;   % strike any key to continue
 echo on
 
%  The control law can be analyzed using mu-analysis. The 
%  closed-loop system, G2, has 4 inputs and 4 outputs. Again,
%  the first two inputs and outputs correspond to the 
%  uncertainty block, and the second two  correspond to the
%  performance block. Therefore, we can define the uncertainty 
%  block as a full 2x2 uncertainty block and the disturbance
%  rejection block as a full 2x2 performance block. 
 
 pause;   % strike any key to continue
 
%  The mu-analysis program, MU, requires a frequency response
%  or constant matrix and a block structure. In this example, 
%  the frequency response is G2G and the block structure is 
%  two, 2x2 full blocks, blk = [2 2; 2 2].
%  MU returns the upper and lower bounds for mu in BNDS2,
%  the frequency varying d-scales (DV2), the rational
%  perturbation which destabilizes the closed-loop plant (RP2)
%  and the sensitivity of mu to the d-scales (SENS2).

 [bnds2,dv2,sens2,rp2]=mu(g2g,blk);
 
%  plot the maximum singular value and mu on the same plot
%    solid line - maximum singular value
%    dashed line - mu
 
 both=sbs(sel(g2gs,1,1),bnds2);
 vplot('liv,m',both)
 echo off
 title('Max. singular value and mu for the closed-loop system, g2');
 xlabel('Frequency (rad/s)');
 ylabel('Magnitude');
 grid;
 pause;   % strike any key to continue
 echo on
 
% clear variables not used and pack the memory space due to
%  the limitations on the Mac and PC's.
 
% clear rp2 g2g g2gs both 
 pack
 
% The next step is the fit the d-scales which are output from
%  the MU program. The MUSYNFIT function is used to fit 
%  the d-scales for each individual block. Next we fit a new
%  set of d-scales based on the new  H-infinity control law.
%
% MUSYNFIT requires the left d-scale from the previous 
%  mu-synthesis iteration (DSYSL) to avoid increasing the states
%  with each iteration. The d-scales from the mu-analysis 
%  problem, DV2, and the sensitivity of the d-scales, SENS2,
%  along with the block structure, number of controller 
%  inputs and outputs. For the second iteration, DSYSL is used 
%  for the left d-scale. Output from MUSYNFIT is the new left 
%  d-scale and right d-scale to be multiplied into the 
%  interconnection structure.
 
 pause;   % strike any key to continue
 
%  A 3rd order transfer function fit is recommended. This increases
%   the number of states in the interconnection structure to
%   3*(size of full block)*2. If one selects a different order
%   for the d-scale weight, the gamma iteration will converge
%   to a different value.

 [dsysl,dsysr]=musynfit(dsysl,dv2,sens2,blk,2,2);

% Now, wrap the new d-scales around the original 
%   interconnection structure.
%
%          himat_ic3 = dsysl*himat_ic*(dsysr)^-1

 pause;   % strike any key to continue
 himat_ic3=mmult(dsysl,himat_ic,minv(dsysr));
 minfo(himat_ic3)
 
% Thus ends the second iteration of mu-synthesis. Notice that
%  the new interconnection structure, HIMAT_IC3, has 20 states, 
%  only 12 more states than the original interconnection 
%  structure. The 12 states that were in the system from the 
%  previous iteration have already been scaled into the
%  d-scale fitting routine (Note, this is of course if you
%  fit the d-scales using a 3rd order system.)
 
%  Type in 'himat_x3' to continue with the third iteration of
%   mu-synthesis control design.
%
% Copyright MUSYN INC 1991,  All Rights Reserved
