cse 330 : numerical methods lecture 17: solution of ordinary differential equations (a) euler’s...

27
CSE 330 : Numerical Methods Lecture 17: Solution of Ordinary Differential Equations (a) Euler’s Method (b) Runge-Kutta Method Dr. S. M. Lutful Kabir Visiting Research Professor, BRAC University & Professor (on leave) IICT, BUET 1 Prof. S. M. Lutful Kabir, BRAC University

Upload: ginger-davidson

Post on 23-Dec-2015

237 views

Category:

Documents


5 download

TRANSCRIPT

CSE 330 : Numerical Methods

Lecture 17: Solution of Ordinary Differential Equations (a) Euler’s Method

(b) Runge-Kutta Method

Dr. S. M. Lutful Kabir

Visiting Research Professor, BRAC University

& Professor (on leave) IICT, BUET

1Prof. S. M. Lutful Kabir, BRAC

University

Euler’s Method

Φ

Step size, h

x

y

x0,y0

True value

y1, Predicted

value

00,, yxyyxfdx

dy

Slope Run

Rise

01

01

xx

yy

00 , yxf

010001 , xxyxfyy

hyxfy 000 ,Figure 1 Graphical interpretation of the first step of Euler’s method

Prof. S. M. Lutful Kabir, BRAC University2

Euler’s Method

Φ

Step size

h

True Value

yi+1, Predicted value

yi

x

y

xi xi+1

Figure 2. General graphical interpretation of Euler’s method

hyxfyy iiii ,1

ii xxh 1

Prof. S. M. Lutful Kabir, BRAC University3

How to write Ordinary Differential Equation

Example

50,3.12 yeydx

dy x

is rewritten as

50,23.1 yyedx

dy x

In this case

yeyxf x 23.1,

How does one write a first order differential equation in the form of

yxfdx

dy,

Prof. S. M. Lutful Kabir, BRAC University4

ExampleA 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 Euler’s method. Assume a step size of

240h seconds.

Prof. S. M. Lutful Kabir, BRAC University5

Solution

K

f

htf

htf iiii

09.106

2405579.41200

24010811200102067.21200

2401200,01200

,

,

8412

0001

1

Step 1:

1 is the approximate temperature at 240240001 httt

K09.106240 1

8412 1081102067.2

dt

d

8412 1081102067.2, tf

Prof. S. M. Lutful Kabir, BRAC University6

Solution ContFor 09.106,240,1 11 ti

K

f

htf

32.110

240017595.009.106

240108109.106102067.209.106

24009.106,24009.106

,

8412

1112

Step 2:

2 is the approximate temperature at 48024024012 httt

K32.110480 2

Prof. S. M. Lutful Kabir, BRAC University7

Solution Cont

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

9282.21022067.000333.0tan8519.1300

300ln92593.0 31

t

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

