program main !*****************************************************************************80 ! !! runge_test() tests runge(). ! ! Licensing: ! ! This code is distributed under the MIT license. ! ! Modified: ! ! 25 May 2024 ! ! Author: ! ! John Burkardt ! call timestamp ( ) write ( *, '(a)' ) '' write ( *, '(a)' ) 'runge_test():' write ( *, '(a)' ) ' Fortran90 version' write ( *, '(a)' ) ' Test runge().' call runge_antideriv_test ( ) call runge_deriv_test ( ) call runge_deriv2_test ( ) call runge_fun_test ( ) call runge_power_series_test ( ) ! ! Terminate. ! write ( *, '(a)' ) '' write ( *, '(a)' ) 'runge_test():' write ( *, '(a)' ) ' Normal end of execution.' write ( *, '(a)' ) '' call timestamp ( ) return end subroutine runge_antideriv_test ( ) !*****************************************************************************80 ! !! runge_antideriv_test() tests runge_antideriv(). ! ! Licensing: ! ! This code is distributed under the MIT license. ! ! Modified: ! ! 24 March 2025 ! ! Author: ! ! John Burkardt ! implicit none integer, parameter :: rk8 = kind ( 1.0D+00 ) integer, parameter :: n = 51 real ( kind = rk8 ) a real ( kind = rk8 ) b character ( len = 255 ) header integer i real ( kind = rk8 ) runge_antideriv real ( kind = rk8 ) x(n) real ( kind = rk8 ) y(n) write ( *, '(a)' ) '' write ( *, '(a)' ) 'runge_antideriv_test():' write ( *, '(a)' ) ' runge_antideriv() evaluates the antiderivative' write ( *, '(a)' ) ' of the runge function.' a = -1.0D+00 b = +1.0D+00 call r8vec_linspace ( n, a, b, x ) do i = 1, n y(i) = runge_antideriv ( x(i) ) end do header = 'runge_antideriv' call gnuplot_fx ( n, x, y, trim ( header ) ) return end subroutine runge_deriv_test ( ) !*****************************************************************************80 ! !! runge_deriv_test() tests runge_deriv(). ! ! Licensing: ! ! This code is distributed under the MIT license. ! ! Modified: ! ! 24 March 2025 ! ! Author: ! ! John Burkardt ! implicit none integer, parameter :: rk8 = kind ( 1.0D+00 ) integer, parameter :: n = 51 real ( kind = rk8 ) a real ( kind = rk8 ) b character ( len = 255 ) header integer i real ( kind = rk8 ) runge_deriv real ( kind = rk8 ) x(n) real ( kind = rk8 ) y(n) write ( *, '(a)' ) '' write ( *, '(a)' ) 'runge_deriv_test():' write ( *, '(a)' ) ' runge_deriv() evaluates the derivative' write ( *, '(a)' ) ' of the runge function.' a = -1.0D+00 b = +1.0D+00 call r8vec_linspace ( n, a, b, x ) do i = 1, n y(i) = runge_deriv ( x(i) ) end do header = 'runge_deriv' call gnuplot_fx ( n, x, y, trim ( header ) ) return end subroutine runge_deriv2_test ( ) !*****************************************************************************80 ! !! runge_deriv2_test() tests runge_deriv2(). ! ! Licensing: ! ! This code is distributed under the MIT license. ! ! Modified: ! ! 24 March 2025 ! ! Author: ! ! John Burkardt ! implicit none integer, parameter :: rk8 = kind ( 1.0D+00 ) integer, parameter :: n = 51 real ( kind = rk8 ) a real ( kind = rk8 ) b character ( len = 255 ) header integer i real ( kind = rk8 ) runge_deriv2 real ( kind = rk8 ) x(n) real ( kind = rk8 ) y(n) write ( *, '(a)' ) '' write ( *, '(a)' ) 'runge_deriv2_test():' write ( *, '(a)' ) ' runge_deriv2() evaluates the second derivative' write ( *, '(a)' ) ' of the runge function.' a = -1.0D+00 b = +1.0D+00 call r8vec_linspace ( n, a, b, x ) do i = 1, n y(i) = runge_deriv2 ( x(i) ) end do header = 'runge_deriv2' call gnuplot_fx ( n, x, y, trim ( header ) ) return end subroutine runge_fun_test ( ) !*****************************************************************************80 ! !! runge_fun_test() tests runge_fun(). ! ! Licensing: ! ! This code is distributed under the MIT license. ! ! Modified: ! ! 24 March 2025 ! ! Author: ! ! John Burkardt ! implicit none integer, parameter :: rk8 = kind ( 1.0D+00 ) integer, parameter :: n = 51 real ( kind = rk8 ) a real ( kind = rk8 ) b character ( len = 255 ) header integer i real ( kind = rk8 ) runge_fun real ( kind = rk8 ) x(n) real ( kind = rk8 ) y(n) write ( *, '(a)' ) '' write ( *, '(a)' ) 'runge_fun_test():' write ( *, '(a)' ) ' runge_fun() evaluates the runge function.' a = -1.0D+00 b = +1.0D+00 call r8vec_linspace ( n, a, b, x ) do i = 1, n y(i) = runge_fun ( x(i) ) end do header = 'runge_fun' call gnuplot_fx ( n, x, y, trim ( header ) ) return end subroutine runge_power_series_test ( ) !*****************************************************************************80 ! !! runge_power_series_test() tests runge_power_series(). ! ! Licensing: ! ! This code is distributed under the MIT license. ! ! Modified: ! ! 25 March 2025 ! ! Author: ! ! John Burkardt ! implicit none integer, parameter :: rk8 = kind ( 1.0D+00 ) integer, parameter :: n = 51 real ( kind = rk8 ) a real ( kind = rk8 ) b character ( len = 255 ) header integer i integer n2 real ( kind = rk8 ) runge_fun real ( kind = rk8 ) runge_power_series real ( kind = rk8 ) x(n) real ( kind = rk8 ) y1(n) real ( kind = rk8 ) y2(n) write ( *, '(a)' ) '' write ( *, '(a)' ) 'runge_power_series_test():' write ( *, '(a)' ) ' runge_power_series() evaluates a runge power series.' a = -0.2D+00 b = +0.2D+00 call r8vec_linspace ( n, a, b, x ) n2 = 8 do i = 1, n y1(i) = runge_fun ( x(i) ) y2(i) = runge_power_series ( x(i), n2 ) end do header = 'runge_power_series' call gnuplot_fxfd ( n, x, y1, y2, trim ( header ) ) return end subroutine get_unit ( iunit ) !*****************************************************************************80 ! !! get_unit() returns a free FORTRAN unit number. ! ! Discussion: ! ! A "free" FORTRAN unit number is a value between 1 and 99 which ! is not currently associated with an I/O device. A free FORTRAN unit ! number is needed in order to open a file with the OPEN command. ! ! If IUNIT = 0, then no free FORTRAN unit could be found, although ! all 99 units were checked (except for units 5, 6 and 9, which ! are commonly reserved for console I/O). ! ! Otherwise, IUNIT is a value between 1 and 99, representing a ! free FORTRAN unit. The code assumes that units 5 and 6 ! are special, and will never return those values. ! ! Licensing: ! ! This code is distributed under the MIT license. ! ! Modified: ! ! 26 October 2008 ! ! Author: ! ! John Burkardt ! ! Output: ! ! integer IUNIT, the free unit number. ! implicit none integer i integer ios integer iunit logical lopen iunit = 0 do i = 1, 99 if ( i /= 5 .and. i /= 6 .and. i /= 9 ) then inquire ( unit = i, opened = lopen, iostat = ios ) if ( ios == 0 ) then if ( .not. lopen ) then iunit = i return end if end if end if end do return end subroutine gnuplot_fx ( n, x, y, header ) !*****************************************************************************80 ! !! gnuplot_fx() plots x, f(x) data. ! ! Discussion: ! ! Actually, we simply create two files for processing by gnuplot(). ! ! Licensing: ! ! This code is distributed under the MIT license. ! ! Modified: ! ! 24 March 2025 ! ! Author: ! ! John Burkardt ! implicit none integer, parameter :: rk8 = kind ( 1.0D+00 ) integer n character ( len = 255 ) command_filename integer command_unit character ( len = 255 ) data_filename integer data_unit character ( len = * ) header integer i character ( len = 255 ) png_filename real ( kind = rk8 ) x(n) real ( kind = rk8 ) y(n) ! ! Create a graphics data file. ! data_filename = header // '_data.txt' call get_unit ( data_unit ) open ( unit = data_unit, file = data_filename, status = 'replace' ) do i = 1, n write ( data_unit, '(2g14.6)' ) x(i), y(i) end do close ( unit = data_unit ) write ( *, '(a)' ) ' ' write ( *, '(a)' ) ' Created graphics data file "' & // trim ( data_filename ) // '".' png_filename = header // '.png' ! ! Create graphics command file. ! call get_unit ( command_unit ) command_filename = header // '_commands.txt' open ( unit = command_unit, file = command_filename, status = 'replace' ) write ( command_unit, '(a)' ) '# ' // trim ( command_filename ) write ( command_unit, '(a)' ) '#' write ( command_unit, '(a)' ) '# Usage:' write ( command_unit, '(a)' ) '# gnuplot < ' // trim ( command_filename ) write ( command_unit, '(a)' ) '#' write ( command_unit, '(a)' ) 'set term png' write ( command_unit, '(a)' ) 'set output "' // trim ( png_filename ) // '"' write ( command_unit, '(a)' ) 'set xlabel "<--- X --->"' write ( command_unit, '(a)' ) 'set ylabel "<-- Y(X) -->"' write ( command_unit, '(a)' ) 'set title "' // header // '"' write ( command_unit, '(a)' ) 'set grid' write ( command_unit, '(a)' ) 'set style data lines' write ( command_unit, '(a)' ) 'plot "' // trim ( data_filename ) // & '" using 1:2 lw 3 linecolor rgb "blue"' close ( unit = command_unit ) write ( *, '(a)' ) & ' Created command file "' // trim ( command_filename ) // '".' return end subroutine gnuplot_fxfd ( n, x, y1, y2, header ) !*****************************************************************************80 ! !! gnuplot_fxfd() plots x, f(x) curve and x, y data. ! ! Discussion: ! ! Actually, we simply create two files for processing by gnuplot(). ! ! Licensing: ! ! This code is distributed under the MIT license. ! ! Modified: ! ! 25 March 2025 ! ! Author: ! ! John Burkardt ! implicit none integer, parameter :: rk8 = kind ( 1.0D+00 ) integer n character ( len = 255 ) command_filename integer command_unit character ( len = 255 ) data_filename integer data_unit character ( len = * ) header integer i character ( len = 255 ) png_filename real ( kind = rk8 ) x(n) real ( kind = rk8 ) y1(n) real ( kind = rk8 ) y2(n) ! ! Create a graphics data file. ! data_filename = header // '_data.txt' call get_unit ( data_unit ) open ( unit = data_unit, file = data_filename, status = 'replace' ) do i = 1, n write ( data_unit, '(3g14.6)' ) x(i), y1(i), y2(i) end do close ( unit = data_unit ) write ( *, '(a)' ) ' ' write ( *, '(a)' ) ' Created graphics data file "' & // trim ( data_filename ) // '".' png_filename = header // '.png' ! ! Create graphics command file. ! call get_unit ( command_unit ) command_filename = header // '_commands.txt' open ( unit = command_unit, file = command_filename, status = 'replace' ) write ( command_unit, '(a)' ) '# ' // trim ( command_filename ) write ( command_unit, '(a)' ) '#' write ( command_unit, '(a)' ) '# Usage:' write ( command_unit, '(a)' ) '# gnuplot < ' // trim ( command_filename ) write ( command_unit, '(a)' ) '#' write ( command_unit, '(a)' ) 'set term png' write ( command_unit, '(a)' ) 'set output "' // trim ( png_filename ) // '"' write ( command_unit, '(a)' ) 'set xlabel "<--- X --->"' write ( command_unit, '(a)' ) 'set ylabel "<-- Y(X) -->"' write ( command_unit, '(a)' ) 'set title "' // header // '"' write ( command_unit, '(a)' ) 'set grid' write ( command_unit, '(a)' ) 'set style data lines' write ( command_unit, '(a)' ) 'plot "' // trim ( data_filename ) // & '" using 1:2 lw 3 linecolor rgb "blue",\' write ( command_unit, '(a)' ) ' "' // trim ( data_filename ) // & '" using 1:3 with points pointtype 7 linecolor "red"' close ( unit = command_unit ) write ( *, '(a)' ) & ' Created command file "' // trim ( command_filename ) // '".' return end subroutine r8vec_linspace ( n, a, b, x ) !*****************************************************************************80 ! !! r8vec_linspace() creates a vector of linearly spaced values. ! ! Discussion: ! ! An R8VEC is a vector of R8's. ! ! 4 points evenly spaced between 0 and 12 will yield 0, 4, 8, 12. ! ! In other words, the interval is divided into N-1 even subintervals, ! and the endpoints of intervals are used as the points. ! ! Licensing: ! ! This code is distributed under the MIT license. ! ! Modified: ! ! 14 March 2011 ! ! Author: ! ! John Burkardt ! ! Input: ! ! integer N, the number of entries in the vector. ! ! real ( kind = rk8 ) A, B, the first and last entries. ! ! Output: ! ! real ( kind = rk8 ) X(N), a vector of linearly spaced data. ! implicit none integer, parameter :: rk8 = kind ( 1.0D+00 ) integer n real ( kind = rk8 ) a real ( kind = rk8 ) b integer i real ( kind = rk8 ) x(n) if ( n == 1 ) then x(1) = ( a + b ) / 2.0D+00 else do i = 1, n x(i) = ( real ( n - i, kind = rk8 ) * a & + real ( i - 1, kind = rk8 ) * b ) & / real ( n - 1, kind = rk8 ) end do end if 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