 echo on
%
%	HIMAT_X5
%
%  It is assumed that you have just run 'himat_x4' 

 
 pause     % strike any key to continue
 
%  We will compare the  'best' mu-synthesis control design for
%   HIMAT with the a loop-shaping control law using singular values 
%   and mu-analysis tools. Several comparisons will be preformed
%   between the two: weighted output sensitivity transfer functions,
%   weighted input complementary sensitivity transfer functions, 
%   mu values and the condition numbers of the control designs as a
%   function of frequency.
 
 
%  The loop-shaping control law will be loaded from the file 
%   KLP, an aschii file, and the 'best' mu control law is 
%   from the last mu-synthesis iteration  K4.
 
echo off
 mkklp;
echo on
 minfo(klp)
 minfo(k4)
 
% Notice that the mu-controller has a fairly high order

 pause      % strike any key to continue
 
% We need to form the closed-loop system with the loop-shaping
%  control law. We'll check it's eigenvalues to make sure it
%  stablizes the system.
 
 g4 = starp(himat_ic,k4,2,2);
 g_lp = starp(himat_ic,klp,2,2);
 g_mu = starp(himat_ic,k4,2,2);
 format short e

 rifd(spoles(g_lp))

 pause         % strike any key to continue
 om2 = logspace(-2,2,20);
 g_lpg = frsp(g_lp,om2);
 g_mug = frsp(g4,om2);
 
% Compare the singular value plots of the closed loop systems 
%   which is a 4 by 4 transfer function with the loopshaping 
%   and mu design.
 
  g_mugs = vsvd(g_mug);
  g_lpgs = vsvd(g_lpg);
  both = sbs(sel(g_mugs,1,1),sel(g_lpgs,1,1));
  clg
  vplot('liv,lm',both)
  echo off
  title('Closed loop mu and loop shaping controllers max singular values')
  xlabel('Frequency (rad/s)');
  ylabel('Magnitude');
  text(.15,.20,'solid = mu controller','sc')
  text(.15,.85,'dashed = loop shape controller','sc')
  pause
  clear both g_lpgs g_mugs
  echo on

  pause     % strike any key to continue
 
% The maximum singular value of the closed loop system with the 
%  mu controller is greater than 1 and the loop shaping control 
%  law is on the order of 1000. The mu controller doesn't 
%  try to make sigma_max small, it tries to make mu small. This can 
%  be done by allowing one of the off diagonal blocks to become very
%  large, as long as the other off diagonal block is very small 
%  (think about how the affect the off-diagonal terms).
 
% Comparing mu values of the mu-synthesis and the loop-shaping 
%  control law (mu is the lower curve). 

 pause      % strike any key to continue
 
 [bnd_mu,dv_mu,sens_mu,rp_mu]=mu(g_mug,blk);
 [bnd_lp,dv_lp,sens_lp,rp_lp]=mu(g_lpg,blk);
 both=sbs(sel(bnd_mu,1,1),sel(bnd_lp,1,1));
 vplot('liv,lm',both)
 echo off
 title('mu of the mu-synthesis controller and loop-shaping control design ');
 xlabel('Frequency (rad/s)');
 ylabel('Magnitude');
 text(.15,.20,'solid = mu controller','sc')
 text(.15,.85,'dashed = loop shape controller','sc')
 pause
 clear dv_mu dv_lp rp_mu rp_lp sens_mu sens_lp both
 echo on
 pause      % strike any key to continue
 
%   Let's look at their output sensitivity (So) and the 
%   input complementary (Ti) transfer functions for the nominal case. 
  
%   We will compare the two designs, for the nominal case and 
%   with an uncertainty block [0.1 0;0 -0.1] fed back around 
%   the input multiplicative uncertainty. 
 
% For the mu control law:
 
 g_munS = sel(g4,[3 4],[3 4]);
 g_munT = sel(g4,[1 2],[1 2]);
 
% For the loop shape control law:
 
 g_lpnS = sel(g_lp,[3 4],[3 4]);
 g_lpnT = sel(g_lp,[1 2],[1 2]);

 pause      % strike any key to continue
 
% Calculate their frequency responses between .01 and 100 rad/s 
%  and  compare them.
 
 om3 = logspace(-2,2,40);
 g_munSg = vsvd(frsp(g_munS,om3));
 g_munTg = vsvd(frsp(g_munT,om3));
 g_lpnSg = vsvd(frsp(g_lpnS,om3));
 g_lpnTg = vsvd(frsp(g_lpnT,om3));
 clg;
 
