euler <- function ( f, t0, y0, tstop, n ) #*****************************************************************************80 # ## euler() uses the Euler method to solve an ordinary differential equation (ODE). # # Licensing: # # Copyright 2016 James P. Howard, II # # The computer code and data files on this web page are distributed under # https://opensource.org/licenses/BSD-2-Clause, the BSD-2-Clause license. # # Modified: # # 23 March 2020 # # Author: # # Original R code by James Howard; # This version by John Burkardt. # # Reference: # # James Howard, # Computational Methods for Numerical Analysis with R, # CRC Press, 2017 # ISBN13: 978-1-4987-2363-3. # # Input: # # function f ( t, y ): evaluates the right hand side of the ODE at (x,y). # # real T0: the initial time. # # real Y0: the initial solution value. # # real TSTOP: the final time. # # integer N: the number of steps to take. # # Output: # # list ( T(1:n+1), Y(1:n+1) ): the solution estimates. # { t <- t0 y <- y0 dt <- ( tstop - t0 ) / ( n - 1 ) for ( i in 2 : n ) { y0 <- y0 + dt * f ( t0, y0 ) t0 <- t0 + dt t <- c ( t, t0 ) y <- c ( y, y0 ) } return ( list ( t = t, y = y ) ) }