% TITLE:MVBOUND1.M
% DONE BY: RENO CIPOLLA
% DATE: OCT 1993
% VERSION 2.0 
% REVISED: JAN 1994
% COPYRIGHT 1995 T. Marlin and R. Cipolla
% This file calculates the bound on the manipulated variable and
% stores variables for model mismatch plot.  
% ******************************************************************

% Set all menu variables to global which allows them to be passed from mfile
% to mfile.

%global tau1 tau2 tau3 Kp set deltat dist vp time pv Dyn junk2 rho1 obj distg    
%global tdist D change tset SP t usegrh maxit IAE ISE ITAE tmg boundg violates  
%global cn_t scn_t bound mvtest betaa Kn A useopt td maxdev mindev rise rho chg 
%global epsl p pp bdpos bdneg bd kc ti bgen Xmis vpmis bdmis Kpp ob_j op_ts lim_it  
%global Qp mm theta taulead xi thetad tauleadd xid taud1 taud2 taud3 Kpd x_ ver_sion 
%global c_1 c_2 c_i b_1 b_2 b_i int_kc int_ti o_func le_vel numlev 
%global ss ss1  ss2 ss3 v v1 v2 v3 al_pha save_graph ct_count  

mvtest=0;
sumtau=tau1(p)+tau2(p)+tau3(p);
if change == 1
  cn_t = cn_t + 1;               
  scn_t=1;
  gam=D;
  betaa=tdist;
elseif change == 2 
  scn_t = scn_t + 1;
  cn_t=1;
  gam= - SP;
  betaa=tset;
end

if p==1
 vpmin = -50;
 vpmax = 50;
 bd= zeros(1,maxit +1); 
 bdpos= zeros(1,maxit +1);
 bdneg= zeros(1,maxit +1);
 clear bdmis 
end
 bound= zeros(1,maxit +1);
 tmv= zeros(1,maxit +1);                    
%  bdmis = zeros(max(size(bound)),3);   % set size of matrix  

 clear boundg upper tmg    

if gam ~= 0
Kpp=Kp;
if Kp(p) < 0
  Kpp(p)= - Kp(p);
  gam = -gam;
end
  if gam > 0
   mz= -gam/Kpp(p) - mindev;
  else
   mz= -gam/Kpp(p) + mindev;
  end
end

  j=1;
  k=1;                             % dummy var. for plot
  tmg(k)=betaa -1;                 %       "
  tmv(j)=0;
  upperi=(-gam/Kpp(p))+(-maxdev*gam/Kpp(p) - (-gam/Kpp(p)));
  if gam > 0
   bound(j)= min(upperi,mz);
   boundg(k) = bound(j);
   boundg(k)= max(vpmin,boundg(k));
   boundg(k)= min(vpmax,boundg(k));
  elseif gam < 0
   bound(j) = max(upperi,mz);
   boundg(k) = bound(j);
   boundg(k)= max(vpmin,boundg(k));
   boundg(k)= min(vpmax,boundg(k));
  else  
   bound(j) = 0;
   boundg(k) = 0;
  end
  
  while tmv < time
    j=j+1;

    tmv(j)=tmv(j-1)+deltat;
    if tmv < betaa
      bound(j)= bound(j-1);
    else
     k=k+1;
     tmg(k)=tmg(k-1)+deltat;
     upper=(-gam/Kpp(p))+(-maxdev*gam/Kpp(p) - (-gam/Kpp(p)))*exp(-(tmv(j)-betaa)/(rise*(sumtau+theta(p))));

     if gam > 0
      bound(j)=min(upper,mz);
     else
      bound(j)=max(upper,mz);
     end
     boundg(k)=bound(j);               % boundg used for plot
     boundg(k)=max(vpmin,boundg(k));   % Anti-reset on bound
     boundg(k)=min(vpmax,boundg(k));
    end

  end   % while tmv <
end
  

  % save MV bound for each set of operating
  % conditions for the optimization routine (bd,bdpos,bdneg).
  
  j = 1:1: maxit +1 ;
     if p==1
     
      bd(j)=bound(j);
      bd(j)=max(vpmin,bd(j));           % Anti-reset Windup
      bd(j)=min(vpmax,bd(j));
      bgen=bd;                          % bgen used in integ3A for one call
      bdmis=[bd' bd' bd'] ;                      % saved vector for plot
     elseif p==2                     
      bdpos(j)=bound(j);
      bdpos(j)=max(vpmin,bdpos(j));   
      bdpos(j)=min(vpmax,bdpos(j));
      bgen=bdpos;
      bdmis= [bd' bdpos' bd'];
     elseif p==3
      bdneg(j)=bound(j);
      bdneg(j)=max(vpmin,bdneg(j));
      bdneg(j)=min(vpmax,bdneg(j));
      bgen=bdneg;
      bdmis= [ bd' bdpos' bdneg'];
     end
  


