44
王王王 (Chun-Hsin Wang) 王王王王 王王王王王 Fall 2002 Chap 2 Numerical Methods for First-Order Differential Equations

王俊鑫 (Chun-Hsin Wang) 中華大學 資訊工程系 Fall 2002 Chap 2 Numerical Methods for First-Order Differential Equations

  • View
    247

  • Download
    0

Embed Size (px)

Citation preview

Page 1: 王俊鑫 (Chun-Hsin Wang) 中華大學 資訊工程系 Fall 2002 Chap 2 Numerical Methods for First-Order Differential Equations

王俊鑫 (Chun-Hsin Wang)

中華大學 資訊工程系

Fall 2002

Chap 2 Numerical Methods for First-Order

Differential Equations

Chap 2 Numerical Methods for First-Order

Differential Equations

Page 2: 王俊鑫 (Chun-Hsin Wang) 中華大學 資訊工程系 Fall 2002 Chap 2 Numerical Methods for First-Order Differential Equations

Page 2

Outline

Direction Field Methods Using dfield

(MATLAB / java)

Euler’s Method

Heun’s Method

Runge-Kutta Method of Order 2

Runge-Kutta Method of Order 4

Page 3: 王俊鑫 (Chun-Hsin Wang) 中華大學 資訊工程系 Fall 2002 Chap 2 Numerical Methods for First-Order Differential Equations

Page 3

Direction Field

General Forms for First-Order ODE

Implicit Form

Explicit Form

