program main c*********************************************************************72 c cc zero_itp_test() tests zero_itp(). c c Licensing: c c This code is distributed under the MIT license. c c Modified: c c 03 March 2024 c c Author: c c John Burkardt c implicit none double precision a double precision b double precision epsi double precision f_01 external f_01 double precision f_02 external f_02 double precision f_03 external f_03 double precision f_04 external f_04 double precision f_05 external f_05 double precision f_06 external f_06 double precision k1 double precision k2 integer n0 character * ( 80 ) title logical verbose write ( *, '(a)' ) ' ' call timestamp ( ) write ( *, '(a)' ) 'zero_itp_test():' write ( *, '(a)' ) ' Fortran77 version.' write ( *, '(a)' ) ' Test zero_itp(), which seeks a root of' write ( *, '(a)' ) ' a function f(x) in an interval [A,B].' a = 1.0D+00 b = 2.0D+00 epsi = sqrt ( epsilon ( 1.0D+00 ) ) k1 = 1.0D+00 / ( b - a ) / 5.0D+00 k2 = 2.0D+00 n0 = 1 verbose = .false. title = 'f_01(x) = sin ( x ) - x / 2' call zero_itp_example ( f_01, a, b, epsi, k1, k2, n0, verbose, & title ) a = 0.0D+00 b = 1.0D+00 epsi = sqrt ( epsilon ( 1.0D+00 ) ) k1 = 1.0D+00 / ( b - a ) / 5.0D+00 k2 = 2.0D+00 n0 = 1 verbose = .false. title = 'f_02(x) = 2 * x - exp ( - x )' call zero_itp_example ( f_02, a, b, epsi, k1, k2, n0, verbose, & title ) a = -1.0D+00 b = 0.5D+00 epsi = sqrt ( epsilon ( 1.0D+00 ) ) k1 = 1.0D+00 / ( b - a ) / 5.0D+00 k2 = 2.0D+00 n0 = 1 verbose = .false. title = 'f_03(x) = x * exp ( - x )' call zero_itp_example ( f_03, a, b, epsi, k1, k2, n0, verbose, & title ) a = 0.0001D+00 b = 20.0D+00 epsi = sqrt ( epsilon ( 1.0D+00 ) ) k1 = 1.0D+00 / ( b - a ) / 5.0D+00 k2 = 2.0D+00 n0 = 1 verbose = .false. title = 'f_04(x) = exp ( x ) - 1 / ( 100 * x * x )' call zero_itp_example ( f_04, a, b, epsi, k1, k2, n0, verbose, & title ) a = -5.0D+00 b = 2.0D+00 epsi = sqrt ( epsilon ( 1.0D+00 ) ) k1 = 1.0D+00 / ( b - a ) / 5.0D+00 k2 = 2.0D+00 n0 = 1 verbose = .false. title = 'f_05(x) = (x+3) * (x-1) * (x-1)' call zero_itp_example ( f_05, a, b, epsi, k1, k2, n0, verbose, & title ) a = 1.0D+00 b = 2.0D+00 epsi = 0.0005D+00 k1 = 1.0D+00 / ( b - a ) / 5.0D+00 k2 = 2.0D+00 n0 = 1 verbose = .false. title = 'f_06(x) = x^3 - x - 2' call zero_itp_example ( f_06, a, b, epsi, k1, k2, n0, verbose, & title ) c c Terminate. c write ( *, '(a)' ) ' ' write ( *, '(a)' ) 'zero_itp_test():' write ( *, '(a)' ) ' Normal end of execution.' write ( *, '(a)' ) ' ' call timestamp ( ) stop 0 end subroutine zero_itp_example ( f, a, b, epsi, k1, k2, n0, & verbose, title ) c*********************************************************************72 c cc zero_itp_example() tests zero_itp() on one test function. c c Licensing: c c This code is distributed under the MIT license. c c Modified: c c 03 March 2024 c c Author: c c John Burkardt c c Input: c c external double precision F, the name of a user-supplied c function, of the form "FUNCTION F ( X )", which evaluates the c function whose zero is being sought. c c double precision A, B, the endpoints of the change of sign interval. c c double precision epsi: error tolerance between exact and computed roots. c c double precision k1: a parameter, with suggested value 0.2 / ( b - a ). c c double precision k2: a parameter, typically set to 2. c c integer n0: a parameter that can be set to 0 for difficult problems, c but is usually set to 1, to take more advantage of the secant method. c c logical verbose: if true, requests additional output from zero_itp(). c c character ( len = * ) TITLE, a title for the problem. c implicit none double precision a double precision b integer calls double precision epsi double precision f external f double precision fa double precision fb double precision fz double precision k1 double precision k2 integer n0 character * ( * ) title logical verbose double precision z call zero_itp ( f, a, b, epsi, k1, k2, n0, verbose, z, fz, calls ) fa = f ( a ) fb = f ( b ) write ( *, '(a)' ) ' ' write ( *, '(2x,a)' ) trim ( title ) write ( *, '(a)' ) ' ' write ( *, '(a)' ) ' A Z B' write ( *, '(a)' ) ' F(A) F(Z) F(B)' write ( *, '(a)' ) ' ' write ( *, '(2x,f14.8,2x,f14.8,2x,f14.8)' ) a, z, b write ( *, '(2x,g14.6,2x,g14.6,2x,g14.6)' ) fa, fz, fb write ( *, '(a,i8)' ) ' Number of calls to F = ', calls write ( *, '(a,g14.6)' ) ' Tolerance epsi = ', epsi write ( *, '(a,g14.6,a,g14.6,a,i4)' ) & ' Parameters k1 =', k1, ', k2 = ', k2, ', n0 = ', n0 return end function f_01 ( x ) c*********************************************************************72 c cc f_01() evaluates sin ( x ) - x / 2. c c Licensing: c c This code is distributed under the MIT license. c c Modified: c c 07 March 1999 c c Author: c c John Burkardt c c Input: c c double precision X, the evaluation point. c c Output: c c double precision F_01, the value of the function at X. c implicit none double precision f_01 double precision x f_01 = sin ( x ) - 0.5D+00 * x return end function f_02 ( x ) c*********************************************************************72 c cc f_02() evaluates 2*x-exp(-x). c c Licensing: c c This code is distributed under the MIT license. c c Modified: c c 07 March 1999 c c Author: c c John Burkardt c c Input: c c double precision X, the evaluation point. c c Output: c c double precision F_02, the value of the function at X. c implicit none double precision f_02 double precision x f_02 = 2.0D+00 * x - exp ( - x ) return end function f_03 ( x ) c*********************************************************************72 c cc f_03() evaluates x*exp(-x). c c Modified: c c 07 March 1999 c c Author: c c John Burkardt c c Input: c c double precision X, the evaluation point. c c Output: c c double precision F_03, the value of the function at X. c implicit none double precision f_03 double precision x f_03 = x * exp ( - x ) return end function f_04 ( x ) c*********************************************************************72 c cc f_04() evaluates exp(x) - 1 / (100*x*x). c c Licensing: c c This code is distributed under the MIT license. c c Modified: c c 07 March 1999 c c Author: c c John Burkardt c c Input: c c double precision X, the evaluation point. c c Output: c c double precision F_04, the value of the function at X. c implicit none double precision f_04 double precision x f_04 = exp ( x ) - 1.0D+00 / 100.0D+00 / x / x return end function f_05 ( x ) c*********************************************************************72 c cc f_05() evaluates (x+3)*(x-1)*(x-1). c c Licensing: c c This code is distributed under the MIT license. c c Modified: c c 07 March 1999 c c Author: c c John Burkardt c c Input: c c double precision X, the evaluation point. c c Output: c c double precision F_05, the value of the function at X. c implicit none double precision f_05 double precision x f_05 = ( x + 3.0D+00 ) * ( x - 1.0D+00 ) * ( x - 1.0D+00 ) return end function f_06 ( x ) c*********************************************************************72 c cc f_06() evaluates x^3 - x - 2. c c Licensing: c c This code is distributed under the MIT license. c c Modified: c c 03 March 2024 c c Author: c c John Burkardt c c Input: c c double precision X, the evaluation point. c c Output: c c double precision F_06, the value of the function at X. c implicit none double precision f_06 double precision x f_06 = x ** 3 - x - 2.0D+00 return end subroutine timestamp ( ) c*********************************************************************72 c cc timestamp() prints the YMDHMS date as a timestamp. c c Licensing: c c This code is distributed under the MIT license. c c Modified: c c 12 June 2014 c c Author: c c John Burkardt c implicit none character * ( 8 ) ampm integer d character * ( 8 ) date integer h integer m integer mm character * ( 9 ) month(12) integer n integer s character * ( 10 ) time integer y save month data month / & 'January ', 'February ', 'March ', 'April ', & 'May ', 'June ', 'July ', 'August ', & 'September', 'October ', 'November ', 'December ' / call date_and_time ( date, time ) read ( date, '(i4,i2,i2)' ) y, m, d read ( time, '(i2,i2,i2,1x,i3)' ) h, n, s, mm if ( h .lt. 12 ) then ampm = 'AM' else if ( h .eq. 12 ) then if ( n .eq. 0 .and. s .eq. 0 ) then ampm = 'Noon' else ampm = 'PM' end if else h = h - 12 if ( h .lt. 12 ) then ampm = 'PM' else if ( h .eq. 12 ) then if ( n .eq. 0 .and. s .eq. 0 ) then ampm = 'Midnight' else ampm = 'AM' end if end if end if write ( *, & '(i2,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