% Let's plot them and compare
 
% First the Nominal Output Sensitivity (SO) function for the 
% loop-shape control law and the mu control law.
 
 echo off  
 subplot(211), axis([-2 2 0 1]);
 subplot(212), axis([-2 2 0 1]);
 subplot(211), vplot('liv,m',g_lpnSg)
 subplot(211), title('SO (sensitivity function) for loop-shape control law');
 subplot(212), vplot('liv,m',g_munSg)
 subplot(212), title('SO (sensitivity function) for mu control law');
 subplot(212), xlabel('Frequency (rad/s)') ;ylabel('Magnitude');
 pause
 clear g_munSg g_lpnSg
 echo on

 pause      % strike any key to continue
 clg;
 
% Now, let's look at the Nominal Complementary Sensitivity (Ti)
%  function for the loop-shape control law and the mu control law.
   
 echo off
 subplot(211), axis([-2 2 0 1]);
 subplot(212), axis([-2 2 0 1]);
 subplot(211), vplot('liv,m',g_lpnTg)
 subplot(211), title('Ti (comp sens function) for loop-shape control law');
 subplot(212), vplot('liv,m',g_munTg)
 subplot(212), title('Ti (comp sens function) for mu control law');
 subplot(212), xlabel('Frequency (rad/s)') ;ylabel('Magnitude');
 pause
 clear g_munT g_munTg g_lpnT g_lpnTg
 echo on

 pause      % strike any key to continue
 clg;
 
%   Let's look at their output sensitivity (So) transfer functions for the 
%   perturbed system with delta = [0.1 0; 0 -.1].

delta = [0.1 0;0 -0.1];
himat_idn = himat_ic;
g_muidn = starp(himat_idn,k4,2,2);
g_lpidn = starp(himat_idn,klp,2,2);
g_muinS = sel(g_muidn,[3:4],[3:4]);
g_lpinS = sel(g_lpidn,[3:4],[3:4]);

g_lpids = starp(delta,g_lpidn,2,2);
g_lpidsg = frsp(g_lpids,om2);
g_muids = starp(delta,g_muidn,2,2);
g_muidsg = frsp(g_muids,om2);

 echo off  
 subplot(211), axis([-2 2 0 12]);
 subplot(212), axis([-2 2 0 12]);
 subplot(211), vplot('liv,m',vnorm(g_lpidsg))
 subplot(211), title('Sens function perturbed system, loop-shape controller');
 subplot(212), vplot('liv,m',vnorm(g_muidsg))
 subplot(212), title('Sensitivity function perturbed system, mu control law');
 pause
 echo on
 pause           %strike any key to continue

% One notices how much the loop shape controller degrades with the
%  inclusion of the perturbation, whereas the mu controller hardly
%  degrades at all. Hence, this degradation is related to the mu value
%  of the closed loop system. The mu control law had a very low mu
%  value whereas the loop shape control law had a large mu value.
 
% Let's look at the condition number of the control laws as
%  a function of frequency.
 
 kmug = frsp(k4,om2);
 klpg = frsp(klp,om2);
 outkmu = vsvd(kmug);
 outkl = vsvd(klpg);
 clear kmug klpg
 maxmu = sel(outkmu,1,1);
 minmu = sel(outkmu,2,1);
 maxl = sel(outkl,1,1);
 minl = sel(outkl,2,1);
 condmu = mmult(maxmu,minv(minmu));
 condl = mmult(maxl,minv(minl));
 clear max4 min4 maxl minl outk4 outkl
 clg
 axis([-2 2 0 4]);
 vplot('liv,lm',condmu,condl)
 echo off
 title('Condition number of loopshaping and mu control laws');
 xlabel('Frequency (rad/sec)');
 ylabel('Magnitude');
  text(.15,.30,'solid = mu controller','sc')
  text(.15,.85,'dashed = loop shape controller','sc')
 pause
 echo on

% High condition number controllers and plant are usually very 
%  sensitive to the introduction of perturbations into the system.

 pause      % strike any key to continue

% Comparing the output sensitivity and the input complementary 
%  sensitivity one can see that the mu control law is more robust
%  to input uncertainty.
 
% To wrap up the analysis, we will look at time domain performance.
%  Simulations with the nominal plants, G_MUNS and G_LPNS will be 
%  compared with the plants with uncertainty, G_MUDS and G_LPDS. 

% Type in 'himat_x6' for the time response analysis.
%
% Copyright MUSYN INC 1991,  All Rights Reserved
