program main !*****************************************************************************80 ! !! gaussian_test() tests gaussian(). ! ! Licensing: ! ! This code is distributed under the MIT license. ! ! Modified: ! ! 07 July 2025 ! ! Author: ! ! John Burkardt ! implicit none write ( *, '(a)' ) '' write ( *, '(a)' ) 'gaussian_test():' write ( *, '(a)' ) ' Fortran90 version' write ( *, '(a)' ) ' gaussian() evaluates a Gaussian function,' write ( *, '(a)' ) ' its antiderivative, or derivatives of any order.' call gaussian_antideriv_test ( ) call gaussian_n_test ( ) call gaussian_mu_test ( ) call gaussian_sigma_test ( ) ! ! Terminate. ! write ( *, '(a)' ) '' write ( *, '(a)' ) 'gaussian_test()' write ( *, '(a)' ) ' Normal end of execution.' call timestamp ( ) return end subroutine gaussian_antideriv_test ( ) !*****************************************************************************80 ! !! gaussian_antideriv_test() tests gaussian_antideriv(). ! ! Licensing: ! ! This code is distributed under the MIT license. ! ! Modified: ! ! 03 July 2025 ! ! Author: ! ! John Burkardt ! implicit none integer, parameter :: rk8 = kind ( 1.0D+00 ) real ( kind = rk8 ) a real ( kind = rk8 ) b character ( len = 255 ) command_filename integer command_unit character ( len = 255 ) data_filename integer data_unit real ( kind = rk8 ) gaussian_antideriv character ( len = 80 ) header integer i real ( kind = rk8 ) mu integer n character ( len = 255 ) png_filename real ( kind = rk8 ) sigma real ( kind = rk8 ), allocatable :: x(:) real ( kind = rk8 ), allocatable :: y(:) write ( *, '(a)' ) '' write ( *, '(a)' ) 'gaussian_antideriv_test():' write ( *, '(a)' ) ' Compute gaussian_antideriv(mu=0,sigma=1x).' write ( *, '(a)' ) '' mu = 0.0D+00 sigma = 1.0D+00 a = - 4.0D+00 b = + 4.0D+00 n = 101 allocate ( x(1:n) ) call r8vec_linspace ( n, a, b, x ) allocate ( y(1:n) ) do i = 1, n y(i) = gaussian_antideriv ( mu, sigma, x(i) ) end do header = 'gaussian_antideriv' ! ! Create a graphics data file. ! data_filename = trim ( 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 = trim ( header ) // '.png' ! ! Create graphics command file. ! 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 ( png_filename ) // '"' write ( command_unit, '(a)' ) 'set xlabel "<--- X --->"' write ( command_unit, '(a)' ) 'set ylabel "<-- Y(X) -->"' write ( command_unit, '(a)' ) 'set title "g\\_antideriv(mu=0,sigma=1;x)"' 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 ) // '".' deallocate ( x ) deallocate ( y ) return end subroutine gaussian_mu_test ( ) !*****************************************************************************80 ! !! gaussian_mu() varies the mu parameter. ! ! Licensing: ! ! This code is distributed under the MIT license. ! ! Modified: ! ! 03 July 2025 ! ! Author: ! ! John Burkardt ! implicit none integer, parameter :: rk8 = kind ( 1.0D+00 ) real ( kind = rk8 ) a real ( kind = rk8 ) b character ( len = 255 ) command_filename integer command_unit character ( len = 255 ) data_filename integer data_unit real ( kind = rk8 ) gaussian_value character ( len = 80 ) header integer i integer j real ( kind = rk8 ), save, dimension ( 5 ) :: mu = (/ & -1.0D+00, -0.5D+00, 0.0D+00, 0.5D+00, 1.0D+00 /) integer n integer n_deriv character ( len = 255 ) png_filename real ( kind = rk8 ) sigma real ( kind = rk8 ), allocatable :: x(:) real ( kind = rk8 ), allocatable :: y(:,:) write ( *, '(a)' ) '' write ( *, '(a)' ) 'gaussian_sigma_test():' write ( *, '(a)' ) ' Compute gaussian(n,mu,sigma;x) for various values of mu.' write ( *, '(a)' ) '' n_deriv = 0 sigma = 1.0D+00 a = - 4.0D+00 b = + 4.0D+00 n = 101 allocate ( x(1:n) ) call r8vec_linspace ( n, a, b, x ) allocate ( y(1:n,1:5) ) header = 'gaussian_mu' ! ! Create a graphics data file. ! data_filename = trim ( header ) // '_data.txt' call get_unit ( data_unit ) open ( unit = data_unit, file = data_filename, status = 'replace' ) do i = 1, n do j = 1, 5 y(i,j) = gaussian_value ( n_deriv, mu(j), sigma, x(i) ) end do write ( data_unit, '(6g14.6)' ) x(i), y(i,1:5) end do close ( unit = data_unit ) write ( *, '(a)' ) ' ' write ( *, '(a)' ) ' Created graphics data file "' & // trim ( data_filename ) // '".' png_filename = trim ( header ) // '.png' ! ! Create graphics command file. ! 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 ( png_filename ) // '"' write ( command_unit, '(a)' ) 'set xlabel "<--- X --->"' write ( command_unit, '(a)' ) 'set ylabel "<-- Y(X) -->"' write ( command_unit, '(a)' ) 'set title "g(mu=[-1,-1/2,0,1/2,1],sigma;x)"' 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 lw 3 linecolor rgb "red",\' write ( command_unit, '(a)' ) ' "' // trim ( data_filename ) // & '" using 1:4 lw 3 linecolor rgb "green",\' write ( command_unit, '(a)' ) ' "' // trim ( data_filename ) // & '" using 1:5 lw 3 linecolor rgb "cyan",\' write ( command_unit, '(a)' ) ' "' // trim ( data_filename ) // & '" using 1:6 lw 3 linecolor rgb "magenta"' close ( unit = command_unit ) write ( *, '(a)' ) & ' Created command file "' // trim ( command_filename ) // '".' ! ! Free memory. ! deallocate ( x ) deallocate ( y ) return end subroutine gaussian_n_test ( ) !*****************************************************************************80 ! !! gaussian_n_test() varies the n parameter. ! ! Licensing: ! ! This code is distributed under the MIT license. ! ! Modified: ! ! 03 July 2025 ! ! Author: ! ! John Burkardt ! implicit none integer, parameter :: rk8 = kind ( 1.0D+00 ) real ( kind = rk8 ) a real ( kind = rk8 ) b character ( len = 255 ) command_filename integer command_unit character ( len = 255 ) data_filename integer data_unit integer deriv real ( kind = rk8 ) gaussian_value character ( len = 80 ) header integer i real ( kind = rk8 ) mu character ( len = 255 ) png_filename real ( kind = rk8 ) sigma integer :: n = 101 real ( kind = rk8 ), allocatable :: x(:) real ( kind = rk8 ), allocatable :: y(:) write ( *, '(a)' ) '' write ( *, '(a)' ) 'gaussian_n_test():' write ( *, '(a)' ) ' gaussian(n,mu,sigma;x) evaluates the nth derivative.' write ( *, '(a)' ) '' mu = 0.0 sigma = 1.0 a = - 4.0 b = + 4.0 allocate ( x(1:n ) ) call r8vec_linspace ( n, a, b, x ) allocate ( y(1:n) ) header = 'gaussian_deriv_0' do deriv = 0, 5 do i = 1, n y(i) = gaussian_value ( deriv, mu, sigma, x(i) ) end do ! ! Create a graphics data file. ! data_filename = trim ( 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 = trim ( header ) // '.png' ! ! Create graphics command file. ! 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 ( 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 ) // '".' call filename_inc ( header ) end do deallocate ( x ) deallocate ( y ) return end subroutine gaussian_sigma_test ( ) !*****************************************************************************80 ! !! gaussian_sigma_test() varies the sigma parameter. ! ! Licensing: ! ! This code is distributed under the MIT license. ! ! Modified: ! ! 03 July 2025 ! ! Author: ! ! John Burkardt ! implicit none integer, parameter :: rk8 = kind ( 1.0D+00 ) real ( kind = rk8 ) a real ( kind = rk8 ) b character ( len = 255 ) command_filename integer command_unit character ( len = 255 ) data_filename integer data_unit real ( kind = rk8 ) gaussian_value character ( len = 80 ) header integer i integer j real ( kind = rk8 ) mu integer n integer n_deriv character ( len = 255 ) png_filename real ( kind = rk8 ), save, dimension ( 5 ) :: sigma = (/ & 0.25D+00, 0.50D+00, 1.0D+00, 1.5D+00, 2.0D+00 /) real ( kind = rk8 ), allocatable :: x(:) real ( kind = rk8 ), allocatable :: y(:,:) write ( *, '(a)' ) '' write ( *, '(a)' ) 'gaussian_sigma_test():' write ( *, '(a)' ) ' Compute gaussian(n,mu,sigma,x) for various values of sigma.' write ( *, '(a)' ) '' n_deriv = 0 mu = 0.0D+00 a = - 4.0D+00 b = + 4.0D+00 n = 101 allocate ( x(1:n) ) call r8vec_linspace ( n, a, b, x ) allocate ( y(1:n,1:5) ) header = 'gaussian_sigma' ! ! Create a graphics data file. ! data_filename = trim ( header ) // '_data.txt' call get_unit ( data_unit ) open ( unit = data_unit, file = data_filename, status = 'replace' ) do i = 1, n do j = 1, 5 y(i,j) = gaussian_value ( n_deriv, mu, sigma(j), x(i) ) end do write ( data_unit, '(6g14.6)' ) x(i), y(i,1:5) end do close ( unit = data_unit ) write ( *, '(a)' ) ' ' write ( *, '(a)' ) ' Created graphics data file "' & // trim ( data_filename ) // '".' png_filename = trim ( header ) // '.png' ! ! Create graphics command file. ! 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 ( png_filename ) // '"' write ( command_unit, '(a)' ) 'set xlabel "<--- X --->"' write ( command_unit, '(a)' ) 'set ylabel "<-- Y(X) -->"' write ( command_unit, '(a)' ) 'set title "g(mu,sigma=[0.25,0.50,1,1.5,2];x)"' 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 lw 3 linecolor rgb "red",\' write ( command_unit, '(a)' ) ' "' // trim ( data_filename ) // & '" using 1:4 lw 3 linecolor rgb "green",\' write ( command_unit, '(a)' ) ' "' // trim ( data_filename ) // & '" using 1:5 lw 3 linecolor rgb "cyan",\' write ( command_unit, '(a)' ) ' "' // trim ( data_filename ) // & '" using 1:6 lw 3 linecolor rgb "magenta"' close ( unit = command_unit ) write ( *, '(a)' ) & ' Created command file "' // trim ( command_filename ) // '".' ! ! Free memory. ! deallocate ( x ) deallocate ( y ) return end subroutine filename_inc ( file_name ) !*****************************************************************************80 ! !! filename_inc() increments a partially numeric filename. ! ! Discussion: ! ! It is assumed that the digits in the name, whether scattered or ! connected, represent a number that is to be increased by 1 on ! each call. If this number is all 9's on input, the output number ! is all 0's. Non-numeric letters of the name are unaffected. ! ! If the name is empty, then the routine stops. ! ! If the name contains no digits, the empty string is returned. ! ! Example: ! ! Input Output ! ----- ------ ! 'a7to11.txt' 'a7to12.txt' ! 'a7to99.txt' 'a8to00.txt' ! 'a9to99.txt' 'a0to00.txt' ! 'cat.txt' ' ' ! ' ' STOP! ! ! Licensing: ! ! This code is distributed under the MIT license. ! ! Modified: ! ! 14 September 2005 ! ! Author: ! ! John Burkardt ! ! Input: ! ! character ( len = * ) FILE_NAME: a string to be incremented. ! ! Output: ! ! character ( len = * ) FILE_NAME, the incremented string. ! implicit none character c integer change integer digit character ( len = * ) file_name integer i integer lens lens = len_trim ( file_name ) if ( lens <= 0 ) then write ( *, '(a)' ) ' ' write ( *, '(a)' ) 'file_name_inc(): Fatal error!' write ( *, '(a)' ) ' The input string is empty.' stop end if change = 0 do i = lens, 1, -1 c = file_name(i:i) if ( lge ( c, '0' ) .and. lle ( c, '9' ) ) then change = change + 1 digit = ichar ( c ) - 48 digit = digit + 1 if ( digit == 10 ) then digit = 0 end if c = char ( digit + 48 ) file_name(i:i) = c if ( c /= '0' ) then return end if end if end do ! ! No digits were found. Return blank. ! if ( change == 0 ) then file_name = ' ' return end if 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 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