### Numerical Solutions to ODEs

```Numerical Solutions to ODEs
Nancy Griffeth
January 14, 2014
Funding for this workshop was provided by the program “Computational Modeling and Analysis of Complex Systems,” an
NSF Expedition in Computing (Award Number 0926200).
ODE
Numerical Differentiation

Definition of Differentiation
df
f (x + h) - f (x)
= f '(x) = lim
h®0
dx
h


Problem: We do not have an infinitesimal h
Solution: Use a small h as an
approximation
2
ODE
Forward Difference & Backward Difference

Forward Difference
df
f (x + h) - f (x)
» f '(x)approx =
dx
h

Backward Difference
f (x) - f (x - h)
f '(x)approx =
h
3
ODE
Numerical Differentiation - Example

Compute the derivative of function
f (x) = e x

At point x=1.15
f '(1.15) = f (1.15) » 3.1582
4
Euler Method
Explicit Euler Method

Consider Forward Difference

Which implies
y(t + Dt) - y(t)
y'(t) »
Dt
y(t + Dt) » y(t)+ Dt × y'(t)
5
Euler Method
Explicit Euler Method

Split time t into n slices of equal length Δt
ìt 0 = 0
ï
íti = i × Dt
ït = t
în

The Explicit Euler Method Formula
y(ti+1 ) = y(ti )+ Dt × y'(ti )
6
Euler Method
Explicit Euler Method - Algorithm
7
Euler Method
Implicit Euler Method

Consider Backward Difference
y(t) - y(t - Dt)
y'(t) »
Dt

Which implies
y(t) » y(t - Dt)+ Dt × y'(t)
8
Euler Method
Implicit Euler Method

Split the time into slices of equal length
y(ti+1 ) » y(ti )+ Dt × y'(ti+1 )

The above differential equation should be solved to
get the value of y(ti+1)


Extra computation
Sometimes worth because implicit method is more
accurate
9
Euler Method
A Simple Example

Try to solve IVP
ì y'(t) = e-t + t
í
î y(0) = 1


What is the value of y when t=0.5?
The analytical solution is
1 2
y = -e + t + 2
2
-t
10
Euler Method
A Simple Example

Using explicit Euler method
-ti
yi+1 = yi + dt × (e + ti )

We choose different dts to compare the accuracy
ì dt1 = 0.05 Þ t = 0, 0.05, 0.1,..., 0.5
ï
í dt2 = 0.025 Þ t = 0, 0.025, 0.05,..., 0.5
ï dt = 0.0125 Þ t = 0, 0.0125, 0.025,..., 0.5
î 3
11
Euler Method
A Simple Example
t
exact
dt=0.05
error
dt=0.025
error
dt=0.012
5
error
0.1
0.2
0.3
0.4
0.5
1.10016
1.20126
1.30418
1.40968
1.51846
1.10030
1.20177
1.30525
1.41150
1.52121
0.00014
0.00050
0.00107
0.00182
0.00274
1.10022
1.20151
1.30470
1.41057
1.51982
0.00006
0.00024
0.00052
0.00089
0.00135
1.10019
1.20138
1.30444
1.41012
1.51914
0.00003
0.00011
0.00025
0.00044
0.00067
At some given time t, error is proportional to dt.
12
Euler Method
A Simple Example


For some equations called Stiff Equations, Euler
method requires an extremely small dt to make result
accuracy
The Explicit Euler Method Formula
y'(t) = -k × y(t), k > 0

The choice of Δt matters!
yi+1 = yi - Dt × k × yi = (1- Dt × k)yi
13
Euler Method
A Simple Example

Assume k=5
ì y'(t) = -5y(t)
í
î y(0) =1

Analytical Solution is
y(t) = e

-5t
Try Explicit Euler Method with different dts
14
Choose dt=0.002, s.t.
Works!
1
0 <1- Dt × k <1 Þ Dt <
k
15
Choose dt=0.25, s.t.
1
2
-1 <1- Dt × k < 0 Þ < Dt <
k
k
Oscillates, but works.
16
Choose dt=0.5, s.t.
2
Dt > Þ 1- Dt × k < -1
k
Instability!
17
Euler Method
Stiff Equation – Explicit Euler Method

For large dt, explicit Euler Method does
not guarantee an accurate result
t
exact
dt=0.5
error
dt=0.25
error
dt=0.002
error
0.4
0.135335
1
6.389056
-0.25
2.847264
0.13398
0.010017
0.8
0.018316
-1.5
-0.015625 1.853096
0.017951
0.019933
1.2
0.002479
2.25
-0.000977 1.393973
0.002405
0.02975
1.6
0.000335
-3.375
-0.000061 1.181943
0.000322
0.039469
2
0.000045
5.0625
82.897225
906.71478
5
10061.733
21
111507.98
31
0.000015
0.000043
0.04909
0.663903
18
Euler Method
Stiff Equation – Implicit Euler Method

Implicit Euler Method Formula
yi+1 = y i - dt × 5× y i+1

Which implies
yi
yi+1 =
1+ 5dt
19
Choose dt=0.5,
Oscillation eliminated!
Not elegant, but works.
20
```