program main !======================================================================= ! Chapter 9, code 0: fixed-step methods for one first-order ODE. ! Solve x' = f(t,x), given x(t0) = x0. ! ! key = 1: Euler (first order) ! key = 2: modified Euler / Heun (second order) ! key = 3: classical Runge-Kutta (fourth order) ! ! Based on Ode11.f90 by Alexander Godunov (February 2010). ! The three methods are kept in separate, short subroutines. ! Heun uses an Euler predictor and ONE trapezoidal corrector. ! ! To solve another equation: ! 1. Change x0, t0, t_end, and dt in the driver. ! 2. Edit the derivative function f below. ! 3. Set check_exact = .false. unless the exact formula also matches. ! ! Default example: x' = x-t**2+1, x(0) = 0.5, 0 <= t <= 4. ! Exact solution: x(t) = (t+1)**2 - 0.5*exp(t). ! The smooth curve rises, turns, and falls within this interval. ! Try dt = 0.2, 0.1, and 0.05 with each method key. The table gives ! the absolute error at every time, not only at the endpoint. ! Halve dt and compare errors: asymptotically they decrease by factors ! of about 2 (Euler), 4 (Heun), and 16 (RK4) at the same final time. !======================================================================= implicit none double precision t0, t_end, dt, x0, ti, tf, xi, xf, exact, err double precision f integer key, i integer, parameter :: max_steps = 100000 logical check_exact character(len=24) method character(len=32) filename external f !-------------------- Initial data: edit here ---------------------------- t0 = 0.0d0 x0 = 0.5d0 t_end = 4.0d0 dt = 0.1d0 key = 3 check_exact = .true. !------------------------------------------------------------------------ if (dt <= 0.0d0 .or. t_end <= t0) stop 'Use dt > 0 and t_end > t0.' select case (key) case (1) method = 'Euler' filename = 'ch09_euler.dat' case (2) method = 'Modified Euler (Heun)' filename = 'ch09_heun.dat' case (3) method = 'Runge-Kutta 4th order' filename = 'ch09_rk4.dat' case default stop 'Choose key = 1, 2, or 3.' end select ! One plain text file per run; a rerun of that method overwrites it. ! Unit 20 is separate from standard screen output (*). open(unit=20, file=filename, status='replace', action='write') write(*,'(a,a)') 'Method: ', trim(method) write(*,'(a,f10.5)') 'Fixed step: ', dt if (check_exact) then write(*,'(a)') ' t numerical x exact x abs error' write(20,'(a)') '# t x_numerical x_exact absolute_error' else write(*,'(a)') ' t numerical x' write(20,'(a)') '# t x_numerical' end if ti = t0 xi = x0 i = 0 ! Print the initial point before taking any step. do if (check_exact) then ! Exact formula for x' = x-t**2+1, also valid if t0 or x0 changes. exact = (ti+1.0d0)**2 + (x0-(t0+1.0d0)**2)*exp(ti-t0) err = abs(xi-exact) write(*,100) ti, xi, exact, err write(20,101) ti, xi, exact, err else write(*,100) ti, xi write(20,101) ti, xi end if if (ti >= t_end) exit if (i >= max_steps) stop 'Maximum number of steps exceeded.' ! Build grid times from the step index, avoiding repeated-addition ! drift. The last step is shortened if dt does not divide the interval. i = i+1 tf = min(t_end, t0+dble(i)*dt) if (t_end-tf <= 64.0d0*epsilon(t_end)*max(1.0d0,abs(t_end))) tf = t_end if (tf <= ti) stop 'Step too small to advance time.' select case (key) case (1) call euler1(f, ti, xi, tf, xf) case (2) call heun1(f, ti, xi, tf, xf) case (3) call rk4_scalar(f, ti, xi, tf, xf) end select ti = tf xi = xf end do close(20) write(*,'(a,i8)') 'Steps: ', i write(*,'(a,a)') 'Data file: ', trim(filename) 100 format(f10.5, 2(1x,f18.12), 1x,es14.5) 101 format(4(1x,es24.16)) end program main function f(t,x) !======================================================================= ! Supply the equation here: return the derivative x' = f(t,x). ! This smooth example uses both t and x. !======================================================================= implicit none double precision f, t, x f = x-t*t+1.0d0 end function f subroutine euler1(f,ti,xi,tf,xf) !======================================================================= ! One Euler step: use the slope at the beginning of the interval. ! Input: derivative f, current point (ti,xi), next time tf. ! Output: xf, the numerical solution at tf. !======================================================================= implicit none double precision f, ti, xi, tf, xf, h external f h = tf-ti xf = xi+h*f(ti,xi) end subroutine euler1 subroutine heun1(f,ti,xi,tf,xf) !======================================================================= ! One modified Euler (Heun) step. ! Predict with Euler, then average slopes at the initial point and ! the predicted endpoint. This is an explicit second-order RK method; ! the corrector is applied once, without an iterative implicit solve. !======================================================================= implicit none double precision f, ti, xi, tf, xf, h, slope1, predicted, slope2 external f h = tf-ti slope1 = f(ti,xi) predicted = xi+h*slope1 slope2 = f(tf,predicted) xf = xi+0.5d0*h*(slope1+slope2) end subroutine heun1 subroutine rk4_scalar(f,ti,xi,tf,xf) !======================================================================= ! One classical fourth-order Runge-Kutta step. ! Each k includes h: initial slope, two midpoint slopes, endpoint slope. ! Combine them with weights 1/6, 1/3, 1/3, 1/6. !======================================================================= implicit none double precision f, ti, xi, tf, xf, h, k1, k2, k3, k4 external f h = tf-ti k1 = h*f(ti,xi) k2 = h*f(ti+h/2.0d0,xi+k1/2.0d0) k3 = h*f(ti+h/2.0d0,xi+k2/2.0d0) k4 = h*f(tf,xi+k3) xf = xi+(k1+2.0d0*(k2+k3)+k4)/6.0d0 end subroutine rk4_scalar