function Euler( u0, f, T, n )
h = T/n
t = 0:h:T
u = zeros(n+1)
u[1] = u0
for j = 1:n
u[j+1] = u[j] + h * f( t[j], u[j] )
end
return u
end Euler (generic function with 1 method)
Lecture 3
I encourage you to play around with the juptyer notebook for this lecture - you can copy the code with the this notebook button on the side of this page.
These notes are mainly a record of what we discussed and are not a substitute for attending the lectures and reading books! If anything is unclear/wrong, let me know and I will update the notes.
This lecture is mostly based on Burden, Faires, and Burden (2015) section 5.2
Recall that we are considering IVPs: seek u :[0,T] \to \mathbb R such that
\begin{align} &u(0) = u_0 \nonumber\\ &u'(t) = f\big( t, u(t) \big). \tag{IVP} \end{align}
We will suppose that this equation is well-posed. The numerical schemes we introduce in this section will approximate the solution u to (\text{IVP}) on a mesh \{t_j\}_{j=0}^n \subset [0,T]. The approximation at time t_j will be written u_j so that u_j \approx u(t_j). We will consider the error on the mesh: \max_j |u(t_j) - u_j |.
We now fix an equispaced mesh t_j = j\frac{T}{n}. We will sometimes call h := \frac{T}n the step size.
Replacing the derivative in (\text{IVP}) with the forward difference
\begin{align} u'(t_j) \approx \frac{u_{j+1} - u_j}{h} \end{align}
yields Euler’s method
\begin{align} u_{j+1} = u_j + h f(t, u_j) \tag{Euler} \end{align}
Last time, we saw that by integrating (\text{IVP}) we obtain
\begin{align} u(t_{j+1}) = u(t_{j}) + \int_{t_j}^{t_{j+1}} f\big( s, u(s) \big) \mathrm{d}s. \end{align}
Approximating this integral with the rectangular rule and replacing u(t_j) with u_j also yields Euler’s method.
That is, we approximate the differential equation with a difference equation.
We implement Euler’s method here:
function Euler( u0, f, T, n )
h = T/n
t = 0:h:T
u = zeros(n+1)
u[1] = u0
for j = 1:n
u[j+1] = u[j] + h * f( t[j], u[j] )
end
return u
end Euler (generic function with 1 method)
We now apply Euler’s method to each of the examples from Lecture 2:
Example 3.1 (Exponential growth) We consider u' = \mu u with u(0) = u_0 > 0. Recall that this problem is well-posed with unique solution u(t) = u_0 e^{\mu t}. We approximate this solution using Euler’s method:
μ = 2
f(t, u) = μ * u
u0, T, n = 1, 2, 4
u = Euler( u0, f, T, n )
scatter( 0:T/n:T, u,
label="Euler (n=$n)",
title="Exponential (u' = u)")
n=100
u2 = Euler( u0, f, T, n )
scatter!( 0:T/n:T, u2,
label="Euler (n=$n)" )
plot!(t-> u0 * exp(μ * t),
label="exact solution")In the following, we plot the errors as a function of n:
Exercise 3.1 Show that Euler’s method applied to u' = u with u_0 > 0 always underestimates the function: i.e. we have u(t_j)\geq u_j for all j (equality is only achieved for j=0).
Example 3.2 (Logistic equation) Recall that the logistic equation takes the form
\begin{align} &u(0) = u_0, \\ &u'(t) = \mu u(t) - \kappa u(t)^2 \quad \text{on } (0,T). \end{align}
Last time, we saw that this problem is well-posed with solution
\begin{align} u(t) = \frac{\mu}{\kappa u_0 (e^{\mu t}-1) + \mu } u_0 e^{\mu t} \end{align}
u0, μ, κ, T, n = 1, 1., .05, 15, 5
f2(t,u) = μ * u - κ * u^2
u = Euler( u0, f2, T, n )
u_exact(t) = ( μ/( κ*u0*(exp(μ*t) - 1) + μ) ) * u0 * exp( μ * t )
plot(u_exact, 0, T,
title=L"u'=\mu u(t) - \kappa u(t)^2", legend=false,
xlabel=L"t", ylabel=L"u(t)")
scatter!( 0:T/n:T, u,
label="Euler (n=$n)")
n=100
u2 = Euler( u0, f2, T, n )
scatter!( 0:T/n:T, u2,
label="Euler (n=$n)" )Heres the error as a function of n:
Exercise 3.2 Now consider the logistic equation as above (with \mu = 1 and \kappa=0.05) but with u_0 = 100. What do expect to happen in Euler’s method when (i) n < 20, (ii) n = 20, and (iii) n > 20?
Example 3.3 Consider the following (well-posed) linear IVP
\begin{align} &u(0) = 0, \\ &u'(t) = t^2 - u \sin(t) \quad \text{on } (0,10) \end{align}
We apply Euler’s method to this equation:
# using the built-in method
f3(u,p,t) = t^2 - u * sin(t)
T = 10
u0, tspan = 0., (0., T)
ivp = ODEProblem(f3, u0, tspan)
sol = solve(ivp, Tsit5());
plot(sol,
title=L"u'=f(t,u)", legend=false,
xlabel=L"t", ylabel=L"u(t)")
# Euler
n=5
u = Euler( u0, (t,u)->f3(u,0,t), T, n )
scatter!(0:T/n:T, u)
n=50
u2 = Euler( u0, (t,u)->f3(u,0,t), T, n )
scatter!(0:T/n:T, u2)We will now prove that, under some assumptions on u, Euler’s method does indeed converge with rate O(h).
Burden, Faires, and Burden (2015) chapter 5.2, some of chapter 5.3