

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% private_STBEG -- Sets some initial parameters for STEPIT. Somewhat archaic and leftover from matlab, a large number of its duties have been			%	
%			  dropped by the current author, being implented elsewhere or by easier ways. Still, I retain the original structure of the program		%
%			  for clarity's sake.																															%
%																																								%
%																																								%	
%			 INPUT QUANTITIES-- FUNK,X,XMAX,XMIN,DELTX,DELMIN,NV,NTRACE,MATRX,MASK,NFMAX,NFLAT,KW									%
%																																								%
%			 OUTPUT QUANTITIES-- NSSW,NACTIVE,NF,KFLAG,NOREP AND SOMETIMES X,XMAX,XMIN,DELTX,DELMIN								%
%																																								%
%			 DOUBLE PRECISION-- X,XMAX,XMIN,DELTX,DELMIN,ERR,FOBJ,DX,XS,DLX,HUGE,DELDF,RZERO,UNITR,RTEN,RELACC,XPLUS,FSAVE					%
%																																								%
%			 INTEGER-- J,JUMP,JVARY,KERFL,KFLAG,KTYPE,KW,MASK,MATRX,NACTIV,NF,NFLAT,NFMAX,NOREP,NSSW,NTRACE,NV,NVMAX,NXTRA					%
%																																								%
%			 **STBEG IS A PRIVATE METHOD CALLED BY STEPIT TO SET INITIAL PARAMETERS														%																														%
%																																								%
% Ported to Matlab by Jason Lott, University of Alabama at Birmingham
% CONTACT:hoffa@uab.edu
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

function a = private_STBEG2(b)	

global X
global XMAX
global XMIN
global DELTX
global DELMIN
global ERR
global FOBJ
global VEC
global DLX
global XS
global FSTORE
global DX
global SALVO
global XOSC
global FOSC
global ARG
global STCUT
global ACK
global FACUP
global RZERO
global XPLUS
global FSAVE
global FBEST
global XSAVE
global ABSDX
global FPREV
global DENOM
global DEL
global DXZ
global DXU
global DFZ
global DFU
global ABSVEC
global SUMV
global CINDER
global COXCOM
global COSIN
global STEPS
global J
global JFLAT
global JFLMIN
global JOCK
global JUMP
global JVARY
global JX
global K
global KERFEL
global KL
global KW
global MASK
global MATRX
global MINSOC
global MAXOSC
global MAXSTP
global NACK
global NACTIV
global NAH
global NCIRC
global NEQUAL
global NF
global NFLAT
global NFMAX
global NFSAV
global NGATE
global NGIANT
global NONZER
global NOREP
global NOSC
global NOUT
global NRETRY
global NSSW
global NSTEPS
global NT
global NTRACE
global NV
global NEXTRA
global NZIP
global tag_380
global tag_390
global wtag
global tag
global continueflag
global T
global Y
global YSIG
global grandtag
global NVMAX
global KFLAG
global DX
global DLX
global XS


HUGE = 1.0D35;																										
DELDF = 0.01D0;																									
RZERO = 0.0D0;
UNITR = 1.0D0;
RTEN = 10.0D0;
XPLUS = 1.1D0;

KFLAG = 0;
NOREP = 0;
KERFL = 0;

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%Check some input quantities, and set them to default values if so desired------------------------------


