program main !*****************************************************************************80 ! !! golden_section_test() tests golden_section(). ! ! Licensing: ! ! This code is distributed under the MIT license. ! ! Modified: ! ! 23 February 2026 ! ! Author: ! ! John Burkardt ! implicit none integer, parameter :: rk8 = kind ( 1.0D+00 ) real ( kind = rk8 ) a real ( kind = rk8 ) b real ( kind = rk8 ), external :: humps_fun integer it integer n real ( kind = rk8 ), external :: test_fun real ( kind = rk8 ) x_tol call timestamp ( ) write ( *, '(a)' ) '' write ( *, '(a)' ) 'golden_section_test():' write ( *, '(a)' ) ' Fortran90 version' write ( *, '(a)' ) ' golden_section() seeks a minimizer' write ( *, '(a)' ) ' of a function f(x) in the interval [a,b],' write ( *, '(a)' ) ' assuming f(x) is unimodal over [a,b],' ! ! humps(x) = 1 / ((x-0.3)^2 + 0.01) + 1 / ((x-0.9)^2 + 0.04) - 6.0 ! a = 0.3D+00 b = 0.9D+00 n = 25 x_tol = 0.000001D+00 write ( *, '(a)' ) '' write ( *, '(a)' ) ' f = humps_fun(x)' write ( *, '(a,g14.6,a,g14.6)' ) ' f(', a, ') = ', humps_fun ( a ) write ( *, '(a,g14.6,a,g14.6)' ) ' f(', b, ') = ', humps_fun ( b ) write ( *, '(a,i6)' ) ' iteration limit n = ', n write ( *, '(a,g14.6)' ) ' x_tol = ', x_tol call golden_section ( humps_fun, n, x_tol, a, b, it ) write ( *, '(a)' ) '' write ( *, '(a,i6)' ) ' Number of iterations:', it write ( *, '(a,g14.6,a,g14.6)' ) ' f(', a, ') = ', humps_fun ( a ) write ( *, '(a,g14.6,a,g14.6)' ) ' f(', b, ') = ', humps_fun ( b ) ! ! test_fun(x)=x^4+10*x*sin(x^2) ! a = -2.0D+00 b = 1.0D+00 n = 35 x_tol = 0.000001D+00 write ( *, '(a)' ) '' write ( *, '(a)' ) ' f = test_fun(x)' write ( *, '(a,g14.6,a,g14.6)' ) ' f(', a, ') = ', test_fun ( a ) write ( *, '(a,g14.6,a,g14.6)' ) ' f(', b, ') = ', test_fun ( b ) write ( *, '(a,i6)' ) ' iteration limit n = ', n write ( *, '(a,g14.6)' ) ' x_tol = ', x_tol call golden_section ( test_fun, n, x_tol, a, b, it ) write ( *, '(a)' ) '' write ( *, '(a,i6)' ) ' Number of iterations:', it write ( *, '(a,g14.6,a,g14.6)' ) ' f(', a, ') = ', test_fun ( a ) write ( *, '(a,g14.6,a,g14.6)' ) ' f(', b, ') = ', test_fun ( b ) ! ! Terminate. ! write ( *, '(a)' ) '' write ( *, '(a)' ) 'golden_section_test():' write ( *, '(a)' ) ' Normal end of execution.' call timestamp ( ) stop end function humps_fun ( x ) !*****************************************************************************80 ! !! humps_fun() evaluates a function used for demonstrations. ! ! Discussion: ! ! The "interesting" portion of the function is visible over the range ! 0 <= x <= 2. ! ! Licensing: ! ! This code is distributed under the MIT license. ! ! Modified: ! ! 29 June 2019 ! ! Author: ! ! John Burkardt ! ! Input: ! ! real x(): the evaluation points. ! ! Output: ! ! real y(): the function values. ! implicit none integer, parameter :: rk8 = kind ( 1.0D+00 ) real ( kind = rk8 ) humps_fun real ( kind = rk8 ) x humps_fun = 1.0D+00 / ( ( x - 0.3D+00 )**2 + 0.01D+00 ) & + 1.0D+00 / ( ( x - 0.9D+00 )**2 + 0.04D+00 ) - 6.0D+00 return end function test_fun ( x ) !*****************************************************************************80 ! !! test_fun() evaluates a function used for demonstrations. ! ! Licensing: ! ! This code is distributed under the MIT license. ! ! Modified: ! ! 23 February 2026 ! ! Author: ! ! John Burkardt ! ! Input: ! ! real x(): the evaluation points. ! ! Output: ! ! real y(): the function values. ! implicit none integer, parameter :: rk8 = kind ( 1.0D+00 ) real ( kind = rk8 ) x real ( kind = rk8 ) test_fun test_fun = x**4 + 10.0D+00 * x * sin ( x**2 ) return end subroutine timestamp ( ) !*****************************************************************************80 ! !! timestamp() prints the current YMDHMS date as a time stamp. ! ! Example: ! ! 31 May 2001 9:45:54.872 AM ! ! Licensing: ! ! This code is distributed under the MIT license. ! ! Modified: ! ! 15 August 2021 ! ! Author: ! ! John Burkardt ! implicit none character ( len = 8 ) ampm integer d integer h integer m integer mm character ( len = 9 ), parameter, dimension(12) :: month = (/ & 'January ', 'February ', 'March ', 'April ', & 'May ', 'June ', 'July ', 'August ', & 'September', 'October ', 'November ', 'December ' /) integer n integer s integer values(8) integer y call date_and_time ( values = values ) y = values(1) m = values(2) d = values(3) h = values(5) n = values(6) s = values(7) mm = values(8) if ( h < 12 ) then ampm = 'AM' else if ( h == 12 ) then if ( n == 0 .and. s == 0 ) then ampm = 'Noon' else ampm = 'PM' end if else h = h - 12 if ( h < 12 ) then ampm = 'PM' else if ( h == 12 ) then if ( n == 0 .and. s == 0 ) then ampm = 'Midnight' else ampm = 'AM' end if end if end if write ( *, '(i2.2,1x,a,1x,i4,2x,i2,a1,i2.2,a1,i2.2,a1,i3.3,1x,a)' ) & d, trim ( month(m) ), y, h, ':', n, ':', s, '.', mm, trim ( ampm ) return end