
%%Created by Jason Lott, CNS SURP, NYU
%%University of Alabama at Birmingham
%%CONTACT: hoffa@uab.edu

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%	STEPIT program by Chandler-- Ported by Jason Lott, UAB																%
%	DATE: June 25 2001																												%
%	Originally in Fortran 77 Copyright J.P. Chandler 1991																%
%																																		%
%  STEPIT FINDS LOCAL MINIMA OF A SMOOTH FUNCTION OF SEVERAL PARAMETERS											%
%																																		%
%  "STEPIT is a phlegmatic method of solving a problem."																%
%										J.H. Burrell, Jr.																			%
%																																		%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%SECTION I: -- VARIABLES		-----------------------------------------------------------------------------
%----------------------------------------------------------------------------------------------------------

%INPUT QUANTITIES -- FUNK, NV, NTRACE, MATRX, MASK, X, XMAX, XMIN, DELTX, DELMIN, NFMAX, KW

%OUTPUT QUANTITIES -- X, FOBJ, ERR, KFLAG, NOREP, KERFL
%
%
%VARIABLE LIST AND DESCRIPTION
%
%	FUNK			-- The name of the method that computes FOBJ given X(1), X(2), ... X(NV)
%					
%	NV 			-- The number of parameters, X
%
%  NTRACE		-- = 0 for normal output
%						= 1 for trace output
%						= -1 for no output
%	
%	MATRX			-- = 0 for no error calculation
%						= 100+M to approximate the errors in the X(J)using steps
%					 	 10**(-M) times ad large as X(J), if nonzero	
%
%	FOBJ			-- The value of the function to be minimized, as computed by FUNK
%
%	MASK(J)		-- Nonzero if X(J) is to be held fixed
%
%	X(J)			-- The J-th parameter
%
%	XMAX(J)		-- The upper limit on X(J)
%
%	XMIN(J)		-- The lower limit on X(J)
%
%	DELTX(J)		-- The initial step size for X(J)
%
%	DELMIN(J)	--	The lower limit (convergence tolerance) on the step size for X(J)
%
%	ERR(J,K) 	-- Returns the error matrix if MATRIX is nonzero (ERR is also used for scratch storage)
%
%	NFMAX			-- The maximum number of function evaluations
%
%	NFLAT			-- Nonzero if the search is to terminate when all trial steps give identical function
%						values. The recommended value of NFLAT is usually NFLAT = 1
%	
%	NXTRA		   -- Not used STEPIT
%
%	KFLAG			-- Returned >0 for a normal exit; returned <0 for an abnormal exit
%
%	NOREP			--	Returned >0 if the function was not reproducible
%
%	KERFL			-- Returned <0 if the method STERR terminated abnormally
%
%	KW				--	The logical unit number of the printer-- NOT USED IN THIS VERSION
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%



function y = STEPIT10(x)					%Dummy argument passed; ignore for now



%Double precision variables-- X,XMAX,XMIN,DELTX,DELMIN,ERR,FOBJ,VEC,DLX,XS,FSTORE,DX,SALVO,XOSC,FOSC,ARG,STCUT,
%										ACK,FACUP
%
%Double precision variables--	RZERO,XPLUS,FSAVE,FBEST,XSAVE,ABSDX,FPREV,DENOM,DEL,DXZ,DXU,DFZ,DFU,ABSVEC,SUMV,
%										CINDER,COXCOM,COSIN,STEPS,ZSQRT,DSQRT
%
%Integer variables			-- J,JFLAT,JFLMIN,JOCK,JUMP,JVARY,JX,K,KERFL,KFLAG,KL,KW,MASK,MATRX,MINOSC,MAXOSC,
%										MAXSTP,NACK,NACTIV,NAH,NCIRC,NEQUAL,NF,NFLAT,NFMAX,NFSAV,NGATE,NGIANT,NONZER,
%										NOREP,NOSC,NOUT,NRETRY,NSSW,NSTEPS,NT,NTRACE,NV,NXTRA,NZIP
%
%
%NOTE: The dimensions of all vectors and matrices are NV, except for ERR(NV,MAXOSC), XOSC(NV,MAXOSC),
%		 FOSC(MAXOSC)
%
%NOTE: For matlab, traditional declarations of 'double', 'single' etc do not apply; variables are declared as
%	    they are needed. For this reason they are only shown as comments above, to be introduced as needed
%		 during program execution
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


%SECTION I.B--User input for Fixed quantities

global NV
global NVMAX
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 KFLAG
global DX
global XS
global DLX
global fortag
global KFLAGENCOUNTER


VEC = zeros(1,NV);
FSTORE = zeros(1,NV);
SALVO = zeros(1,NV);
JFLAT = zeros(1,NV);
XOSC = zeros(NV,MAXOSC);
FOSC = zeros(1,MAXOSC);

%%CALL METHOD STBEG TO FURTHER SET DEFAULT VALUES AND PRINT INITIAL OUTPUT

private_STBEG2(1)									%ARG is dummy

RZERO = 0.0D0;
KERFL = 0;
JOCK =1;												%JOCK is a flag used in setting JVARY
NOSC = 0;												%NOSC = Current depth of zigzagging information
FBEST = FOBJ	;										%FBEST = Best value of FOBJ found so far

