Docsity

Understanding Runge-Kutta Methods for Solving Differential Equations, Study notes of Physics

Partial preview of the text

Download Understanding Runge-Kutta Methods for Solving Differential Equations and more Study notes Physics in PDF only on Docsity!

SUMMARY (so far) To solve dy dx

= f (x, y) starting from (x 0 , y 0 ).

Euler:

yn+1 = yn + hf (xn, yn) Explicit but not very accurate.

Trapezoidal:

yn+1 = yn + h 2

[ f (xn, yn) + f (xn+1, yn+1)

]

More accurate but implicit.

  • Can we construct accurate explicit methods?

Yes, Runge-Kutta methods.

Consider Euler-trapezoidal predictor-corrector method with just one correction step:

yn+1 = yn + h^1 2

[ f (xn, yn) + f

( xn+1, yn + hf (xn, yn)

)]

x (^) n x +hn

n n

(x +h,y +hf(x ,y ))n n n n

slope = f(x ,y )n n

slope = f(x +h,y +hf(x ,y ))n n n n

slope = 0.5*(f(x ,y ) + f(x +h,y +hf(x ,y )))

(x ,y )

n n n n n n

2nd order Runge-Kutta method

Step 1: Take an Euler step from xn to a second point in the interval xn+αh (α ≤ 1).

Euler gives approximate value of y at this point as

yn + αhf (xn, yn)

Step 2: Take a weighted average of the slopes at the initial point and the point given by the Euler method estimate:

yn+1 = yn + h [w 1 f (xn, yn) + w 2 f (xn + αh, yn + αhf (xn, yn))]

Choose α, w 1 and w 2 to make the local error as small as possible.

[Improved Euler method corresponds to α = 1, w 1 = w 2 = 12 .]

To calculate the local error we must substitute in the analytic expression and Taylor expand:

Error = y(xn+1)−y(xn)−w 1 hf (xn, y(xn))−w 2 hf (xn + αh, y(xn) + αhf (xn, y(xn)))

First note, (Taylor series)

y(xn+1) = y(xn) + hy′(xn) + h

2 2

y′′(xn) + 0(h^3 ) We know y′(x) = f (x, y(x)) (†) so y′(xn) = f (xn, y(xn)) ≡ fn for short. To find y′′(x) differentiate (†):

y′′(x) = d dx

f (x, y(x)) = ∂f ∂x

  • ∂f ∂y

dy dx

= ∂f ∂x

  • ∂f ∂y

f

So, in the obvious notation,

y(xn+1) = y(xn) + hfn + h

2 2

[ ∂fn ∂x +^

∂fn ∂y fn

]

  • O(h^3 ).

Examples Write k = hf (xn, yn)

i) α = 1 ⇒ w 2 = 12 ⇒ w 1 = (^12)

yn+1 = yn + h 2

[f (xn, yn) + f (xn + h, yn + k)] This is our previous improved Euler method.

ii) α = 12 ⇒ w 2 = 1 ⇒ w 1 = 0

yn+1 = yn + hf (xn + 12 h, yn + 12 k) This is sometimes called the modified Euler method.

iii) α = 23 ⇒ w 2 = 34 ⇒ w 1 = (^14)

yn+1 = yn + 4 h

[ f (xn, yn) + 3f (xn + 23 h, yn + 23 k)

]

These methods will, in general, give different answers but they will have comparable errors.

Geometrical interpretation of the modified Euler method

x x +h

slope = f(x ,y )

slope = f(x +0.5h,y +0.5hf(x ,y ))) (x ,y )n n

n n n n

n n

n (^) n

  1. 2nd order Runge-Kutta: yn+1 = yn+hf (xn+h 2 , yn+k 2 ) where k = hf (xn, yn).

⇒ y 1 = y 0 + hf (0. 1 , y 0 + 12 hf (0, y 0 )) = 1 + 0. 2 × (− 0 .9) = 0. 82 ⇒ y 2 = y 1 + h · f (0. 3 , y 1 + 12 hf (0. 2 , y 1 )) = 0 .82 + 0. 2 × (− 0 .738) = 0. 6724

⇒ Error = 0. 6703 − 0 .6724 = − 0. 0021

Conclusion: Euler → 2% accuracy Second order RK → 0 .3% accuracy

with the same computational effort.

Fourth order Runge-Kutta

Often called the Runge-Kutta method. In calculating yn+1 starting from (xn, yn) the function f is evaluated four times.

i.e. four “slopes” are calculated and combined to give a “mean slope”,

yn+1 = yn + h × “mean slope”, in such a way that the error is O(h^5 ).

There are many ways of doing this (just like the second order case) but one has been adopted as standard.

Geometrical interpretation of the standard 4th order Runge-Kutta method

A

B

C

D

slope=f(A)

slope=f(B)^ slope=f(C)

slope=(f(A)+2f(B)+2f(C)+f(D))/

Example: Solve dy

  • = − 2 x − y with y(0) = − dx
  • Use h = 0. 1 We have x 0 = 0, y 0 = −
  • 1st step: Calculate y 1 , i.e. value at x 1 = 0.
  • k 1 = 0.1(− 2 × (0) + 1) = 0. 1 ⇒ y 0 + 12 k 1 = −
  • k 2 = 0.1(− 2 × (0.05) + 0.95) = 0. 085 ⇒ y 0 + 12 k 2 = −
  • k 3 = 0.1(− 2 × (0.05) + 0.9575) = 0. 0858 ⇒ y 0 + k 3 = −
  • k 4 = 0.1(− 2 × (0.1) + 0.9142) = 0.
  • ⇒ y 1 = −1 + 16 (0.1 + 2 × 0 .085 + 2 × 0 .0858 + 0.0714) = −