GENERAL PHYSICS 2 REFRACTION OF LIGHT SENIOR HIGH SCHOOL GENPHYS2.pptx
SPSF04 - Euler and Runge-Kutta Methods
1. 1
Programming, Computation, Simulation
Applications in Math & Physics
Euler and Runge-Kutta Methods
Ordinary Differential Equations & Initial Value Problems
Syeilendra Pramuditya
Department of Physics
Institut Teknologi Bandung
2. Grid-based Computation
Euler-type Methods
Differential equations
Temporal domain (variation in time)
Initial conditions
Finite Difference Methods
Differential equations
Spatial domain (variation in space)
Boundary conditions: Von Neumann VS
Dirichlet
4. 4
Differential Equation
2
( ) ( , ) 1
( 0) 0
dy
y x f x y x
dx
y x
2
3
3
Exact Solution
( 1)
1
3
( 0) 0 0
1
( ) ( , )
3
dy x dx
y x x C
y x C
y x f x y x x
6. 6
Differential Equation
1
( ) ( , )
( 1) 0
dy
y x f x y
dx x
y x
Exact Solution
1
ln( )
( 1) 0 0
( ) ( , ) ln( )
dy dx
x
y x C
y x C
y x f x y x
8. Euler Methods
Linear approx. of Taylor series
“Simple Euler”
0
x x
0
( )
f x
( )
f x
0
0 0 0
0
0 0 0
0
0 0 0 0
0 0 0 0
0 0 0
( ) ( )
( ) ( , )
( ) ( ) ( )
( ) ( ) ( , )
( ) ( ) ( , ( ))
( )
y x y x
y x f x y
x x
y x y x x x y x
x x h
y x h y x hf x y
y x h y x hf x y x
y x h y hf
0 0 0
( ) ( , )
y
y x f x y
x
9. 9
Ordinary Differential Equation (ODE)
0 0 0 0
0 0 0
( ) ( ) ( , )
( )
( , )
new old old old
y x h y x hf x y
y x h y hf
y y hf x y
Solve the above ODE numerically (find y(x)) using Simple
Euler method (for 1<x<10), use h = 1.0 and h = 0.5
How good the numerical solution is?
1
( ) ( , )
( 1) 0
dy
y x f x y
dx x
y x
10. Simple Euler Code
0 0 0 0
0 0 0
( ) ( ) ( , )
( )
( , )
new old old old
y x h y x hf x y
y x h y hf
y y hf x y
Solve the ODE numerically (find y(x)) using Simple
Euler method (for 1<x<10), use h = 1.0 and h = 0.5
1
( ) ( , )
( 1) 0
dy
y x f x y
dx x
y x
12. Euler Methods
Linear approx. of Taylor series
“Modified Euler” Use more correct gradient
0
x x
0
( )
f x
( )
f x
0 0
0 0
0
0
0 0
0 0
( ) ( ) ( )
( ) ( ) ( , )
2
( )
2
2
( )
mid
mid mid
mid
mid
mid
mid
y x y x x x y x
y x h y x hf x y
h
x x
h
y y x
h
y y f
y x h y hf
13. Modified Euler
0 0
0 0
0
0
0 0
0 0
( ) ( ) ( )
( ) ( ) ( , )
2
( )
2
2
( )
mid
mid mid
mid
mid
mid
mid
y x y x x x y x
y x h y x hf x y
h
x x
h
y y x
h
y y f
y x h y hf
14. Euler Methods
Linear approx. of Taylor series
“Improved Euler” Use more correct gradient
0
x x
0
( )
f x
( )
f x
0 0 0
0 0
0
0 0
0 0 0 0
0 0
( ) ( , )
( ) ( , )
( ) ( )
( )
2
( , ) ( , )
2
( , ) ( , ( , ))
( ) ( )
avg
avg
avg
y x f x y
y x f x y y hf
y x y x
y x
f x y f x y
f
f x y f x h y hf x y
y x h y x hf
15. Improved Euler
0 0 0
0 0
0
0 0
0 0 0 0
0 0
( ) ( , )
( ) ( , )
( ) ( )
( )
2
( , ) ( , )
2
( , ) ( , ( , ))
( ) ( )
avg
avg
avg
y x f x y
y x f x y y hf
y x y x
y x
f x y f x y
f
f x y f x h y hf x y
y x h y x hf
16. 2nd Order Runge-Kutta (RK2)
1 0 0
1
2 0 0
0 0 2
( , )
( , )
( , )
2 2
( )
dy
y f x y
dx
k hf x y
k
h
k hf x y
y x h y k
17. 4th Order Runge-Kutta (RK4)
1 0 0
2 0 0 1
3 0 0 2
4 0 0 3
0 0 1 2 3 4
4th Order Runge-Kutta
( , )
( , )
( , )
2 2
( , )
2 2
( , )
( ) 2 2
6
dy
y f x y
dx
k f x y
h h
k f x y k
h h
k f x y k
k f x h y hk
h
y x h y k k k k
22. 22
Block-Spring System
( ) ( , ) ( )
( ) ( , ) ( )
F ma
dv F k
a x
dt m m
dv k k
x v t f x t x t
dt m m
dx
v x t f v t v t
dt
23. 23
Block-Spring System
0 0 0
0 0 0 0
0
0 0 0
0 0 0 0
0
2 2
1 1
( ) ( ) ( ) ( ) ( , )
1 1
( ) ( ) ( ) ( ) ( , )
1 1
2 2
t
t
v v at v t v v t t v t v t v f v t
t t
dv k
v t v t v x
dt m
x x vt x t x x t t x t x t x f x t
t t
dx
x t x t x v
dt
E mv kx
0 0 0 0
0 0 0
( ) ( ) ( , )
( )
y x h y x hf x y
y x h y hf
24. 24
Block-Spring System (Simplified)
1
2
0 0
0 0 1 0 0 0 0
0 0 2 0 0 0 0 0 0
2 2
( ) ( , ) ( )
( ) ( , ) ( )
( )
( ) ( , )
( ) ( , ) ( )
1 1
2 2
dv k
v t f t v x t
dt m
dx
x t f t x v t
dt
kx t kx
v t t v t f t v v t v t
m m
x t t x t f t x x tv t x tv
E mv kx
0 0 0 0
0 0 0 0
( ) ( ) ( , )
( )
y x h y x hf x y
dy
y x h y hf y h
dx
25. 25
Block-Spring System (Simplified)
0 0
0 0 0 0 0
0 0 0 0 0 0 0
2 2
( ) ( )
( ) ( )
( )
( ) ( )
( ) ( ) ( )
1 1
2 2
dv k
v t x t
dt m
dx
x t v t
dt
kx t kx
v t t v t v t v t v t
m m
x t t x t x t x tv t x tv
E mv kx
0 0 0 0
0 0 0 0
( ) ( ) ( , )
( )
y x h y x hf x y
dy
y x h y hf y h
dx
26. 26
Block-Spring System (Simplified)
0
0 0 0 0 0
0 0 0
0 0 0 0
2 2
( )
( )
( )
1 1
2 2
kx
dv F
v t t v t v a t v t v t
dt m m
k
v t t v x t
m
dx
x t t x t x v t
dt
E mv kx
0 0 0 0
0 0 0 0
( ) ( ) ( , )
( )
y x h y x hf x y
dy
y x h y hf y h
dx
27. 27
Block-Spring System
1
0 0 0 0 0 0 0 0 0
2
0 0 0
0 0 0 0 0 0
Simple Euler
( , ) ( ) ( )
( ) ( , ) ( , )
( , ) ( ) ( )
( , )
( ) ( , )
new old old
new old
dx
x t x f t v t
dt
x t t x t x t x x tv t v x tv
x x tv
dv k
v t v f t x t
dt m
kx t x kx
v t t v t v t v v t v t
m m
v v t
2 2
1 1
2 2
old
kx
m
E mv kx
29. 29
Write a Simple Euler Code
m = k = 1
0 < t < 20 sec
dt = 0.2
x(t=0) = 1
v(t=0) = 0
Calculate x(t) and v(t) using Simple Euler
Output: time,x(t),x_exact,v(t),v_exact,Em
0 0 0 0
0 0 0 0
( ) ( ) ( , )
( )
y x h y x hf x y
dy
y x h y hf y h
dx
0
0 0 0 0 0
0 0 0
0 0 0 0
2 2
( )
( )
( )
1 1
2 2
kx
dv F
v t t v t v a t v t v t
dt m m
k
v t t v x t
m
dx
x t t x t x v t
dt
E mv kx
30. Simple Euler Code
0 0 0
0 0 0
2 2
( )
( )
1 1
2 2
k
v t t v x t
m
x t t x v t
E mv kx
32. Gnuplot Script
# Script to plot 1D dataset
reset
unset label
unset key
set key left top
set xrange [0:10]
set yrange [-3:3]
set title "Plot Image"
set xlabel "X Value"
set ylabel "Y Value"
set terminal wxt size 600,400 font "Verdana,10"
plot 'output.txt' using 2:3 title "Numeric" with linespoints pointtype 6 lw 1 lc 7,
'output.txt' using 2:4 title "Analytic" with linespoints pointtype 6 lw 1 lc 6
36. 36
Block-Spring System
1
2
0 0 1 0 0 0
0 0 2 0 0 0
0 0 1
0 0 2
Modified Euler
( , ) ( ) ( )
( , ) ( ) ( )
( ) ( )
2 2 2
( ) ( )
2 2 2
( ) ( )
( ) ( )
mid
mid
mid
mid
dx
x t x f t v t
dt
dv k
v t v f t x t
dt m
t t t
x x t x f t x v
t t t k
v v t v f t v x
m
x t t x t f t
v t t v t f t
40. Mass-Spring System
2nd Order Runge-Kutta (RK2)
1 0 0
1
2 0 0
0 0 2
( ) ?
( , )
( , )
( , )
2 2
( )
q t
dq
q f t q
dt
k dt f t q
k
dt
k dt f t q
q t dt q k
1 0 0
1
2 0 0
0 0 2
( ) ?
( , )
( , )
( , )
2 2
( )
y x
dy
y f x y
dx
k hf x y
k
h
k hf x y
y x h y k
y(x)
x
q(t)
t
41. Mass-Spring System
2nd Order Runge-Kutta (RK2)
1
( ) ?
( , ) ( )
x t
dx
x f t x v t
dt
x(t)
t
v(t)
t
2
( ) ?
( , ) ( )
v t
dv k
v f t v x t
dt m
42. Mass-Spring System
2nd Order Runge-Kutta (RK2)
1 0 0
1
2 0 0
0 0 2
( ) ?
( , )
( , )
( , )
2 2
( )
q t
dq
q f t q
dt
k dt f t q
k
dt
k dt f t q
q t dt q k
1
1 1
2 1
2
2
( ) ( )
( ) ( )
( ) ( )
2 2
( ) ( ) ( ) ( )
2 2 2
( ) ( )
2
( ) ( ) ( ) ( ) ( )
2
dx
x f t v t
dt
k t f t t v t
t t
k t f t t v t
t t t k
v t v t v v t x t
m
t k
k t v t x t
m
t k
x t t x t k x t t v t x t
m
43. Mass-Spring System
2nd Order Runge-Kutta (RK2)
1 0 0
1
2 0 0
0 0 2
( ) ?
( , )
( , )
( , )
2 2
( )
q t
dq
q f t q
dt
k dt f t q
k
dt
k dt f t q
q t dt q k
2
1 2
2 2
2
2
( ) ( )
( ) ( )
( ) ( )
2 2
( ) ( ) ( ) ( )
2 2 2
( ) ( )
2
( ) ( ) ( ) ( ) ( )
2
dv k
v f t x t
dt m
k
k t f t t x t
m
t k t
k t f t t x t
m
t t t
x t x t x x t v t
k t
k t x t v t
m
k t
v t t v t k v t t x t v t
m
47. 4th Order Runge-Kutta (RK4)
1 0 0
2 0 0 1
3 0 0 2
4 0 0 3
0 0 1 2 3 4
( , )
( , )
( , )
2 2
( , )
2 2
( , )
( ) 2 2
6
dy
y f x y
dx
k f x y
h h
k f x y k
h h
k f x y k
k f x h y hk
h
y x h y k k k k
1
2
( ) ( )
( ) ( )
( ) ?
( ) ?
dx
x f t v t
dt
dv k
v f t x t
dt m
x t dt
v t dt
50. 50
Damped Mass-Spring System
0 0 0
0 0 0 0
0
0 0 0
0 0 0 0
0
2 2
1 1
( ) ( ) ( ) ( ) ( , )
1 1
( ) ( ) ( ) ( ) ( , )
1 1
2 2
t
t
v v at v t v v t t v t v t v f v t
t t
dv
v t v t v x cv
dt
x x vt x t x x t t x t x t x f x t
t t
dx
x t x t x v
dt
E mv kx
51. 51
Damped Oscillation
m = k = 1
0 < t < 20 sec
dt = 0.2
x(t=0) = 1
v(t=0) = 0
c = 0.15
Calculate x(t) and v(t) using Euler Method
Output: time,x(t),x_exact,v(t),v_exact,Em
0 0 0 0
0 0 0 0
( ) ( ) ( , )
( )
y x h y x hf x y
dy
y x h y hf y h
dx
0 0
0 0 0 0 0
0 0
0 0
0 0 0 0
2 2
( )
( )
( )
1 1
2 2
kx cv
dv F
v t t v t v a t v t v t
dt m m
kx cv
v t t v t
m
dx
x t t x t x v t
dt
E mv kx