if NV>=1 & NV<=NVMAX
   
   RELACC = UNITR;	
   
   while XPLUS>UNITR
      
   		RELACC = RELACC/RTEN;																													
      		XPLUS = UNITR+RELACC;
   end
 
   NACTIV =0	;
   
   tag = 0;
   
   for J=1:NV
      
      if MASK(J)~0
         
         tag = 1;
         
      end
      
      while tag == 0
         
         NACTIV = NACTIV + 1;
         
         if DELTX(J) == RZERO
            
            if X(J) == RZERO
               
               DELTX(J) = DELDF;
               
               if DELMIN(J) == RZERO
                  
                  DELMIN(J) = DELTX(J)*RELACC;
                  
               else
                  
                  DELMIN(J) = -DELMIN(J);
                  
               end
               
               if XMAX(J)>XMIN(J)
                  
                  if X(J)>XMAX(J)
                     
                     X(J) = XMAX(J);
                     
                  end
                  
                  if X(J)<XMIN(J)
                     
                     X(J) = XMIN(J);
                     
                  end
                  
               
               else
                  
                  XMAX(J) = HUGE;
                  XMIN(J) = -HUGE;
               
                  if X(J)>XMAX(J)
                     
                     X(J) = XMAX(J);
                     
                  end
                  
                  if X(J)<XMIN(J)
                     
                     X(J) = XMIN(J);
                     
                  end
                  
               end
               
            else
               
               DELTX(J) = DELDF*X(J);
               
               if DELMIN(J) == RZERO
                  
                  DELMIN(J) = DELTX(J)*RELACC;
                  
               else
                  
                  DELMIN(J) = -DELMIN(J);
                  
               end
               
               if XMAX(J)>XMIN(J)
                  
                  if X(J)>XMAX
                     
                     X(J) = XMAX(J);
                     
                  end
                  
                  if X(J)<XMIN(J)
                     
                     X(J) = XMIN(J);
                     
                  end
                  
               else
                  
                  XMAX(J) = HUGE;
                  XMIN(J) = -HUGE;
               
                  if X(J)>XMAX(J)
                     
                     X(J) = XMAX(J);
                     
                  end
                  
                  if X(J)<XMIN(J)
                     
                     X(J) = XMIN(J);
                     
                  end
                  
               end
               
            end
            
         else
            
            XPLUS = X(J) + DELTX(J);
            
            if XPLUS == X(J)
               
               if X(J) == RZERO
                  
                  DELTX(J) = DELDF;
                  
                  if DELMIN(J) == 0
                     
                     DELMIN(J) = DELTX(J)*RELACC;
                     
                  else
                     
                     DELMIN(J) = -DELMIN(J);
                     
                  end
                  
                  if XMAX(J)>XMIN(J)
                     
                     if X(J)>XMAX(J)
                        
                        X(J) = XMAX(J);
                        
                     end
                     
                     if X(J)<XMIN(J)
                        
                        X(J) = XMIN(J);
                        
                     end
                     
                  else
                     
                     XMAX(J) = HUGE;
                     XMIN(J) = -HUGE;
                  
                     if X(J)>XMAX(J)
                        
                        X(J) = XMAX(J);
                        
                     end
                     
                     if X(J)<XMIN(J)
                        
                        X(J) = XMIN(J);
                        
                     end
                     
                  end
                  
               else
                  
                  DELTX(J) = DELDF*X(J);
                  
                  if DELMIN(J) == RZERO
                     
                     DELMIN(J) = DELTX(J)*RELACC;
                     
                  else
                     
                     DELMIN(J) = -DELMIN(J);
                     
                  end
                  
                  if XMAX(J)>XMIN(J)
                     
                     if X(J)>XMAX(J)
                        
                        X(J) = XMAX(J);
                        
                     end
                     
                     if X(J)<XMIN(J)
                        
                        X(J) = XMIN(J);
                        
                     end
                     
                  else
                     
                     XMAX(J) = HUGE;
                     XMIN(J) = -HUGE;
                  
                     if X(J)>XMAX(J)
                        
                        X(J) = XMAX(J);
                        
                     end
                     
                     if X(J) <XMIN(J);
                        
                        X(J) = XMIN(J);
                        
                     end
                     
                  end
                  
               end
               
            else
               
               XPLUS = X(J) - DELTX(J);
               
               if XPLUS ~ X(J)
                  
                  if DELMIN(J) == RZERO
                     
                     DELMIN(J) = DELTX(J)*RELACC;
                     
                  else
                     
                     DELMIN(J) = -DELMIN(J);
                     
                  end
                  
                  if XMAX(J)>XMIN(J)
                     
                     if X(J)>XMAX(J)
                        
                        X(J) = XMAX(J);
                        
                     end
                     
                     if X(J)<XMIN(J)
                        
                        X(J) = XMIN(J);
                        
                     end
                     
                  else
                     
                     XMAX(J) = HUGE;
                     XMIN(J) = -HUGE;
                  
                     if X(J)>XMAX(J)
                        
                        X(J) = XMAX(J);
                        
                     end
                     
                     if X(J)<XMIN(J)
                        
                        X(J) = XMIN(J);
                        
                     end
                     
                  end
                  
               else
                  
                  if X(J) == RZERO
                     
                     DELTX(J) = DELDF;
                     
                     if DELMIN(J) == RZERO
                        
                        DELMIN(J) = DELTX*RELACC;
                        
                     else
                        
                        DELMIN(J) = -DELMIN(J);
                        
                     end
                     
                     if XMAX(J)>XMIN(J)
                        
                        if X(J)>XMAX(J)
                           
                           X(J) = XMAX(J);
                           
                        end
                        
                        if X(J)<XMIN(J)
                           
                           X(J) = XMIN(J);
                           
                        end
                        
                     else
                        
                        XMAX = HUGE;
                        XMIN = -HUGE;
                     
                        if X(J)>XMAX(J)
                           
                           X(J) = XMAX(J);
                           
                        end
                        
                        if X(J)<XMIN(J)
                           
                           X(J) = XMIN(J);
                           
                        end
                        
                     end
                     
                  else
                     
                     DELTX(J) = DELDF*X(J);
                     
                     if DELMIN(J) == RZERO
                        
                        DELMIN(J) = DELTX(J)*RELACC;
                        
                     else
                        
                        DELMIN(J) = -DELMIN(J);
                        
                     end
                     
                     if XMAX(J)>XMIN(J)
                        
                        if X(J)>XMAX(J)
                           
                           X(J) = XMAX(J);
                           
                        end
                        
                        if X(J)<XMIN(J)
                           
                           X(J) = XMIN(J);
                           
                        end
                        
                     else
                        
                        XMAX(J) = HUGE;
                        XMIN(J) = -HUGE;
                     
                        if X(J)>XMAX(J)
                           
                           X(J) = XMAX(J);
                           
                        end
                        
                        if X(J)<XMIN(J)
                           
                           X(J) = XMIN(J);
                           
                        end
                        
                     end
                     
                  end
                  
               end
               
            end
            
         end
         tag = 1; 
      end
      tag = 0;
   end
   

   if NTRACE<0
      
      JVARY = 0;
      private_FUNK(1);
      FSAVE = FOBJ;
      private_FUNK(1);
   
      if NTRACE>=0
         
         disp('FOBJ= '), disp(FOBJ);
         
      end
      
      NF = 2;
      
      if FOBJ == FSAVE
         
         if NACTIV>0
            
            if NTRACE>=0
               
               disp('BEGIN MINIMIZATION.')
               
            else
               
            
            end
            
         else
            
            KFLAG = -1;
            
            if NTRACE >=-1
               
               disp('****WARNING!! MASK(J)~0 FOR ALLJ; FOBJ WILL BE EVALUATED BUT NOT MINIMIZED.');
               
            end	
            
         end
         
      else
         
         NOREP = 1;
         
         if NTRACE>=-1
            
            disp('****WARNING! FOBJ is not a reproducible function of X(J)')
            
         end
         
         if NACTIV>0
            
            if NTRACE>=0
               
               disp('BEGIN MINIMIZATION.')
               
            else
               
            end
            
         else
            
            KFLAG = -1;
            
            if NTRACE>=-1
               
               disp('****WARNING! MASK(J)~0 for all J; FOBJ WILL BE EVALUATED BUT NOT MINIMIZED.')
               
            end
            
         end
         
      end
      
   else
      
      disp('STEPIT INITIAL VALUES: ')
      
      for J = 1:NV
         
         disp('MASK = '), disp(MASK(J))
         disp('X = '), disp(X(J))
         disp('XMAX = '), disp(XMAX(J))
         disp('XMIN = '), disp(XMIN(J))
         disp('DELTX = '), disp(DELTX(J))
         disp('DELMIN = '), disp(DELMIN(J))
      
      end
      
      JVARY = 0
      private_FUNK(1);
      FSAVE = FOBJ;
      private_FUNK(1);
   
      if NTRACE>=0
         
         disp('FOBJ = '), disp(FOBJ)
         
      end
      
      NF = 2;
      
      if FOBJ == FSAVE
         
         if NACTIV>0
            
            if NTRACE>=0
               
               disp('BEGIN MINIMIZATION.')
               
            else
               
            end
            
         
         else
            
            KFLAG = -1;
            
            if NTRACE>=-1
               
               disp('****WARNING! MASK(J)~0 FOR ALL J; FOBJ WILL BE EVALUATED BUT NOT MINIMIZED.')
               
            end
            
         end
         
      else
         
         NOREP = 1;
         
         if NTRACE>=-1
            
            disp('****WARNING! FOBJ IS NOT A REPRODUCIBLE FUNCTION OF X(J)')
            
         end
         
         if NACTIV>0
            
            if NTRACE>=0
               
               disp('BEGIN MINIMIZATION.')
               
            else
               
            end
            
         else
            
            KFLAG = -1;
            
            if NTRACE>=-1
               
               disp('****WARNING! MASK(J)~0 FOR ALL J; FOBJ WILL BE EVALUATED BUT NOT MINIMIZED.')
               
            end
            
         end
         
      end
      
   end
else
   
   error('FATAL ERROR IN SUBROUTINE STEPIT/STBEG; NV=',NV, ' NVMAX=', NVMAX);
   
end
