0% found this document useful (0 votes)
155 views

Computation and Programming in Physics - Euler & Runge-Kutta Methods

The document discusses numerical methods for solving differential equations using grid-based computation. It introduces the Euler method and Runge-Kutta method for solving differential equations in the temporal and spatial domains. Specifically, it discusses using the simple Euler method, modified Euler method, and improved Euler method to solve ordinary differential equations numerically and compares the numerical solutions to exact solutions. It also provides an example of using the simple Euler method to model a block-spring system and derives the equations to implement a simple Euler code to solve this system numerically.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
155 views

Computation and Programming in Physics - Euler & Runge-Kutta Methods

The document discusses numerical methods for solving differential equations using grid-based computation. It introduces the Euler method and Runge-Kutta method for solving differential equations in the temporal and spatial domains. Specifically, it discusses using the simple Euler method, modified Euler method, and improved Euler method to solve ordinary differential equations numerically and compares the numerical solutions to exact solutions. It also provides an example of using the simple Euler method to model a block-spring system and derives the equations to implement a simple Euler code to solve this system numerically.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 34

Computation and Programming in Physics

Euler & Runge-Kutta Methods

Syeilendra Pramuditya

Physics Program
Institut Teknologi Bandung
1
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
3
Differential Equation

dy 2
y( x)   f ( x, y )  x  1
dx
y ( x  0)  0

y ( x)  ..?
4
Differential Equation
dy
y( x)   f ( x, y )  x 2  1
dx
y ( x  0)  0

Exact Solution
2
   1)dx
