# The Euler and improved Euler methods

**URL:** <https://discourse.julialang.org/t/the-euler-and-improved-euler-methods/41404>\
**Category:** General Usage\
**Tags:** question\
**Created:** [June 14, 2020, 8:50pm UTC](https://discourse.julialang.org/t/the-euler-and-improved-euler-methods/41404 "2020-06-14T20:50:31Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![A.U.M](https://avatars.discourse-cdn.com/v4/letter/a/aeb1de/32.png) [@A.U.M](https://discourse.julialang.org/u/A.U.M)\
**Post date:** [June 14, 2020, 8:50pm UTC](https://discourse.julialang.org/t/the-euler-and-improved-euler-methods/41404/1 "2020-06-14T20:50:31Z")

</div>

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 :

```julia
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[1]) || (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[1] = t0
  ys[1] = 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 :

```julia
function improved_euler(F, t0, tn, y0, n)
  h = (tn - t0)/n
  ts = zeros(n+1)
  ys = zeros(n+1)
  ts[1] = t0
  ys[1] = 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)

```

---

<div class="post-metadata">

**Author:** ![mzaffalon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mzaffalon/32/214168_2.png) [@mzaffalon](https://discourse.julialang.org/u/mzaffalon)\
**Post date:** [June 15, 2020, 4:23am UTC](https://discourse.julialang.org/t/the-euler-and-improved-euler-methods/41404/2 "2020-06-15T04:23:38Z")

</div>

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.