0),,( yyxF

),( yxfy

Page 4: 王俊鑫 (Chun-Hsin Wang) 中華大學 資訊工程系 Fall 2002 Chap 2 Numerical Methods for First-Order Differential Equations

Page 4

Direction Field

dfield is an interactive tool for studying

single first order differential equations.

Written in MATLAB by John C. Polking

http://math.rice.edu/~polking/

http://math.rice.edu/~dfield/dfpp.html

(java version)

Page 5: 王俊鑫 (Chun-Hsin Wang) 中華大學 資訊工程系 Fall 2002 Chap 2 Numerical Methods for First-Order Differential Equations

Page 5

Starting Difeld Tool

Page 6: 王俊鑫 (Chun-Hsin Wang) 中華大學 資訊工程系 Fall 2002 Chap 2 Numerical Methods for First-Order Differential Equations

Page 6

Direction Field Methods Using dfield tool

Page 7: 王俊鑫 (Chun-Hsin Wang) 中華大學 資訊工程系 Fall 2002 Chap 2 Numerical Methods for First-Order Differential Equations

Page 7

Direction Field Using Dfield

Page 8: 王俊鑫 (Chun-Hsin Wang) 中華大學 資訊工程系 Fall 2002 Chap 2 Numerical Methods for First-Order Differential Equations

Page 8

Print Direction Field Using dfield

Page 9: 王俊鑫 (Chun-Hsin Wang) 中華大學 資訊工程系 Fall 2002 Chap 2 Numerical Methods for First-Order Differential Equations

Page 9

Pint The Direction Field

Use the print screen function key (PtrSc) to save the picture of your computer screen into the clipboard automatically.

Paste the picture to a graphic tool (ex. 小畫家 ) Use the graph tool to cut the direction field you want Paste the direction field to a new file and then print it Note : verify your picture by using print review

Page 10: 王俊鑫 (Chun-Hsin Wang) 中華大學 資訊工程系 Fall 2002 Chap 2 Numerical Methods for First-Order Differential Equations

Page 10

Plot a particular solution

Page 11: 王俊鑫 (Chun-Hsin Wang) 中華大學 資訊工程系 Fall 2002 Chap 2 Numerical Methods for First-Order Differential Equations

Page 11

Plot a particular solution

Page 12: 王俊鑫 (Chun-Hsin Wang) 中華大學 資訊工程系 Fall 2002 Chap 2 Numerical Methods for First-Order Differential Equations

Page 12

Plot a particular solution

Page 13: 王俊鑫 (Chun-Hsin Wang) 中華大學 資訊工程系 Fall 2002 Chap 2 Numerical Methods for First-Order Differential Equations

Page 13

Enter Text Annotation

Page 14: 王俊鑫 (Chun-Hsin Wang) 中華大學 資訊工程系 Fall 2002 Chap 2 Numerical Methods for First-Order Differential Equations

Page 14

Enter Text Annotation

Page 15: 王俊鑫 (Chun-Hsin Wang) 中華大學 資訊工程系 Fall 2002 Chap 2 Numerical Methods for First-Order Differential Equations

Page 15

Page 16: 王俊鑫 (Chun-Hsin Wang) 中華大學 資訊工程系 Fall 2002 Chap 2 Numerical Methods for First-Order Differential Equations

Page 16

Direction Field Methods

Exercise 2-1: Plot the particular solutions for the

following ODEs and initial conditions using dfield

4)0( ,2 xtx

2)0( ,2 xxx

2)2( ,/ xxtx

0)0( , xtxx

Page 17: 王俊鑫 (Chun-Hsin Wang) 中華大學 資訊工程系 Fall 2002 Chap 2 Numerical Methods for First-Order Differential Equations

Page 17

Page 18: 王俊鑫 (Chun-Hsin Wang) 中華大學 資訊工程系 Fall 2002 Chap 2 Numerical Methods for First-Order Differential Equations

Page 18

Numerical Approximation

To numerically “solve” y = f(x, y) with y(a) = y0 on the interval [a, b], we find a discrete set of points

a = x0 < x1 < x2 < · · · < xN-1 < xN = b

and values y0, y1, y2, . . . , yN-1, yN

with yj approximately equal to y(xj).

Making an error Ej = y(xj) - yj

Page 19: 王俊鑫 (Chun-Hsin Wang) 中華大學 資訊工程系 Fall 2002 Chap 2 Numerical Methods for First-Order Differential Equations

Page 19

Euler, Léonard

Euler, Léonard1707-1783

Page 20: 王俊鑫 (Chun-Hsin Wang) 中華大學 資訊工程系 Fall 2002 Chap 2 Numerical Methods for First-Order Differential Equations

Page 20

Euler’s Method

Problem: Solve ( approximately )

y = f(x, y) with y(a) = y0

on the interval [a, b]. Discrete set of values

x0 = a, fixed step size h = (b - a)/N

x1 = x0 + h, x2 = x1 + h = x0 + 2h, etc,

xN = a + Nh = b

Page 21: 王俊鑫 (Chun-Hsin Wang) 中華大學 資訊工程系 Fall 2002 Chap 2 Numerical Methods for First-Order Differential Equations

Page 21

Euler’s Method

At each step approximate the solution curve by the tangent line.

hxxhxyxyhxy 01000 ,)(')()(

hyxfyxy ),()( 0001

11

0001

)(

,),(

yxyso

hyxfyySet

Page 22: 王俊鑫 (Chun-Hsin Wang) 中華大學 資訊工程系 Fall 2002 Chap 2 Numerical Methods for First-Order Differential Equations

Page 22

Euler’s Method

Euler’s Method solves the first order

differential equation y' = f(x,y) by a

simple stepping procedure using the first

two terms of a Taylor expansion of the

function y(x).

Also called Taylor Series Method (of

order 1) or Stepping Method.

Page 23: 王俊鑫 (Chun-Hsin Wang) 中華大學 資訊工程系 Fall 2002 Chap 2 Numerical Methods for First-Order Differential Equations

Page 23

Euler’s Method

Local Error

) (!3

1) (

!2

1) ( ' ) ( ) (

3 2x y h x y h x y h x y h x y

),(1 nnnnnn yxfhyyhyy

Taylor Series

Euler’s Method

) ( 3 2

!

1

!3

1

!2

1

mn

mn ny h

my h y h Error Local

Local Error = Taylor Series - Euler’s Method

Page 24: 王俊鑫 (Chun-Hsin Wang) 中華大學 資訊工程系 Fall 2002 Chap 2 Numerical Methods for First-Order Differential Equations

Page 24

Euler’s Method

Local Error

Page 25: 王俊鑫 (Chun-Hsin Wang) 中華大學 資訊工程系 Fall 2002 Chap 2 Numerical Methods for First-Order Differential Equations

Page 25

Euler’s Method

Exercise 2-3:

Sol:

)1( ,10,1)0(, yFindnyyxy

12)( xexy x

3620.1)2200.12.0(1.02200.1

2200.1)1000.11.0(1.01000.1

1000.1)0000.10.0(1.00000.1

3

2

1

y

y

y

Note: Exact Solution

Page 26: 王俊鑫 (Chun-Hsin Wang) 中華大學 資訊工程系 Fall 2002 Chap 2 Numerical Methods for First-Order Differential Equations

Page 26

Euler’s Method

Cumulative Error

Page 27: 王俊鑫 (Chun-Hsin Wang) 中華大學 資訊工程系 Fall 2002 Chap 2 Numerical Methods for First-Order Differential Equations

Page 27

Euler’s Method

Cumulative Error (Global Error)

Page 28: 王俊鑫 (Chun-Hsin Wang) 中華大學 資訊工程系 Fall 2002 Chap 2 Numerical Methods for First-Order Differential Equations

Page 28

Euler’s Method

Cumulative Error

Page 29: 王俊鑫 (Chun-Hsin Wang) 中華大學 資訊工程系 Fall 2002 Chap 2 Numerical Methods for First-Order Differential Equations

Page 29

Euler’s Method

Cumulative Error

Page 30: 王俊鑫 (Chun-Hsin Wang) 中華大學 資訊工程系 Fall 2002 Chap 2 Numerical Methods for First-Order Differential Equations

Page 30

Euler’s Method

MATLAB Code

Page 31: 王俊鑫 (Chun-Hsin Wang) 中華大學 資訊工程系 Fall 2002 Chap 2 Numerical Methods for First-Order Differential Equations

Page 31

Heun’s Method

Also called Improved Euler’s Method

11

1 ),(

khyu

yxfk

nn

nn

211

12

2

),(

kkh

yy

uhxfk

nn

nn

Euler’s Method

Heun’s Method(Based on Trapezoid Rule)

Page 32: 王俊鑫 (Chun-Hsin Wang) 中華大學 資訊工程系 Fall 2002 Chap 2 Numerical Methods for First-Order Differential Equations

Page 32

Heun’s Method

Average Slope

Page 33: 王俊鑫 (Chun-Hsin Wang) 中華大學 資訊工程系 Fall 2002 Chap 2 Numerical Methods for First-Order Differential Equations

Page 33

Heun’s Method

Example 2-4

Page 34: 王俊鑫 (Chun-Hsin Wang) 中華大學 資訊工程系 Fall 2002 Chap 2 Numerical Methods for First-Order Differential Equations

Page 34

Heun’s Method

MATLAB Code

Page 35: 王俊鑫 (Chun-Hsin Wang) 中華大學 資訊工程系 Fall 2002 Chap 2 Numerical Methods for First-Order Differential Equations

Page 35

Runge-Kutta Methods

Runge-Kutta Method is more accurate

than Heun’s Method

Developed by two German mathematicians

Carl Runge (1856-1927)

Wilhelm Kutta (1867-1944)

Page 36: 王俊鑫 (Chun-Hsin Wang) 中華大學 資訊工程系 Fall 2002 Chap 2 Numerical Methods for First-Order Differential Equations

Page 36

Runge-Kutta vs Euler

Both use a fixed step size h = (b - a)/N. Euler’s method

Yn+1 = yn + f(xn, yn) h

Runge-Kutta methods

Yn+1 = yn + S · h S is a weighted average of two or more

slopes. Slopes chosen to increase the accuracy.

Page 37: 王俊鑫 (Chun-Hsin Wang) 中華大學 資訊工程系 Fall 2002 Chap 2 Numerical Methods for First-Order Differential Equations

Page 37

Runge-Kutta Methods

積分定義積分定義 泰勒展開式泰勒展開式

TrapezoidRule

TrapezoidRule

Simpson’sRule

Simpson’sRule 二項展開二項展開 四項展開四項展開

Runge-KuttaOrder 2

Runge-KuttaOrder 2

Runge-KuttaOrder 4

Runge-KuttaOrder 4

Page 38: 王俊鑫 (Chun-Hsin Wang) 中華大學 資訊工程系 Fall 2002 Chap 2 Numerical Methods for First-Order Differential Equations

Page 38

Runge-Kutta Method of Order 2

),( yxfy

1

)()()( 1

n

n

x

xnn dxxyxyxy

)(2

1

,2

)),(),((

),()()(

21

11

1

1

KK

RuleTrapezoid yxfyxfh

dxyxfxyxy

nnnn

x

xnn

n

n

),(1 nn yxfhK

),( 12 KyhxfhK nn

Page 39: 王俊鑫 (Chun-Hsin Wang) 中華大學 資訊工程系 Fall 2002 Chap 2 Numerical Methods for First-Order Differential Equations

Page 39

Runge-Kutta Method of Order 2

Example 2-5 ?)1(,2,1)0(, ynyyxy

Round 1 Round 2dX 0.5 0.5x 0 0.5yn 1 1.75K1 0.5(0+1)=0.5 0.5*(0.5+1.75)=1.125K2 0.5*(0+0.5+1+0.5)=1 0.5*(0.5+0.5+1.75+1.125)=1.9375

yn+1 1+(0.5+1)/2=1.75 1.75+(1.125+1.9375)/2=3.28125

Page 40: 王俊鑫 (Chun-Hsin Wang) 中華大學 資訊工程系 Fall 2002 Chap 2 Numerical Methods for First-Order Differential Equations

Page 40

Runge-Kutta Method of Order 4

),( yxfy

1

)()()( 1

n

n

x

xnn dxxyxyxy

)22(6

16

),()2

,2

(4),(

),()()(

4321

1111

1

1

KKKK

yxfyyxx

fyxfh

dxyxfxyxy

nnnnnn

nn

x

xnn

n

n

Simpson’s Rule

Page 41: 王俊鑫 (Chun-Hsin Wang) 中華大學 資訊工程系 Fall 2002 Chap 2 Numerical Methods for First-Order Differential Equations

Page 41

Runge-Kutta Method of Order 4

),(1 nn yxfhK

)2

,2

( 12

Ky

hxfhK nn

)2

,2

( 23

Ky

hxfhK nn

),( 34 KyhxfhK nn

Page 42: 王俊鑫 (Chun-Hsin Wang) 中華大學 資訊工程系 Fall 2002 Chap 2 Numerical Methods for First-Order Differential Equations

Page 42

Runge-Kutta Method of Order 4

Example 2-6

Page 43: 王俊鑫 (Chun-Hsin Wang) 中華大學 資訊工程系 Fall 2002 Chap 2 Numerical Methods for First-Order Differential Equations

Page 43

Runge-Kutta Method of Order 4

MATLAB Code

Page 44: 王俊鑫 (Chun-Hsin Wang) 中華大學 資訊工程系 Fall 2002 Chap 2 Numerical Methods for First-Order Differential Equations

Page 44

References

Dormand, J. R. and P. J. Prince, “A family of embedded Runge-Kutta

formulae,” J. Comp. Appl. Math., Vol. 6, 1980, pp 19–26.

Bogacki, P. and L. F. Shampine, “A 3(2) pair of Runge-Kutta formulas,”

Appl. Math. Letters, Vol. 2, 1989, pp 1–9.

Kahaner, D. , C. Moler, and S. Nash, Numerical Methods and Software,

Prentice-Hall, New Jersey, 1989.

Shampine, L. F. , Numerical Solution of Ordinary Differential Equations,

Chapman & Hall, New York, 1994.

Shampine, L. F. and M. W. Reichelt, “The MATLAB ODE Suite,” (to appear

in SIAM Journal on Scientific Computing, Vol. 18-1, 1997).

Shampine, L. F. and M. E. Hosea, “Analysis and Implementation of TR-

BDF2,” Applied Numerical Mathematics 20, 1996.