subroutine backward_euler ( dydt, tspan, y0, n, m, t, y ) c*****************************************************************************80 c cc backward_euler() uses the backward Euler method + fsolve_be() to solve an 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 external dydt: a subroutine that evaluates the right c hand side of the ODE, of the form c subroutine dydt ( t, y, dy ) c c double precision tspan(2): the initial and final times. c c double precision y0(m): the initial condition. c c integer n: the number of steps to take. c c integer m: the number of variables. c c Output: c c double precision t(n+1), y(n+1,m): the times and solution values. c implicit none integer m integer n double precision dt external dydt double precision fm(m) integer i integer info double precision t(n+1) double precision tn double precision to double precision tol double precision tspan(2) double precision y(n+1,m) double precision y0(m) double precision yn(m) double precision yo(m) dt = ( tspan(2) - tspan(1) ) / n tol = 1.0D-05 t(1) = tspan(1) y(1,1:m) = y0(1:m) do i = 0, n if ( i == 0 ) then t(i+1) = tspan(1) y(i+1,1:m) = y0(1:m) else to = t(i) yo(1:m) = y(i,1:m) c c Call fsolve_be() to compute ym. c tn = t(i) + dt yn(1:m) = y(i,1:m) call fsolve_be ( dydt, m, to, yo, tn, yn, fm, tol, info ) if ( info /= 1 ) then write ( *, '(a)' ) '' write ( *, '(a)' ) 'backward_euler(): Fatal error!' write ( *, '(a,i10)' ) ' info = ', info stop 1 end if t(i+1) = tn y(i+1,1:m) = yn(1:m) end if end do return end