program main !*****************************************************************************80 ! !! cosine_integral_test() tests cosine_integral(). ! ! Licensing: ! ! This code is distributed under the MIT license. ! ! Modified: ! ! 11 August 2025 ! ! Author: ! ! John Burkardt ! implicit none integer, parameter :: rk8 = kind ( 1.0D+00 ) call timestamp ( ) write ( *, '(a)' ) ' ' write ( *, '(a)' ) 'cosine_integral_test():' write ( *, '(a)' ) ' Fortran90 version' write ( *, '(a)' ) ' Test cosine_integral().' call ci_plot ( ) call ci_test ( ) ! ! Terminate. ! write ( *, '(a)' ) ' ' write ( *, '(a)' ) 'cosine_integral_test():' write ( *, '(a)' ) ' Normal end of execution.' write ( *, '(a)' ) ' ' call timestamp ( ) stop 0 end subroutine ci_plot ( ) !*****************************************************************************80 ! !! ci_plot() plots ci(x). ! ! Licensing: ! ! This code is distributed under the MIT license. ! ! Modified: ! ! 11 August 2025 ! ! Author: ! ! John Burkardt ! implicit none integer, parameter :: rk8 = kind ( 1.0D+00 ) real ( kind = rk8 ) ci character ( len = 255 ) command_filename integer command_unit character ( len = 255 ) data_filename integer data_unit integer i integer nplot character ( len = 255 ) output_filename character ( len = 255 ) prefix real ( kind = rk8 ), allocatable :: xplot(:) real ( kind = rk8 ), allocatable :: yplot(:) write ( *, '(a)' ) '' write ( *, '(a)' ) 'ci_plot():' write ( *, '(a)' ) ' Plot the cosine integral ci(x).' nplot = 101 allocate ( xplot(1:nplot) ) allocate ( yplot(1:nplot) ) ! ! Idiotic gnuplot can't handle plot with one essentially infinite value. ! call r8vec_linspace ( nplot, -10.0D+00, +10.0D+00, xplot ) do i = 1, nplot yplot(i) = ci ( xplot(i) ) end do prefix = 'ci' ! ! Create the data file. ! data_filename = trim ( prefix ) // '_data.txt' call get_unit ( data_unit ) open ( unit = data_unit, file = data_filename, status = 'replace' ) do i = 1, nplot write ( data_unit, '(2x,g14.6,2x,g14.6)' ) xplot(i), yplot(i) end do close ( unit = data_unit ) write ( *, '(a)' ) '' write ( *, '(a)' ) & ' Created graphics data file "' // trim ( data_filename ) // '".' ! ! Plot the selected data. ! command_filename = trim ( prefix ) // '_commands.txt' call get_unit ( command_unit ) 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 nokey' output_filename = trim ( prefix ) // '.png' write ( command_unit, '(a)' ) & 'set output "' // trim ( output_filename ) // '"' write ( command_unit, '(a)' ) 'set xlabel "<---X--->"' write ( command_unit, '(a)' ) 'set ylabel "<---Ci(X)--->"' write ( command_unit, '(a)' ) 'set title "Cosine integral function"' write ( command_unit, '(a)' ) 'set grid' write ( command_unit, '(a)' ) 'set style data lines' write ( command_unit, '(a)' ) '$AXIS << EOL' write ( command_unit, '(a)' ) '-10.0 0.0' write ( command_unit, '(a)' ) ' 10.0 0.0' write ( command_unit, '(a)' ) 'EOL' write ( command_unit, '(a)' ) 'plot "' // trim ( data_filename ) & // '" using 1:2 lw 3 linecolor rgb "blue", \' write ( command_unit, '(a)' ) ' $AXIS using 1:2 with lines lw 3 linecolor rgb "black"' close ( unit = command_unit ) write ( *, '(a)' ) & ' Created graphics command file "' // trim ( command_filename ) // '".' ! ! Free memory. ! deallocate ( xplot ) deallocate ( yplot ) 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 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 ci_test ( ) !*****************************************************************************80 ! !! ci_test() tests ci(). ! ! Licensing: ! ! This code is distributed under the MIT license. ! ! Modified: ! ! 11 August 2025 ! ! Author: ! ! John Burkardt ! implicit none integer, parameter :: rk8 = kind ( 1.0D+00 ) real ( kind = rk8 ) ci real ( kind = rk8 ) fx1 real ( kind = rk8 ) fx2 integer n_data real ( kind = rk8 ) x write ( *, '(a)' ) ' ' write ( *, '(a)' ) 'ci_test():' write ( *, '(a)' ) ' ci() evaluates the Cosine Integral function CI(X).' write ( *, '(a)' ) ' ' write ( *, '(a)' ) ' X CI(X)' write ( *, '(a)' ) ' ' n_data = 0 do call ci_values ( n_data, x, fx1 ) if ( n_data == 0 ) then exit end if fx2 = ci ( x ) write ( *, '(2x,g14.6,2x,g24.16,g24.16)' ) x, fx1, fx2 end do return end subroutine ci_values ( n_data, x, fx ) !*****************************************************************************80 ! !! ci_values() returns some values of the cosine integral function. ! ! Discussion: ! ! The cosine integral is defined by ! ! ci ( x ) = - integral ( x <= t < +oo ) ( cos ( t ) ) / t dt ! ! In Mathematica, the function can be evaluated by: ! ! CosIntegral[x] ! ! Licensing: ! ! This code is distributed under the MIT license. ! ! Modified: ! ! 12 August 2004 ! ! Author: ! ! John Burkardt ! ! Reference: ! ! Milton Abramowitz, Irene Stegun, ! Handbook of Mathematical Functions, ! National Bureau of Standards, 1964, ! ISBN: 0-486-61272-4, ! LC: QA47.A34. ! ! Stephen Wolfram, ! The Mathematica Book, ! Fourth Edition, ! Cambridge University Press, 1999, ! ISBN: 0-521-64314-7, ! LC: QA76.95.W65. ! ! Input: ! ! integer N_DATA: The user sets N_DATA to 0 before the first call. ! ! Output: ! ! integer N_DATA: On each call, the routine increments N_DATA by 1, ! and returns the corresponding data; when there is no more data, the ! output value of N_DATA will be 0 again. ! ! real ( kind = rk8 ) X, the argument of the function. ! ! real ( kind = rk8 ) FX, the value of the function. ! implicit none integer, parameter :: rk8 = kind ( 1.0D+00 ) integer, parameter :: n_max = 16 real ( kind = rk8 ) fx real ( kind = rk8 ), save, dimension ( n_max ) :: fx_vec = (/ & -0.1777840788066129D+00, & -0.2227070695927976D-01, & 0.1005147070088978D+00, & 0.1982786159524672D+00, & 0.2760678304677729D+00, & 0.3374039229009681D+00, & 0.4204591828942405D+00, & 0.4620065850946773D+00, & 0.4717325169318778D+00, & 0.4568111294183369D+00, & 0.4229808287748650D+00, & 0.2858711963653835D+00, & 0.1196297860080003D+00, & -0.3212854851248112D-01, & -0.1409816978869304D+00, & -0.1934911221017388D+00 /) integer n_data real ( kind = rk8 ) x real ( kind = rk8 ), save, dimension ( n_max ) :: x_vec = (/ & 0.5D+00, & 0.6D+00, & 0.7D+00, & 0.8D+00, & 0.9D+00, & 1.0D+00, & 1.2D+00, & 1.4D+00, & 1.6D+00, & 1.8D+00, & 2.0D+00, & 2.5D+00, & 3.0D+00, & 3.5D+00, & 4.0D+00, & 4.5D+00 /) if ( n_data < 0 ) then n_data = 0 end if n_data = n_data + 1 if ( n_max < n_data ) then n_data = 0 x = 0.0D+00 fx = 0.0D+00 else x = x_vec(n_data) fx = fx_vec(n_data) 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: ! ! 18 May 2013 ! ! Author: ! ! John Burkardt ! implicit none integer, parameter :: rk8 = kind ( 1.0D+00 ) 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,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