# L-28 MCS 471 Wed 26 Oct 2022 : eulerexpmod.jl # experiments with the modified Euler's method # on the test equation y' = y, y(0) = 1 using Printf """ modeulerexp(n::Int64,verbose::Bool=true) The Modified Euler's method with n evaluations on the interval [0,1] on the test equation y' = y, y(0) = 1. """ function modeulerexp(n::Int64,verbose::Bool=true) h = 1.0/n if verbose println(" i x predict correct exact error") end y0 = 1.0 y1 = 1.0 for i=1:n x = i*h y = (1.0+h)*y0 y1 = y0 + h*(y0 + y)/2.0 exact = exp(x) if verbose stri = @sprintf("%3d", i) strx = @sprintf("%.2f", x) stry = @sprintf("%.6e", y) stry1 = @sprintf("%.6e", y1) strexp = @sprintf("%.6e", exact) strerr = @sprintf("%.2e", abs(y1-exact)) println("$stri $strx $stry $stry1 $strexp $strerr") end y0 = y1 end return y1 end """ Runs the function eulerexp. """ function main() modeulerexp(10) println("Running for n = 10^i, i=1:6 :") exact = exp(1.0) for i=1:6 y = modeulerexp(10^i, false) stri = @sprintf("%8d", 10^i) stry = @sprintf("%.13e", y) strerr = @sprintf("%.2e", abs(y-exact)) println("$stri $stry $strerr") end end main()