      subroutine GenericOptim(FuncToMinimize,f1dim_FuncToMinimize,
     &ndim,beta,betamin,betamax,fatbeta)
      implicit none
      integer ndim,i
      double precision beta(ndim),betamin(ndim),betamax(ndim),
     &fatbeta
!
      double precision ftol,fatbeta0,beta0(ndim)
      parameter(ftol=1.0d-10)
      external FuncToMinimize,f1dim_FuncToMinimize
      call FuncToMinimize(ndim,beta,fatbeta)
10    fatbeta0=fatbeta
      do i=1,ndim
        beta0(i)=beta(i)
      enddo
      call nongradopt(ndim,FuncToMinimize,f1dim_FuncToMinimize,beta,
     &betamin,betamax,ftol,fatbeta)
      call FuncToMinimize(ndim,beta,fatbeta)
       write(*,*)fatbeta
      call RepeatCompassSearch(ndim,beta,fatbeta,betamin,betamax,
     &FuncToMinimize,f1dim_FuncToMinimize,ftol)
      call FuncToMinimize(ndim,beta,fatbeta)
      write(*,*)fatbeta
      write(*,*)

      if((fatbeta0-fatbeta).gt.ftol)goto 10
      if(fatbeta0.lt.fatbeta)then
        fatbeta=fatbeta0
        do i=1,ndim
          beta(i)=beta0(i)
        enddo
      endif
      return
      end subroutine GenericOptim
!$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
