subroutine brent(func,xmin,brack,tol,niter)
interface
function func(x)
real(8) :: x
real(8) :: func
end function func
end interface
real(8),intent(inout) :: xmin
real(8),dimension(:),optional :: brack
real(8),optional :: tol
integer,optional :: niter
real(8) :: tol_
integer :: niter_
integer :: iter
real(8) :: ax,xx,bx,fa,fx,fb,fret
!
tol_=1d-9;if(present(tol))tol_=tol
Niter_=200;if(present(Niter))Niter_=Niter
!
if(present(brack))then
select case(size(brack))
case(1)
stop "Brent error: calling brent with size(brack)==1. None or two points are necessary."
case(2)
ax = brack(1)
xx = brack(2)
call bracket(ax,xx,bx,fa,fx,fb,func)
case (3)
ax = brack(1)
xx = brack(2)
bx = brack(3)
end select
else
ax=0d0
xx=1d0
call bracket(ax,xx,bx,fa,fx,fb,func)
endif
fret=brent_optimize(ax,xx,bx,func,tol_,niter_,xmin)
end subroutine brent