!+ Subroutine to perform a search by golden sections.
!
! Method:
!	This is an implementation of the algorithm given in "Numerical
!	Recipes" by D. E. Knuth.
!
! Current Owner of Code: J. M. Edwards
!
! History:
!	 Version 	   Date	  		Comment
!   
!	 1.0		   12-04-95		First Version under RCS
!						(J. M. Edwards)
!
! Description of Code:
!   FORTRAN 77 with extensions listed in documentation. 
!
!- ---------------------------------------------------------------------
      FUNCTION GOLDEN(K1, K2, K3, K_MINIMUM
     &   , N_DATA
     &   , WEIGHT, RESIDUAL, U
     &   )
!
!
!
      IMPLICIT NONE
!
!
!     INCLUDE HEADER FILES.
      INCLUDE 'incl/prec_machine.ucf'
      INCLUDE 'incl/precision_esft.tcf'
      INCLUDE 'incl/dimensions_spec.ucf'
      INCLUDE 'incl/dimensions_pp.ucf'
!
!     DUMMY ARGUMENTS
      INTEGER 	!, INTENT(IN)
     &     N_DATA                           
!             NUMBER OF DATA POINTS
      REAL*8	!, INTENT(OUT)
     &     GOLDEN                           
!             GOLDEN SECTION SEARCH
      REAL*8 	!, INTENT(IN)
     &     K1                               
!             TRIPLE BRACKETING MINIMUM
     &   , K2
!             TRIPLE BRACKETING MINIMUM
     &   , K3
!             TRIPLE BRACKETING MINIMUM
     &   , WEIGHT(NPD_AMOUNT+1)         
!             WEIGHTS FOR TERMS
     &   , RESIDUAL(NPD_AMOUNT+1)       
!             RESIDUALS
     &   , U(NPD_AMOUNT+1)              
!             AMOUNTS OF ABSORBER
      REAL*8 	!, INTENT(OUT)
     &     K_MINIMUM                        
!             POSITION OF MINIMUM
!
!     LOCAL VARIABLES.
      REAL*8
     &     GOLDEN_RATIO                     
!             GOLDEN RATIO
     &   , GOLDEN_COMPLEMENT                
!             COMPLEMENT OF GOLDEN RATIO
     &   , X0                               
!             SEARCHING VARIABLE
     &   , X1
!             SEARCHING VARIABLE
     &   , X2
!             SEARCHING VARIABLE
     &   , X3
!             SEARCHING VARIABLE
     &   , F0                               
!             EVALUATED FUNCTION
     &   , F1
!             EVALUATED FUNCTION
     &   , F2
!             EVALUATED FUNCTION
     &   , F3
!             EVALUATED FUNCTION
!
!     FUNCTIONS CALLED:
      REAL*8
     &     RESIDUAL_POLYNOMIAL
!             VALUE OF RESIDUAL POLYNOMIAL
      EXTERNAL
     &     RESIDUAL_POLYNOMIAL
!
      PARAMETER (GOLDEN_RATIO=6.1803399D-01
     &   , GOLDEN_COMPLEMENT=1.0D+00-GOLDEN_RATIO)
!
!
!
!     MAKE [X0,X1] THE SMALLER SEGMENT AND FILL IN THE NEW POINT TO
!     BE TESTED, THIS BEING X2.
      X0=K1
      X3=K3
      IF (ABS(K3-K2).GT.ABS(K2-K1)) THEN
         X1=K2
         X2=K2+GOLDEN_COMPLEMENT*(K3-K2)
      ELSE
         X2=K2
         X1=K2-GOLDEN_COMPLEMENT*(K2-K1)
      ENDIF
!
!     EVALUATE THE FUNCTION AT THE INNER POINTS.
      F1=RESIDUAL_POLYNOMIAL(N_DATA, WEIGHT, RESIDUAL, U, K1)
      F2=RESIDUAL_POLYNOMIAL(N_DATA, WEIGHT, RESIDUAL, U, K2)
1     IF (ABS(X3-X0).GT.TOL_MIN*(ABS(X1)+ABS(X2))) THEN
         IF (F2.LT.F1) THEN
            X0=X1
            X1=X2
            X2=GOLDEN_RATIO*X1+GOLDEN_COMPLEMENT*X3
            F0=F1
            F1=F2
            F2=RESIDUAL_POLYNOMIAL(N_DATA, WEIGHT, RESIDUAL, U, X2)
         ELSE
            X3=X2
            X2=X1
            X1=GOLDEN_RATIO*X2+GOLDEN_COMPLEMENT*X0
            F3=F2
            F2=F1
            F1=RESIDUAL_POLYNOMIAL(N_DATA, WEIGHT, RESIDUAL, U, X1)
         ENDIF
!        RETURN TO 1 TO TEST INTERVAL.
         GOTO 1
      ENDIF
!
!     CONVERGENCE HAS BEEN ACHIEVED.
      IF (F1.LT.F2) THEN
         GOLDEN=F1
         K_MINIMUM=X1
      ELSE
         GOLDEN=F2
         K_MINIMUM=X2
      ENDIF
!
!
      RETURN
      END
