Test of the fmin and zeroin functions.
subroutine brent_test() implicit none real(wp) :: r,fzero integer :: iflag real(wp),parameter :: ax = zero real(wp),parameter :: bx = two*pi real(wp),parameter :: tol = 1.0e-6_wp type,extends(brent_class) :: myfunc_type integer :: i = 0 !! function counter end type myfunc_type type(myfunc_type) :: myfunc write(*,*) '' write(*,*) '---------------' write(*,*) ' brent_test' write(*,*) '---------------' write(*,*) '' call myfunc%set_function(sin_func) !set the function !call fmin: ! [the minimum is at 270 deg] myfunc%i = 0 r = myfunc%minimize(ax,bx,tol) write(*,*) 'minimum of sin(x) at: ', r*180.0_wp/pi,' deg' write(*,*) 'number of function calls: ', myfunc%i !call zeroin: ! [the root is at pi] myfunc%i = 0 call myfunc%find_zero(ax+0.0001_wp,bx/two+0.0002,tol,r,fzero,iflag) write(*,*) 'root of sin(x) at: ', r*180.0_wp/pi,' deg' write(*,*) 'number of function calls: ', myfunc%i contains function sin_func(me,x) result(f) !! Example function to minimize: sin(x) implicit none class(brent_class),intent(inout) :: me real(wp),intent(in) :: x real(wp) :: f f = sin(x) select type (me) class is (myfunc_type) me%i = me%i + 1 !number of function calls end select end function sin_func end subroutine brent_test