# The Euler and improved Euler methods

Hello, I am working on some implementation for Euler and Improved Euler method. for the Euler method, it works well but for the improved version, I got some "character ignored due to Unicode error " and the result is totally different from the Euler method this is the code

Euler Method :

``````using CalculusWithJulia   # loads `SymPy`, `Roots`, `Plots`
@vars t y
u = SymFunction("u")
t0, y0 = 1, 1
F(y,t) = y*t
# Exact solution
out = dsolve(u'(t) - F(u(t),t), u(t), ics=(u, t0, y0))
function linterp(ts, ys)
function(t)
((t < ts) || (t > ts[end])) && return NaN
for i in 1:(length(ts) - 1)
if ts[i] <= t < ts[i+1]
l = (t-ts[i]) / (ts[i+1] - ts[i])
return (1-l) * ys[i] + l * ys[i+1]
end
end
ys[end]
end
end
function euler(F, t0, tn, y0, n)
h = (tn - t0)/n
ts = zeros(n+1)
ys = zeros(n+1)
ts = t0
ys = y0
for i in 1:n
ts[i + 1] = ts[i] + h
ys[i + 1] = ys[i] + h * F(ys[i], ts[i])
end
linterp(ts, ys)
end
u = euler(F, 1, 2, 1, 50)
plot(exp(-1/2)*exp(t^2/2), t0, 2)
plot!(u, t0, 2)
``````

The improved Version :

``````function improved_euler(F, t0, tn, y0, n)
h = (tn - t0)/n
ts = zeros(n+1)
ys = zeros(n+1)
ts = t0
ys = y0
for i in 1:n
ts[i + 1] = ts[i] + h
ys[i + 1] = ys[i] + h/2 * F(ys[i], ts[i])+F(ys[i]+h*F(ys[i], ts[i]), ts[i+1])
end
linterp(ts, ys)
end

u = improved_euler(F, 1, 2, 1, 50)
plot(exp(-1/2)*exp(t^2/2), t0, 2)
plot!(u, t0, 2)
``````
1 Like

In the modified Euler method you forgot to multiply the last term of `y[i+1]` by `h/2`.

I could not reproduce the Unicode error: also I didn’t use Plots but PyPlots, although I don’t believe this has anything to do with your error.