%
% ---------------------------------------------------------------
%  ACCPLT.M is a script file that produces the plots for the ACC
%     Benchmark problem.
% ---------------------------------------------------------------
%

% R. Y. Chiang & M. G. Safonov 
% Copyright (c) 1988 by the MathWorks, Inc.
% All Rights Reserved.

clg
if flag == 2
      subplot(111)
      semilogx(w,[perttw;pertt]);
      title('Cost Function in "s~" and "s"');
      grid;xlabel('Rad/Sec'); ylabel('PERRON SSV (db)');
      msmw = max(perttw);
      msmw = 1/(10^(msmw/20)/gamopt)*100;
      msm  = max(pertt);
      msm  = 1/(10^(msm/20)/gamopt)*100;
      text(0.2,0.3,['ADDITIVE MSM IN s~-DOMAIN: +-',num2str(msmw),' %'],'sc');
      text(0.2,0.2,['ADDITIVE MSM IN s-DOMAIN : +-',num2str(msm),' %'],'sc');
      %prtsc
      pause
end

if flag ~= 3
   clg
   subplot(221)
   plot(t,x1_ipw1);grid;
   title('x1');       xlabel('Sec')
   subplot(222)
   plot(t,x2_ipw1);grid;
   title('x2 (z)');   xlabel('Sec');
   subplot(223)
   plot(t,u_ipw1); grid;
   title('Control (u)'); xlabel('Sec');
   subplot(224)
   text(0.6,0.4,'Impulse Response @ M1','sc');
   text(0.6,0.35,'Sensor Noise: 0.001*sin(100t)','sc')
%  if flag == 1
   text(0.65,0.25,'Dashed: k = 0.5','sc');
   text(0.65,0.2,'Solid:  k = 1.0','sc');
   text(0.65,0.15,'Dotted: k = 2.0','sc');
%  end
   %prtsc
   pause
   clg
   subplot(221)
   plot(t,x1_ipw2);grid;
   title('x1');  xlabel('Sec');
   subplot(222)
   plot(t,x2_ipw2);grid
   title('x2 (z)');   xlabel('Sec');
   subplot(223)
   plot(t,u_ipw2); grid;
   title('Control (u)'); xlabel('Sec');
   subplot(224)
   text(0.6,0.4,'Impulse Response @ M2','sc');
   text(0.6,0.35,'Sensor Noise: 0.001*sin(100t)','sc')
%  if flag == 1
   text(0.65,0.25,'Dashed: k = 0.5','sc');
   text(0.65,0.2,'Solid:  k = 1.0','sc');
   text(0.65,0.15,'Dotted: k = 2.0','sc');
%  end
   %prtsc
   pause
else
   clg
   subplot(221)
   plot(t,dist_w);grid
   title('Disturbance: sin(0.5*t) @ M2');
   xlabel('Sec');
   subplot(222)
   plot(t,u_w2);grid;
   title('Control Energy (u)');
   xlabel('Sec'); 
   subplot(223)
   plot(t,x1_w2);grid;
   title('x1'); 
   xlabel('Sensor Noise: 0.001sin(100t)');
   subplot(224)
   plot(t,x2_w2);grid;
   title('x2 (z)');  
   xlabel('Sec (k = 0.5(- -), 1(-), 2(.))');
   %prtsc
   pause
   clg
   subplot(221)
   plot(t,dist_w);grid
   title('Disturbance: sin(0.5*t) @ M1');
   xlabel('Sec');
   subplot(222)
   plot(t,u_w1);grid;
   title('Control Energy (u)');   xlabel('Sec');
   subplot(223)
   plot(t,x1_w1);grid;
   title('x1');   
   xlabel('Sensor Noise: 0.001sin(100t)');
   subplot(224)
   plot(t,x2_w1);grid;
   title('x2 (z)');  
   xlabel('Sec (k = 0.5(- -), 1(-), 2(.))');
   %prtsc
   pause
end  
%
clg
subplot(221)
semilogx(w,gf);title('Controller F(s)');
xlabel('Rad/Sec'); ylabel('Gain (db)')
subplot(223)
semilogx(w,pf);xlabel('Rad/Sec');ylabel('Phase (deg)')
subplot(222)
semilogx(w,gl); title('Loop TF G*F'); xlabel('Rad/Sec');
%if flag == 2
%   text(0.002,-100,['GM: ',num2str(gmin),' db']);
%else
   text(0.002,-50,['Min. GM: ',num2str(gmin),' db']);
   text(0.002,-150,['Max. GM: ',num2str(gmax),' db']);
%end
subplot(224)
semilogx(w,pl);
xlabel('Rad/Sec (k = 0.5(- -), 1(-), 2(.))');ylabel('Phase (deg)')
%if flag == 2
%   text(0.002,min(pl)+100,['PM: ',num2str(pmin),' deg']);
%else
   text(0.002,max(min(pl))+100,['Min. PM: ',num2str(pmin),' deg']);
   text(0.002,max(min(pl)),['Max. PM: ',num2str(pmax),' deg']);
%end 
%prtsc
pause
% plot the root locus
%accroot
%
clc
disp(' ');
disp(' ');
disp('  * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *')
disp('  *                                                               *')
disp('  *      Your design is accomplished ..                           *');
disp('  *              Controller: (nuf,dnf).                           *');
disp('  *                                                               *');
disp('  * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *')
%
% ----------- End of ACCPLT.M % RYC/MGS %