workshop4_euler_method
workshop4_euler_method
dS
= −βSI ,
dt
dE
= βSI − σE ,
dt
dI
= σE − γI ,
dt
dR
= γI . (1)
dt
Euler method implementation
Code available on Canvas under “Files” in the folder
®)
“example codes” as tutorial3 seir model euler.py (in
Python ) and tutorial3 seir model euler.m (in Matlab
Implement automatic peak detection
This is a typical “open-ended“ bonus question for coding
Homeworks. The solution will not be in the example codes!
Poll: how can we detect peaks in numerical ODE solution?
Poll: which equations should be adjusted to reflect the
decline of immunity level in the population?
Solution without arrays
Solution that uses functions
Tutorial problem 2: Finite-difference approximations and
interpolating polynomial
▶ We can shift the enumeration of points for our convenience:
1 2
P4 (t) = f0 + (D+ f )0 (t − t0 ) + D+ f 0 (t − t0 ) (t − t1 ) +
2!
1 3
+ D+ f 0 (t − t0 ) (t − t1 ) (t − t2 ) +
3!
1 4
+ D+ f 0 (t − t0 ) (t − t1 ) (t − t2 ) (t − t3 ) (2)
4!
Finite-difference approximations and interpolating
polynomial
(D+ f )0 = δt −1 (f1 − f0 ),
2
f 0 = δt −2 (f2 − 2f1 + f0 ),
D+
3
f 0 = δt −3 (f3 − 3f2 + 3f1 − f0 ),
D+
4
f 0 = δt −4 (f4 − 4f3 + 6f2 − 4f1 + f0 ).
D+ (3)
dP4 (t) 1 2
d
= (D+ f )0 + D+ f 0 ((t − t0 ) (t − t1 )) +
dt t=t2 2! dt t=t2
1 3
d
+ D+ f 0 ((t − t0 ) (t − t1 ) (t − t2 )) +
3! dt t=t2
1 4
d
+ D+ f 0 ((t − t0 ) (t − t1 ) (t − t2 ) (t − t3 )) (4)
4! dt t=t2
Differentiating the interpolating polynomial
d
((t − t0 ) (t − t1 )) = (t2 − t0 ) + (t2 − t1 ) = 3 δt
dt t=t2
d
((t − t0 ) (t − t1 ) (t − t2 )) = (t2 − t0 ) (t2 − t1 ) = 2 δt 2
dt t=t2
d
((t − t0 ) (t − t1 ) (t − t2 ) (t − t3 )) =
dt t=t2
= (t2 − t0 ) (t2 − t1 ) (t2 − t3 ) = −2 δt 3 (5)
Putting everything together
dP4 (t) −1 3
= δt (f1 − f0 ) + (f2 − 2f1 + f0 )+
dt t=t2 2
1 1
+ (f3 − 3f2 + 3f1 − f0 )− (f4 − 4f3 + 6f2 − 4f1 + f0 ) (7)
3 12
Putting everything together
dP4 (t) 1
= (12f1 − 12f0 + 18f2 − 36f1 + 18f0 +
dt t=t2 12δt
+4f3 − 12f2 + 12f1 − 4f0 −f4 + 4f3 − 6f2 + 4f1 − f0 ) (8)
Restoring enumeration
df (t) −fi+2 + 8fi+1 − 8fi−1 + fi−2
+ O δt 4
= (10)
dt t=ti 12 δt
Error of interpolating polynomials
f (n+1) (τ )
f (t) − Pn (t) = (t − t0 ) (t − t1 ) . . . (t − tn ) (11)
(n + 1)!
0.05 t (t - /2)
2
0.00
0.0 0.5 1.0 1.5
Comparing the error estimate with exact difference
0.80
0.75
0.70
0.65
0.60
t (t - /2)
0.55 (Sin[t]- 2 t )/(- 2
)
0.50
0.45
0.0 0.5 1.0 1.5
Scaling of error for finite difference approximations
dk
(f (t) − Pn (t)) =
dt k !
dk f (n+1) (τ )
= (t − t0 ) (t − t1 ) . . . (t − tn ) =
dt k (n + 1)!
f (n+1) (τ ) d k
= (t − t0 ) (t − t1 ) . . . (t − tn ) +
(n + 1)! dt k
!
d f (n+1) (τ ) d k−1
+ (t − t0 ) (t − t1 ) . . . (t − tn ) + . . . (12)
dt (n + 1)! dt k−1
d2
(f (t) − P2 (t)) =
dt 2 t=ti
d 2 f (3) (τ )
= (t − ti−1 ) (t − ti ) (t − ti+1 ) =
dt 2 3!
t=ti
f (3) (τ ) d2
= (t − ti−1 ) (t − ti ) (t − ti+1 ) +
3! dt 2 t=ti
d f (3) (τ ) d
+2 (t − ti−1 ) (t − ti ) (t − ti+1 ) +
dt 3! dt t=ti
t=ti
d 2 f (3) (τ )
+ (t − ti−1 ) (t − ti ) (t − ti+1 )|t=ti (15)
dt 2 3!
t=ti
Introduce u = t − ti
d2
(t − ti−1 ) (t − ti ) (t − ti+1 ) =
dt 2 t=ti
d2
= (u + δt) u (u − δt) =0 (16)
du 2 u=0
d
(t − ti−1 ) (t − ti ) (t − ti+1 ) =
dt t=ti
= (ti − ti−1 ) (ti − ti+1 ) = −δt 2 (17)
fi+1 − fi−1
f ′ (ti ) = + O δt 2
2δt
′ −fi+2 + 8fi+1 − 8fi−1 + fi−2
+ O δt 4
f (ti ) =
12δt
fi+3 − 9f i+2 + 45f i+1 − 45f i−1 + 9fi−2 − fi−3
f ′ (ti ) = + O δt 6 (18)
60δt
Will higher-order finite differences always improve
precision?
▶ Quickly changing functions?
▶ Finite precision of floating-point numbers?
▶ Discontinuous functions/derivatives?
▶ Imperfect (noisy) data?
Improvements of the Euler method
Poll: What improvement strategy DOES NOT
necessarily lead to more precise or stable method?
▶ Using higher-order finite difference approximations for dfdt(t)
▶ Using improved integration schemes (integral derivation of the
Euler method)
▶ Special methods for Newtonian equations of motion
▶ Using implicit methods
Milne method: higher-order finite differences
df (t)
= F (t, f (t)) ⇒
dt
f¯i+1 − f¯i−1
= F ti , f¯i
⇒ (19)
2δt
Implicit Euler method
▶ Instead of forward finite difference for dfdt(t) , use backward one
▶ Usual Euler method: fi+1 = fi + δt F (ti , fi )
▶ Implicit Euler method: fi+1 = fi + δt F (ti+1 , fi+1 )
▶ Iterative relations differ by terms of order δt 2 - subleading
compared to discretization errors
▶ Usual Euler: straightforward expression for fi+1 in terms of fi
▶ Implicit Euler: solve an equation to express fi+1 in terms of fi
df (t)
= −f (t)
dt
fi+1 = fi − δt fi ⇒ fi+1 = fi (1 − δt)
fi
fi+1 = fi − δt fi+1 ⇒ fi+1 = (20)
1 + δt
▶ For the implicit method, iterations are well-behaved for all
δt > 0!
Implicit Euler method
d 2 f (t)
= −f (t) ⇒
dt 2
df (t) dg (t)
⇒ = g (t), = −f (t) (21)
dt dt
df (t)
Use backward finite difference to approximate dt :
f¯i+1 f¯i
1 −δt
= ⇒
δt 1 ḡi+1 ḡi
f¯i+1 f¯i
1 1 δt
⇒ = (24)
ḡi+1 1 + δt 2 −δt 1 ḡi
d 2 f (t)
= −f (t) ⇒
dt 2
df (t) dg (t)
⇒ = g (t), = −f (t) (25)
dt dt
d 2 f (t)
= F (t, f (t)) ⇒
dt 2
df (t) dg (t)
⇒ = g (t), = F (t, f (t)) (28)
dt dt