for J = 1:NV
   
   DX(J) = DELTX(J);								%DX(J) = Current step size for X(J)
   
end


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
%							SECTION II.	 "And so we begin...."
%
%							
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


if KFLAG<0
   
   JVARY = 0;
   private_FUNK(1);
   NF = NF + 1;
   
   if FBEST<=FSAVE & FOBJ==FBEST
      
      if NTRACE>=0 
         
         disp('FUNCTION COMPUTATIONS:')
         disp('FINAL VALUE OF FOBJ= '), disp(FOBJ);
         disp('FINAL VALUE OF X(J)= ')
         
         for J=1:NV
            
            disp(X(J))
            
         end
         
      end
      
      if KFLAG<0
         
         disp(KFLAG)
         
      elseif MATRX<70 | MATRX>130
         
         disp('MATRIX-OR STATEMENT IN STEPIT REACHED')
         
      else
         
         feval(STERR,dummyvariable);
         
      end
      
      
   else
      
      NOREP = NOREP + 2;
      
      if NTRACE>=-1
         
         disp('****WARNING! FOBJ IS NOT A REPRODUCIBLE FUNCTION OF X(J)')
         
      end
      
      if NTRACE>=0
         
         disp('FUNCTION COMPUTATIONS:')
         disp('FINAL VALUE OF FOBJ= '), disp(FOBJ);
         disp('FINAL VALUE OF X(J)= ')
         
         for J=1:NV
            
            disp(X(J))
            
         end
         
      end
      
      if KFLAG<0
         
         disp(KFLAG);
         
      elseif MATRX<70 | MATRX>130
         
         disp('MATRIX-OR STATEMENT IN STEPIT REACHED')
         
      else feval(STEER, dummyvariable)
         
      end
      
   end
   
else
   
  
   
   %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
   %
   % Vary the parameters, one at a time. This is the starting point used each time the step size is reduced
   % or a successful giant step is completed
   %
   %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
   
   
   NCIRC = 0; 												%Number of consecutive X(JX) without sizable changes
   NZIP = 0;													%Number of consecutive cycles without a giant step
   NACK = 0;													%Number of active X(JX) cycled through
   
   %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
   %
   %MAIN FOR LOOP FOR CYCLING THROUGH THE VARIABLES
   %THE FIRST TRIAL STEP WITH EACH VARIABLE IS SEPARATE
   %
   %
   %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
   
   private_FORLOOP_ROUTINE1(1);
   
end

while KFLAGENCOUNTER == 0
   
   %disp('ENTERING BOTTOM HALF OF PROGRAM AFTER BEING KICKED OUT COMPLETELY!')
   
   if NTRACE>=1
      
      for J =1:NV
         
         disp(VEC(J));
         
      end
      
   end
   
   
   if (NZIP~=0)|(NTRACE<1)
      
      NZIP = NZIP + 1;
      NACK = 0;
      private_FORLOOP_ROUTINE1(1);
      
   else
      
      disp(FBEST),disp(NF)
      
      for J=1:NV
         
         disp(X(J))
         
      end
      
      NZIP = NZIP + 1;
      NACK = 0;
      private_FORLOOP_ROUTINE1(1);
      
   end
   
end

   
      
      


                    
                              
                                    
                                 
                           
                         
                           

                              
                              
                                    
                                    
                              
                                    
                           
                        
                              
                              
                              
                              
                              
                           
                           
                              
                              
                              
                        
                     
                        
                           
                        
                        
                        
                                    
                                 
                                 
                              
                                    
                                       
                                       
                                 
                           
                           
                           
                           			
                                       
                                       
                                       
                                    
                                       
                                                                             
                                          
                                          
                                       
                                    
                                       
                                       												
                                                                              
                                                                              
                                                                              
                                                                              
                                       
                                       
                                       
                                       

                                          
                                          
                                          
                                       
                                       
                                                   
                                                   
                                                   
                                                   
                                                   
                                                   
                                                   
                                                   
                                                   
                                                   
                                                   
                                                   
                                                   
                                                   
                                                   
                                                   
                                                  

 
 


                                                      
                                                      
                                                    
                                                 
                                                          
                                                       
                                                       
                                                          
                                                             
                                                    
                                              
                                  
                                     
                                     
                                                    
                                     
                                        
                                        
                                        
                                  
                               
                               
                               
                                  
                                  
                      
                   
                   
                      
                   
                      

                         
                         
                            
                            
                        
                        
                     
                     
                        
                        
                     
                     

                        
                        

                        
                        
                        
                        
                        
                                             
                        
                           
                              

                     

                     
                     
                     
                     
                     

                        
                                 
                                 
                         
                              
                              
                                 
                                 
                              
                              
                            
                           
                           
                           
                           
                           
                           
                                 
                                 
                                 
                                 
         
          
   
      
   
         
            


         
         
   
   
   
      
      
      
      
      
            
         
         
         
         
            
            
         
         
 
         
                 
  

             
               
               
                     
                    
                 
                  
              
                  
            
         
            
            
         
   
   
      
      


















