program main c*********************************************************************72 c cc sde_test() tests sde(). c c Licensing: c c This code is distributed under the MIT license. c c Modified: c c 21 September 2012 c c Author: c c John Burkardt c implicit none call timestamp ( ) write ( *, '(a)' ) ' ' write ( *, '(a)' ) 'sde_test():' write ( *, '(a)' ) ' Fortran77 version' write ( *, '(a)' ) ' Test sde().' call test01 ( ) call test02 ( ) call test03 ( ) call test04 ( ) call test05 ( ) call test06 ( ) call test07 ( ) call test08 ( ) call test09 ( ) call test10 ( ) call test11 ( ) c c Terminate. c write ( *, '(a)' ) ' ' write ( *, '(a)' ) 'sde_test():' write ( *, '(a)' ) ' Normal end of execution.' write ( *, '(a)' ) ' ' call timestamp ( ) stop end subroutine test01 ( ) c*********************************************************************72 c cc TEST01 tests BPATH. c c Licensing: c c This code is distributed under the MIT license. c c Modified: c c 22 September 2012 c c Author: c c John Burkardt c implicit none integer n parameter ( n = 500 ) integer seed double precision w(0:n) write ( *, '(a)' ) ' ' write ( *, '(a)' ) 'TEST01:' write ( *, '(a)' ) & ' BPATH generates a sample Brownian motion path' seed = 123456789 call bpath ( seed, n, w ) call bpath_gnuplot ( n, w ) return end subroutine test02 ( ) c*********************************************************************72 c cc TEST02 tests BPATH_AVERAGE. c c Licensing: c c This code is distributed under the MIT license. c c Modified: c c 22 September 2012 c c Author: c c John Burkardt c implicit none integer m parameter ( m = 1000 ) integer n parameter ( n = 500 ) double precision error integer seed double precision u(m,0:n) double precision umean(0:n) write ( *, '(a)' ) ' ' write ( *, '(a)' ) 'TEST02:' write ( *, '(a)' ) ' BPATH_AVERAGE generates many Brownian paths' write ( *, '(a)' ) ' and averages them.' seed = 123456789 call bpath_average ( seed, m, n, u, umean, error ) call bpath_average_gnuplot ( m, n, u, umean ) return end subroutine test03 ( ) c*********************************************************************72 c cc TEST03 tests CHAIN. c c Licensing: c c This code is distributed under the MIT license. c c Modified: c c 22 September 2012 c c Author: c c John Burkardt c implicit none integer n parameter ( n = 200 ) double precision diff integer seed double precision vem(0:n) double precision xem(0:n) write ( *, '(a)' ) ' ' write ( *, '(a)' ) 'TEST03:' write ( *, '(a)' ) & ' CHAIN solves a stochastic differential equation for' write ( *, '(a)' ) ' a function of a stochastic variable X.' write ( *, '(a)' ) & ' We can solve for X(t), and then evaluate V(X(t)).' write ( *, '(a)' ) & ' Or, we can apply the stochastic chain rule to derive an' write ( *, '(a)' ) ' an SDE for V, and solve that.' seed = 123456789 call chain ( seed, n, xem, vem, diff ) write ( *, '(a)' ) ' ' write ( *, '(a,g14.6)' ) ' Maximum | Sqrt(X) - V | = ', diff call chain_gnuplot ( n, xem, vem ) return end subroutine test04 ( ) c*********************************************************************72 c cc TEST04 tests EM. c c Licensing: c c This code is distributed under the MIT license. c c Modified: c c 22 September 2012 c c Author: c c John Burkardt c implicit none integer n parameter ( n = 256 ) double precision diff integer seed double precision t(0:n) double precision t2(0:n/4) double precision xtrue(0:n) double precision xem(0:n/4) write ( *, '(a)' ) ' ' write ( *, '(a)' ) 'TEST04:' write ( *, '(a)' ) & ' EM solves a stochastic differential equation' write ( *, '(a)' ) ' using the Euler-Maruyama method.' seed = 123456789 call em ( seed, n, t, xtrue, t2, xem, diff ) write ( *, '(a)' ) ' ' write ( *, '(a,g14.6)' ) ' | Exact X(T) - EM X(T) | = ', diff call em_gnuplot ( n, t, xtrue, t2, xem ) return end subroutine test05 ( ) c*********************************************************************72 c cc TEST05 tests EMSTRONG. c c Licensing: c c This code is distributed under the MIT license. c c Modified: c c 22 September 2012 c c Author: c c John Burkardt c implicit none integer m parameter ( m = 100 ) integer n parameter ( n = 512 ) integer p_max parameter ( p_max = 6 ) double precision dtvals(p_max) integer seed double precision xerr(p_max) write ( *, '(a)' ) ' ' write ( *, '(a)' ) 'TEST05:' write ( *, '(a)' ) & ' EMSTRONG investigates the strong convergence' write ( *, '(a)' ) ' of the Euler-Maruyama method.' seed = 123456789 call emstrong ( seed, m, n, p_max, dtvals, xerr ) call emstrong_gnuplot ( p_max, dtvals, xerr ) return end subroutine test06 ( ) c*********************************************************************72 c cc TEST06 tests EMWEAK. c c Licensing: c c This code is distributed under the MIT license. c c Modified: c c 22 September 2012 c c Author: c c John Burkardt c implicit none integer m parameter ( m = 50000 ) integer p_max parameter ( p_max = 5 ) double precision dtvals(p_max) integer method integer seed double precision xerr(p_max) write ( *, '(a)' ) ' ' write ( *, '(a)' ) 'TEST06:' write ( *, '(a)' ) ' EMWEAK investigates the weak convergence' write ( *, '(a)' ) ' of the Euler-Maruyama method.' seed = 123456789 method = 0 call emweak ( seed, method, m, p_max, dtvals, xerr ) call emweak_gnuplot ( p_max, dtvals, xerr, method ) seed = 123456789 method = 1 call emweak ( seed, method, m, p_max, dtvals, xerr ) call emweak_gnuplot ( p_max, dtvals, xerr, method ) return end subroutine test07 ( ) c*********************************************************************72 c cc TEST07 tests MILSTRONG. c c Licensing: c c This code is distributed under the MIT license. c c Modified: c c 22 September 2012 c c Author: c c John Burkardt c implicit none integer p_max parameter ( p_max = 4 ) double precision dtvals(p_max) integer seed double precision xerr(p_max) write ( *, '(a)' ) ' ' write ( *, '(a)' ) 'TEST07:' write ( *, '(a)' ) & ' MILSTRONG investigates the strong convergence' write ( *, '(a)' ) ' of the Milstein method.' seed = 123456789 call milstrong ( seed, p_max, dtvals, xerr ) call milstrong_gnuplot ( p_max, dtvals, xerr ) return end subroutine test08 ( ) c*********************************************************************72 c cc TEST08 tests STAB_ASYMPTOTIC. c c Licensing: c c This code is distributed under the MIT license. c c Modified: c c 22 September 2012 c c Author: c c John Burkardt c implicit none integer n parameter ( n = 1000 ) integer p_max parameter ( p_max = 3 ) integer seed write ( *, '(a)' ) ' ' write ( *, '(a)' ) 'TEST08:' write ( *, '(a)' ) ' STAB_ASYMPTOTIC investigates the asymptotic' write ( *, '(a)' ) ' stability of the Euler-Maruyama method.' write ( *, '(a)' ) ' ' write ( *, '(a)' ) ' For technical reasons, the plotting is done' write ( *, '(a)' ) ' in the same routine as the computations.' seed = 123456789 call stab_asymptotic ( seed, n, p_max ) return end subroutine test09 ( ) c*********************************************************************72 c cc TEST09 tests STAB_MEANSQUARE. c c Licensing: c c This code is distributed under the MIT license. c c Modified: c c 26 September 2012 c c Author: c c John Burkardt c implicit none integer seed write ( *, '(a)' ) ' ' write ( *, '(a)' ) 'TEST09:' write ( *, '(a)' ) & ' STAB_MEANSQUARE investigates the mean square' write ( *, '(a)' ) ' stability of the Euler-Maruyama method.' write ( *, '(a)' ) ' ' write ( *, '(a)' ) & ' For technical reasons, the plotting is done' write ( *, '(a)' ) ' in the same routine as the computations.' seed = 123456789 call stab_meansquare ( seed ) return end subroutine test10 ( ) c*********************************************************************72 c cc TEST10 tests STOCHASTIC_INTEGRAL_ITO. c c Licensing: c c This code is distributed under the MIT license. c c Modified: c c 21 September 2012 c c Author: c c John Burkardt c implicit none double precision error double precision estimate double precision exact integer i integer n integer seed write ( *, '(a)' ) ' ' write ( *, '(a)' ) 'TEST10:' write ( *, '(a)' ) & ' Estimate the Ito integral of W(t) dW over [0,1].' write ( *, '(a)' ) ' ' write ( *, '(a)' ) & ' ' // & 'Abs Rel' write ( *, '(a)' ) & ' N Exact Estimate ' // & 'Error Error' write ( *, '(a)' ) ' ' n = 100 seed = 123456789 do i = 1, 7 call stochastic_integral_ito ( n, seed, estimate, exact, error ) write ( *, '(2x,i8,2x,g16.8,2x,g16.8,2x,g10.2,2x,g10.2)' ) & n, exact, estimate, error, error / exact n = n * 4 end do return end subroutine test11 ( ) c*********************************************************************72 c cc TEST11 tests STOCHASTIC_INTEGRAL_STRAT. c c Licensing: c c This code is distributed under the MIT license. c c Modified: c c 22 September 2012 c c Author: c c John Burkardt c implicit none double precision error double precision estimate double precision exact integer i integer n integer seed write ( *, '(a)' ) ' ' write ( *, '(a)' ) 'TEST11:' write ( *, '(a)' ) & ' Estimate the Stratonovich integral of W(t) dW over [0,1].' write ( *, '(a)' ) ' ' write ( *, '(a)' ) & ' ' // & 'Abs Rel' write ( *, '(a)' ) & ' N Exact Estimate ' // & 'Error Error' write ( *, '(a)' ) ' ' n = 100 seed = 123456789 do i = 1, 7 call stochastic_integral_strat ( n, seed, estimate, & exact, error ) write ( *, '(2x,i8,2x,g16.8,2x,g16.8,2x,g10.2,2x,g10.2)' ) & n, exact, estimate, error, error / exact n = n * 4 end do return end function r8vec_max ( n, a ) c*********************************************************************72 c cc r8vec_max() returns the maximum value in an R8VEC. c c Discussion: c c An R8VEC is a vector of R8's. c c Licensing: c c This code is distributed under the MIT license. c c Modified: c c 12 May 2014 c c Author: c c John Burkardt c c Parameters: c c Input, integer N, the number of entries in the array. c c Input, double precision A(N), the array. c c Output, double precision R8VEC_MAX, the value of the largest entry. c implicit none integer n double precision a(n) integer i double precision r8_huge parameter ( r8_huge = 1.79769313486231571D+308 ) double precision r8vec_max double precision value value = - r8_huge do i = 1, n value = max ( value, a(i) ) end do r8vec_max = value 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