dy  ( x

1 3
y  x  xC
3
y ( x  0)  0  C  0
1 3
y ( x )  f ( x, y )  x  x
3
5
Differential Equation

dy 1
y( x)   f ( x, y ) 
dx x
y ( x  1)  0

y ( x)  ..?
6
Differential Equation
dy 1
y( x)   f ( x, y ) 
dx x
y ( x  1)  0

Exact Solution
1
 dy   x dx
y  ln( x)  C
y ( x  1)  0  C  0
y ( x)  f ( x, y )  ln( x)
7
Differential Equation

dy 2 1
y( x)   f ( x, y )  x   1
dx y
y ( x  0)  0

y  ..???
Euler Methods
 Linear approx. of Taylor series
 “Simple Euler”

y ( x)  y ( x0 )
y( x0 )   f ( x0 , y0 )
x  x0
f ( x)  
y ( x)  y ( x0 )  x  x0 y( x0 )
x  x0  h
y ( x0  h)  y ( x0 )  hf ( x0 , y0 )
f ( x0 ) y ( x0  h)  y ( x0 )  hf ( x0 , y ( x0 ))
y ( x0  h)  y0  hf 0

x0 x
9
Ordinary Differential Equation (ODE)

dy 1 y ( x0  h)  y ( x0 )  hf ( x0 , y0 )
y( x)   f ( x, y ) 
dx x y ( x0  h)  y0  hf 0
y ( x  1)  0 ynew  yold  hf ( xold , yold )

 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?
Simple Euler Code
Solve the ODE numerically (find y(x)) using Simple
Euler method (for 1<x<10), use h = 1.0 and h = 0.5
dy 1
y( x)   f ( x, y ) 
dx x
y ( x  1)  0

y ( x0  h)  y ( x0 )  hf ( x0 , y0 )
y ( x0  h)  y0  hf 0
ynew  yold  hf ( xold , yold )
Exact VS Numeric

2.5

1.5

1
Exact
Euler, h = 1.0
0.5
Euler, h = 0.5

0
0 2 4 6 8 10
Euler Methods
 Linear approx. of Taylor series
 “Modified Euler” Use more correct gradient

 
y ( x)  y ( x0 )  x  x0 y( xmid )
y ( x0  h)  y ( x0 )  hf ( xmid , ymid )
f ( x) xmid  x0 
h
2
h
ymid  y ( x0  )
2
h
f ( x0 ) ymid  y0  f 0
2
y ( x0  h)  y0  hf mid

x0 x
Euler Methods
 Linear approx. of Taylor series
 “Improved Euler” Use more correct gradient

y( x0 )  f ( x0 , y0 )
y( x)  f ( x, y )  y0  hf 0
f ( x) y ( x0 )  y( x)
 ( x) 
yavg
2
f ( x0 , y0 )  f ( x, y )
f avg 
2
f ( x0 ) f ( x, y )  f ( x0  h, y0  hf ( x0 , y0 ))
y ( x0  h)  y ( x0 )  hf avg

x0 x
4th Order Runge-Kutta (RK4)
Exact VS Numeric

2.5

1.5

1
Exact
Euler, h = 1.0
0.5
Euler, h = 0.5

0
0 2 4 6 8 10
Exact VS Numeric
 Simple Euler
Exact VS Numeric
 Modified Euler
Exact VS Numeric
 Improved Euler
Exact VS Numeric
 RK4
20
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
21
Block-Spring System
y ( x0  h)  y ( x0 )  hf ( x0 , y0 )
y ( x0  h)  y0  hf 0

1 1
v  v0  at  v(t )  v0  v(t )t  v(t )  v(t )  v0  f (v, t )
t t
dv k
   
v t0    v t0  
dt t 0
 v0   x0
m

1 1
x  x0  vt  x(t )  x0  x(t )t  x(t )  x(t )  x0  f ( x, t )
t t
dx
   
x t0    x t0  
dt t 0
 x0   v0

1 2 1 2
E mv  kx
2 2
22
Block-Spring System (Simplified)
y ( x0  h)  y ( x0 )  hf ( x0 , y0 )
dy
y ( x0  h)  y0  hf 0  y0  h
dx

dv F kx0
v(t0  t )  v0  t  v0  at  v0  t  v0  t
dt m m
k
v(t0  t )  v0  x0 t
m
dx
x(t0  t )  x0  t  x0  v0 t
dt
1 2 1 2
E  mv  kx
2 2
23
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

dv F kx0
v(t0  t )  v0  t  v0  at  v0  t  v0  t
dt m m
k
v(t0  t )  v0  x0 t
m
y ( x0  h)  y ( x0 )  hf ( x0 , y0 )
dx
x(t0  t )  x0  t  x0  v0 t dy
dt y ( x0  h)  y0  hf 0  y0  h
1 2 1 2 dx
E  mv  kx
2 2
Simple Euler Code

k
v(t0  t )  v0  x0 t
m
x(t0  t )  x0  v0 t
1 2 1 2
E mv  kx
2 2
25
Simple Euler: Position
8

x(t) numeric
6
x(t) exact

-2

-4

-6
0 5 10 15 20
26
Simple Euler: Velocity
8

v(t) numeric
6
v(t) exact

-2

-4

-6

-8
0 5 10 15 20
Simple Euler: Energy
30

E kinetic
25 E potential
E mechanic
20

15

10

0
0 5 10 15 20
28
Simple Euler: Energy
Energy
1.6
dt = 0.2
1.4
dt = 0.1
1.2

1
Energy [J]

0.8

0.6

0.4

0.2

0
0 1 2 3 4 5 6
Time [s]
29
Modified Euler: Energy
Energy
1.6
dt = 0.2, Simple Euler
1.4
dt = 0.2, Modified Euler
1.2

1
Energy [J]

0.8

0.6

0.4

0.2

0
0 1 2 3 4 5 6
Time [s]
30
Damped Block-Spring System

F  ma  kx  cv
dv F kx  cv
a  
dt m m
m  k 1
dv
  x  cv
dt
dx
v
dt
31
Damped Block-Spring System
1 1
v  v0  at  v(t )  v0  v(t )t  v(t )  v(t )  v0  f (v, t )
t t
dv
   
v t0    v t0  
dt t 0

 v0   x0  cv 

1 1
x  x0  vt  x(t )  x0  x(t )t  x(t )  x(t )  x0  f ( x, t )
t t
dx
   
x t0    x t 0  
dt t 0
 x0   v0

1 2 1 2
E  mv  kx
2 2
32
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 Simple Euler
 Output: time,x(t),x_exact,v(t),v_exact,Em
dv F kx0  cv0
v(t0  t )  v0  t  v0  at  v0  t  v0  t
dt m m
kx0  cv0
v(t0  t )  v0  t
m
y ( x0  h)  y ( x0 )  hf ( x0 , y0 )
dx
x(t0  t )  x0  t  x0  v0 t dy
dt y ( x0  h)  y0  hf 0  y0  h
1 2 1 2 dx
E  mv  kx
2 2
Damped Oscillation: Exact Solution
 Halliday-Resnick, Chapter 15
34
Damped Oscillation (Mod. Euler)
1.2

0.8

0.6

0.4
Position

0.2

0
0 5 10 15 20
-0.2

-0.4

-0.6

-0.8

-1
TIme

You might also like