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