scientific computing

30
Scientific Computing Numerical Solution Of Ordinary Differential Equations - Runge-Kutta Methods

Upload: louvain

Post on 05-Feb-2016

30 views

Category:

Documents


0 download

DESCRIPTION

Scientific Computing. Numerical Solution Of Ordinary Differential Equations - Runge-Kutta Methods. Runge-Kutta Methods. If we take more terms in the Taylor’s Series expansion of f(t,x), we would get a more accurate solution algorithm. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Scientific Computing

Scientific Computing

Numerical Solution Of Ordinary Differential Equations

- Runge-Kutta Methods

Page 2: Scientific Computing

Runge-Kutta Methods

• If we take more terms in the Taylor’s Series expansion of f(t,x), we would get a more accurate solution algorithm.

• Problem: It is difficult to compute higher-order derivatives numerically.

• Idea: Approximate the value of higher derivatives of f(t,x) by evaluating f several times between iterates: ti and ti+1 .

Page 3: Scientific Computing

Better Methods

• The ODE +IV x(a)= x0 =s

can be alternatively solved as an integral equation. For example, on the first interval from x0 to x1 :

dx

dt f t,x

x(a h) x1 x0 a

ah f (t,x)dt

dt

dx

Page 4: Scientific Computing

Better Methods In general:

• The problem is that we don’t know x(t), so evaluating the integral is not trivial.

• We assume that if h is small enough, f(t,x) will not change that much over the interval [ti, ti+1]. Using “left-rectangular integration,”

• This is the Euler Method. A family of improved versions are called Runge-Kutta methods

dttxtfxxht

tii

i