K57.647)480(

Prof. S. M. Lutful Kabir, BRAC University8

Comparison of Exact and Numerical Solutions

Figure 3. Comparing exact and Euler’s method

0

200

400

600

800

1000

1200

1400

0 100 200 300 400 500

Time, t(sec)

Te

mp

era

ture

,

h=240

Exact Solution

θ(K

)

Prof. S. M. Lutful Kabir, BRAC University9

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

4802401206030

−987.81110.32546.77614.97632.77

1635.4537.26100.8032.60714.806

252.5482.96415.5665.03522.2864

Effect of step size

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

K57.647)480( (exact)

Prof. S. M. Lutful Kabir, BRAC University10

Comparison with exact results

-1500

-1000

-500

0

500

1000

1500

0 100 200 300 400 500

Time, t (sec)Tem

per

atu

re,

Exact solution

h=120h=240

h=480

θ(K

)

Figure 4. Comparison of Euler’s method with exact solution for different step sizes

Prof. S. M. Lutful Kabir, BRAC University11

Effects of step size on Euler’s Method

-1200

-800

-400

0

400

800

0 100 200 300 400 500

Step size, h (s) Te

mp

era

ture

, θ(K

)

Figure 5. Effect of step size in Euler’s method.

Prof. S. M. Lutful Kabir, BRAC University12

Errors in Euler’s Method

It can be seen that Euler’s method has large errors. This can be illustrated using Taylor series.

...!3

1

!2

1 31

,

3

32

1

,

2

2

1,

1 ii

yx

ii

yx

iiyx

ii xxdx

ydxx

dx

ydxx

dx

dyyy

iiiiii

...),(''!3

1),('

!2

1),( 3

12

111 iiiiiiiiiiiiii xxyxfxxyxfxxyxfyy

As you can see the first two terms of the Taylor series

hyxfyy iiii ,1

The true error in the approximation is given by

...

!3

,

!2

, 32

hyxf

hyxf

E iiiit

are the Euler’s method.

2hEt

Prof. S. M. Lutful Kabir, BRAC University13

Runge Kutta 2nd Order Method

Runge Kutta thought to consider upto second derivative terms in Taylor’s series

In that case the Eular’s Method will be extended to

21 ,

!2

1, hyxfhyxfyy iiiiii

But finding the second derivative is sometimes difficult

Hence they used the average of two approximate slopes as follows: hkkyy ii

211 2

1

2

1

ii yxfk ,1

hkyhxfk ii 12 ,

where,

14 Prof. S. M. Lutful Kabir, BRAC University

Runge Kutta Method (Heun’s Method)

x

y

xi xi+1

yi+1, predicted

yi

Figure 1 Runge-Kutta 2nd order method (Heun’s method)

hkyhxfSlope ii 1,

iiii yxfhkyhxfSlopeAverage ,,2

1 1

ii yxfSlope ,

Prof. S. M. Lutful Kabir, BRAC University15

ExampleA 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

Prof. S. M. Lutful Kabir, BRAC University16

SolutionStep 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

Prof. S. M. Lutful Kabir, BRAC University17

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

Prof. S. M. Lutful Kabir, BRAC University18

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

Prof. S. M. Lutful Kabir, BRAC University19

Effect of step size

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

Step size, h q(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)

Prof. S. M. Lutful Kabir, BRAC University20

21

Runge-Kutta 4th Order Method

where

hkkkkyy ii 43211 226

1

ii yxfk ,1

hkyhxfk ii 12 2

1,

2

1

hkyhxfk ii 23 2

1,

2

1

hkyhxfk ii 34 ,

For0)0(),,( yyyxf

dx

dy

Runge Kutta 4th order method is given by

Prof. S. M. Lutful Kabir, BRAC University

22

ExampleA 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 Runge-Kutta 4th order method.

240h seconds.

8412 1081102067.2

dt

d

8412 1081102067.2, tf

Assume a step size of

hkkkkii 43211 226

1

Prof. S. M. Lutful Kabir, BRAC University

23

SolutionStep 1: 1200)0(,0,0 00 ti

5579.410811200102067.21200,0, 841201 ftfk o

38347.0108105.653102067.205.653,120

2405579.42

11200,240

2

10

2

1,

2

1

8412

1002

f

fhkhtfk

8954.310810.1154102067.20.1154,120

24038347.02

11200,240

2

10

2

1,

2

1

8412

2003

f

fhkhtfk

0069750.0108110.265102067.210.265,240

240984.31200,2400,8412

3004

f

fhkhtfk

Prof. S. M. Lutful Kabir, BRAC University

24

Solution Cont

1 is the approximate temperature at

240240001 httt

K65.675240 1

K

hkkkk

65.675

2401848.26

11200

240069750.08954.3238347.025579.46

11200

226

1432101

Prof. S. M. Lutful Kabir, BRAC University

25

Comparison with exact results

Figure 1. Comparison of Runge-Kutta 4th order method with exact solution

-400

0

400

800

1200

1600

0 200 400 600

Time,t(sec)

Tem

pera

ture

,

h=120Exact

h=240

h=480

θ(K

)

Prof. S. M. Lutful Kabir, BRAC University

Step size, h

(480) Et |єt|%

4802401206030

−90.278594.91646.16647.54647.57

737.8552.6601.4122

0.0336260.00086900

113.948.1319

0.218070.0051926

0.00013419

26

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

K57.647)480( (exact)

Prof. S. M. Lutful Kabir, BRAC University

THANKS

Prof. S. M. Lutful Kabir, BRAC University27