program main c*********************************************************************72 c cc euler_test() tests euler(). c c Licensing: c c This code is distributed under the MIT license. c c Modified: c c 30 November 2023 c c Author: c c John Burkardt c implicit none integer n call timestamp ( ) write ( *, '(a)' ) '' write ( *, '(a)' ) 'euler_test():' write ( *, '(a)' ) ' FORTRAN90 version' write ( *, '(a)' ) ' Test euler().' n = 50 call euler_humps_test ( n ) c c Terminate. c write ( *, '(a)' ) '' write ( *, '(a)' ) 'euler_test():' write ( *, '(a)' ) ' Normal end of execution.' write ( *, '(a)' ) '' call timestamp ( ) stop ( 0 ) end subroutine euler_humps_test ( n ) c*********************************************************************72 c cc euler_humps_test() tests euler() on the humps ODE. c c Licensing: c c This code is distributed under the MIT license. c c Modified: c c 30 November 2023 c c Author: c c John Burkardt c c Input: c c integer N: the number of steps to take. c implicit none integer, parameter :: m = 1 integer n character ( len = 255 ) command_filename integer command_unit character ( len = 255 ) data_filename integer data_unit character ( len = * ), parameter :: header = 'euler_humps' external humps_deriv integer i double precision t(n+1) double precision tspan(2) double precision y(n+1,1) double precision y0(m) double precision y2(n+1,1) write ( *, '(a)' ) '' write ( *, '(a)' ) 'euler_humps_test():' tspan(1) = 0.0D+00 tspan(2) = 2.0D+00 call humps_exact ( 1, tspan(1), y0 ) call euler ( humps_deriv, tspan, y0, n, m, t, y ) call humps_exact ( n+1, t, y2 ) c c Create the data file. c call get_unit ( data_unit ) data_filename = header // '_data.txt' open ( unit = data_unit, file = data_filename, & status = 'replace' ) do i = 1, n + 1 write ( data_unit, '(3(2x,g14.6))' ) t(i), y(i,1), y2(i,1) end do close ( unit = data_unit ) write ( *, '(a)' ) ' ' write ( *, '(a)' ) ' euler_humps_test: data stored in "' & // trim ( data_filename ) // '".' c c Create the command file. c call get_unit ( command_unit ) command_filename = trim ( 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 ( header ) // '_test.png"' write ( command_unit, '(a)' ) 'set xlabel "<-- T -->"' write ( command_unit, '(a)' ) 'set ylabel "<-- Y(T) -->"' write ( command_unit, '(a)' ) 'set title "euler: Humps ODE"' 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 with lines lw 3 lt rgb "red", \' write ( command_unit, '(a)' ) ' "' & // trim ( data_filename ) // & '" using 1:3 with lines lw 3 lt rgb "blue"' write ( command_unit, '(a)' ) 'quit' close ( unit = command_unit ) write ( *, '(a)' ) & ' euler_humps_test: plot commands stored in "' & // trim ( command_filename ) // '".' return end subroutine humps_deriv ( x, y, yp ) c*****************************************************************************80 c cc humps_deriv() evaluates the right hand side of the humps ODE. c c Discussion: c c y = 1.0 / ( ( x - 0.3 )^2 + 0.01 ) c + 1.0 / ( ( x - 0.9 )^2 + 0.04 ) c - 6.0 c c Licensing: c c This code is distributed under the MIT license. c c Modified: c c 30 November 2023 c c Author: c c John Burkardt c c Input: c c double precision x, y(1): the argument. c c Output: c c double precision yp(1): the value of the derivative at x. c implicit none integer, parameter :: rk = kind ( 1.0D+00 ) double precision x double precision y(1) double precision yp(1) call r8_fake_use ( y(1) ) yp(1) = - 2.0D+00 * ( x - 0.3D+00 ) & / ( ( x - 0.3D+00 )**2 + 0.01D+00 )**2 & - 2.0D+00 * ( x - 0.9D+00 ) & / ( ( x - 0.9D+00 )**2 + 0.04D+00 )**2 return end subroutine humps_exact ( n, x, y ) c*****************************************************************************80 c cc humps_exact() evaluates the humps function. c c Licensing: c c This code is distributed under the MIT license. c c Modified: c c 25 April 2020 c c Author: c c John Burkardt c c Input: c c integer n: the number of evaluation points. c c double precision x(n): the evaluation points. c c Output: c c double precision y(n): the function values. c implicit none integer n double precision x(n) double precision y(n) y = 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 subroutine get_unit ( iunit ) c*********************************************************************72 c cc get_unit() returns a free FORTRAN unit number. c c Discussion: c c A "free" FORTRAN unit number is a value between 1 and 99 which c is not currently associated with an I/O device. A free FORTRAN unit c number is needed in order to open a file with the OPEN command. c c If IUNIT = 0, then no free FORTRAN unit could be found, although c all 99 units were checked (except for units 5, 6 and 9, which c are commonly reserved for console I/O). c c Otherwise, IUNIT is a value between 1 and 99, representing a c free FORTRAN unit. Note that GET_UNIT assumes that units 5 and 6 c are special, and will never return those values. c c Licensing: c c This code is distributed under the MIT license. c c Modified: c c 02 September 2013 c c Author: c c John Burkardt c c Parameters: c c Output, integer IUNIT, the free unit number. c implicit none integer i integer iunit logical value iunit = 0 do i = 1, 99 if ( i .ne. 5 .and. i .ne. 6 .and. i .ne. 9 ) then inquire ( unit = i, opened = value, err = 10 ) if ( .not. value ) then iunit = i return end if end if 10 continue end do return end subroutine r8_fake_use ( x ) c*****************************************************************************80 c cc r8_fake_use() pretends to use an R8 variable. c c Discussion: c c Some compilers will issue a warning if a variable is unused. c Sometimes there's a good reason to include a variable in a program, c but not to use it. Calling this function with that variable as c the argument will shut the compiler up. c c Licensing: c c This code is distributed under the MIT license. c c Modified: c c 19 August 2023 c c Author: c c John Burkardt c c Input: c c double precision x: the variable to be "used". c implicit none double precision x if ( x /= x ) then write ( *, '(a)' ) ' r8_fake_use(): variable is NAN.' end if return end subroutine timestamp ( ) c*********************************************************************72 c cc timestamp() prints out the current YMDHMS date as a timestamp. c c Licensing: c c This code is distributed under the MIT license. c c Modified: c c 12 January 2007 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, month(m), y, h, ':', n, ':', s, '.', mm, ampm return end