i

))(,(1

hxtfxx iiii ),(1

Page 5: Scientific Computing

Euler Method

x

ti ti+1 = ti + h

),( ii xtf

xi true solution

Euler Solution

h

hxtfxx iiii ),(1

Tuncation error: )()( 21 hOtxx ii

true solution

Page 6: Scientific Computing

Euler vs Actual Solution

RK methods differ in how they estimate i

For Euler Method (a type of RK method), i ≈ f(ti, xi)

x

ti ti+1 = ti + h

),( ii xtf

xi true solution

Euler Solution

i

hxx iii 1

h

Page 7: Scientific Computing

Runge-Kutta Order 2 Method

x

xti ti+1

),( ii xtf

xi i

hxx iii 1

h

)),(( iii xthfx

f (ti h,x i (hf (ti,x i )))

)),(,(

),(

2

1

iiii

iii

xthfxhthf

xthf

Weighted average of two slopes

ti+αh

Page 8: Scientific Computing

Runge-Kutta Order 2 Method

• Let

• Then,• Expand k2 using Taylor’s Series in (t,x):

where is a point between

ii xtfk ,1

)),(,(2 iiii xtfhxhtfk

)( 22111 kkhxx ii

k2 = f (ti +α h,x i + β h f (ti,x i))

= f +αhf t + βhf fx +1

2(α 2h2 f tt (t,x) + 2αβ f h2 f tx (t,x) + β2h2 f 2 fxx (t,x))

),( xt ),(,(),( iiiiii xtfhxhtandxt

Page 9: Scientific Computing

Runge-Kutta Order 2 Method

Then,

The actual Taylor Series expansion for x(ti+h) is:

Thus, our weighted average approximation will be good to O(h3) if

)(

)(

322

2221

22111

hffhfhfhfhx

kkhxx

xti

ii

)()(2

1

)()'(2

1),(

)()(''2

1)(')()(

32

32

321

hfffhhfx

hxffhxthfx

hhtxthxtxhtxx

xti

xtiii

iiiii

1221 2

11 and

Page 10: Scientific Computing

Runge-Kutta Order 2 Method

So, general RK order 2 Method is:

Examples: 1) ω1=1, ω2=0 -> Euler’s Method (Still O(h2) as no averaging)

)( 22111 kkhxx ii

1221 2

11 and

ii xtfk ,1

)),(,(2 iiii xtfhxhtfk

Truncation error: )()( 31 hOtxx ii

true solution

Error is lower than Euler, but

now there are 2 calls to f(x,y) required per step

Page 11: Scientific Computing

Heun’s Method (Pav’s RK-Order 2)

x

x

ti ti+1

xi+1

xi

Heun’s method is ~ Trapezoidal Rule in Integration

hkxhtfSlope ii 1,

ii xtfSlope ,

Heun’s method

1

12

12

1

2

1

resulting in

hkkxx ii

211 2

1

2

1

where

ii xtfk ,1

hkxhtfk ii 12 ,

)],(),([2

11 iiii xtfhkxhtfSlopeAvg

x i1 x i t i

ti h f (r,x)dr

Page 12: Scientific Computing

Midpoint Method

resulting in

hkxx ii 21

where ii xtfk ,1

hkxhtfk ii 12 2

1,

2

1

2

12

1

1

0

2

1

Midpoint method is ~ Midpoint Rule in Integration

Page 13: Scientific Computing

Ralston’s Method

resulting in

hkkxx ii

211 3

2

3

1

where ii xtfk ,1

hkxhtfk ii 12 4

3,

4

3

4

34

33

23

1

2

1

Ralston (1962) and Ralston and Rabinowitiz (1978)determined that choosing ω2 = 2/3 provides a minimum bound on the truncation error for the second order RKalgorithms.

Page 14: Scientific Computing

http://numericalmethods.eng.usf.edu

Example

A ball at 1200K is allowed to cool down in air at an ambient temperature of 300K. Assuming heat is lost only due to radiation, the differential equation for the temperature of the ball is given by

Kdt

d12000,1081102067.2 8412

Find the temperature at 480t seconds using Heun’s method. Assume a step size of

240h seconds.

8412 1081102067.2

dt

d

8412 1081102067.2, tf

hkkii

211 2

1

2

1

Page 15: Scientific Computing

http://numericalmethods.eng.usf.edu

Solution

Step 1: Kti 1200)0(,0,0 00

5579.4

10811200102067.2

1200,0

,

8412

01

f

tfk o

017595.0

108109.106102067.2

09.106,240

2405579.41200,2400

,

8412

1002

f

f

hkhtfk

K

hkk

16.655

2402702.21200

240017595.02

15579.4

2

11200

2

1

2

12101

Page 16: Scientific Computing

http://numericalmethods.eng.usf.edu

Solution Cont

Step 2: Khtti 16.655,2402400,1 101

38869.0

108116.655102067.2

16.655,240

,

8412

111

f

tfk

20206.0

108187.561102067.2

87.561,480

24038869.016.655,240240

,

8412

1112

f

f

hkhtfk

K

hkk

27.584

24029538.016.655

24020206.02

138869.0

2

116.655

2

1

2

12112

Page 17: Scientific Computing

http://numericalmethods.eng.usf.edu

Solution Cont

The exact solution of the ordinary differential equation is given by the solution of a non-linear equation as

9282.21022067.00033333.0tan8519.1300

300ln92593.0 31

t

The solution to this nonlinear equation at t=480 seconds is

K57.647)480(

Page 18: Scientific Computing

http://numericalmethods.eng.usf.edu

Comparison with exact results

Figure 2. Heun’s method results for different step sizes

-400

0

400

800

1200

0 100 200 300 400 500

Time, t(sec)

Tem

per

atu

re,θ

(K) Exact h=120

h=240

h=480

Page 19: Scientific Computing

http://numericalmethods.eng.usf.edu

Effect of step size

Table 1. Temperature at 480 seconds as a function of step size, h

Step size, h (480) Et |єt|%

4802401206030

−393.87584.27651.35649.91648.21

1041.463.304

−3.7762−2.3406

−0.63219

160.829.7756

0.583130.36145

0.097625

K57.647)480( (exact)

Page 20: Scientific Computing

Step size,h

(480)

Euler Heun Midpoint Ralston

4802401206030

−987.84110.32546.77614.97632.77

−393.87584.27651.35649.91648.21

1208.4976.87690.20654.85649.02

449.78690.01667.71652.25648.61

http://numericalmethods.eng.usf.edu

Comparison of Euler and Runge-Kutta 2nd Order Methods

Table 2. Comparison of Euler and the Runge-Kutta methods

K57.647)480( (exact)

Page 21: Scientific Computing

http://numericalmethods.eng.usf.edu

Comparison of Euler and Runge-Kutta 2nd Order Methods

Table 2. Comparison of Euler and the Runge-Kutta methods

Step size,h Euler Heun Midpoint Ralston

480

240

120

60

30

252.54

82.964

15.566

5.0352

2.2864

160.82

9.7756

0.58313

0.36145

0.097625

86.61250.8516.58231.1239

0.22353

30.5446.55373.1092

0.722990.15940

K57.647)480( (exact)

%t

Page 22: Scientific Computing

http://numericalmethods.eng.usf.edu

Comparison of Euler and Runge-Kutta 2nd Order Methods

Figure 4. Comparison of Euler and Runge Kutta 2nd order methods with exact results.

500

600

700

800

900

1000

1100

1200

0 100 200 300 400 500 600

Time, t (sec)

Tem

pera

ture

,

Analytical

Ralston

Midpoint

Euler

Heun

θ(K

)

Page 23: Scientific Computing

Runge-Kutta Order 4 Method

hxx iii 1

where 6/)22( 4321 kkkki

),(1 ii xtfk

)2/,2/( 12 hkxhtfk ii

)2/,2/( 23 hkxhtfk ii

),( 34 hkxhtfk ii

different estimatesof slope

weighted average

Page 24: Scientific Computing

Runge-Kutta Order 4 Method

ti ti + h/2 ti + h

k1

k2

k3

k4

4321 226

1kkkkk

k

x

Page 25: Scientific Computing

Runge-Kutta Order 4 Example

• Consider Exact Solution

• The initial condition is:

• The step size is:

2txdt

dx t222 ettx

10 x

1.0h

Page 26: Scientific Computing

Runge-Kutta Order 4 Example

The example of a single step:

104829.122

6

109499.0104988.1,1.01.0,

104988.02/.1,05.01.02

1,

2

1

10475.005.1,05.01.02

1,

2

1

1.0011.01,01.0,

432101

34

223

12

21

kkkkh

xx

fkxhtfhhk

kfkxhtfhhk

fkxhtfhhk

fxtfhhk

Page 27: Scientific Computing

The RK values are nearly equivalent to those of the exact solution out to x=5.

y(5) = -111.4129 (-111.4132)

The error is small relative to the exact solution.

-10

-8

-6

-4

-2

0

2

4

0 1 2 3 4

x V

alu

e

t Value

4th Order Runge-Kutta Method

Exact

4th order

Runge-Kutta Order 4 Example

Page 28: Scientific Computing

A comparison between the 2nd order and the 4th order Runge-Kutta methods show a slight difference.

-10

-8

-6

-4

-2

0

2

4

0 1 2 3 4

Runge Kutta Comparison

Exact

2nd order

4th order 0.00

2.00

4.00

6.00

8.00

10.00

0 1 2 3 4 5

Ab

solu

te |E

rro

r|

t Value

Error of the Methods

Error 2nd order method

Error 4th order method

Runge-Kutta Order 4 Example

Page 29: Scientific Computing

ERRORS

1st-order

Local(single-step)

Global(multi-step)

2nd-order

4th-order

O(h2)

O(h3)

O(h5)

O(h) = O(1/n)

O(h2) = O(1/n2)

O(h4) = O(1/n4)

RKfunction

evals per step

1

2

4

h = (t f − t0) /nFor n steps,

Therefore, “global error”

=n *O(h p ) =t f − t0h

⎝ ⎜

⎠ ⎟*O(h p ) =O(h p−1)

Order of global error is 1 less than order of local error

“local error”

Page 30: Scientific Computing

A fair comparison between two RK methods must account the differing number of function calls per step

1st-order Euler with h = 0.01… global error = O(0.01)

4th-order RK with h = 0.04… global error = O(0.00000256)

t0 t1 t2 t3 t4 t5 t6 t7 t8

t0 t1 t2

Example: integrate from 0 to 4 using n=400 function evaluations

RK1 (Euler Method)- - 1 function call per step

RK4 - - 4 function calls per step

104 times less error for same amount of work FREE LUNCH !!!