cs537 numerical analysis - university of kentuckyjzhang/cs537/lecture7.pdftaylor series methods 8...

41
CS537 Numerical Analysis Lecture 7 Numerical Solution of Ordinary Differential Equations Professor Jun Zhang Department of Computer Science University of Kentucky Lexington, KY 40506-0633 April 7, 2019

Upload: others

Post on 20-Apr-2020

5 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: CS537 Numerical Analysis - University of Kentuckyjzhang/CS537/lecture7.pdfTaylor Series Methods 8 The Taylor series of a function gives the numerical solution of x(t+h) when we truncate

CS537

Numerical Analysis

Lecture 7

Numerical Solution of Ordinary Differential Equations

Professor Jun Zhang

Department of Computer ScienceUniversity of Kentucky

Lexington, KY 40506-0633

April 7, 2019

Page 2: CS537 Numerical Analysis - University of Kentuckyjzhang/CS537/lecture7.pdfTaylor Series Methods 8 The Taylor series of a function gives the numerical solution of x(t+h) when we truncate

What is ODE

2

An Ordinary Differential Equation (ODE) is an equation that involves one or more derivatives of an unknown function

A solution of a differential equation is a specific function that satisfies the equation

For the ODE

The solution is

where c is a constant. Note that the solution is not unique

Also, the function has derivatives only respect to one variable

For derivatives with multiple variables, the equation is usually called partial differential equation (PDE)

texdtdx =−

tt cetetx +=)(

Page 3: CS537 Numerical Analysis - University of Kentuckyjzhang/CS537/lecture7.pdfTaylor Series Methods 8 The Taylor series of a function gives the numerical solution of x(t+h) when we truncate

An Example of ODE

3

An ODE solution as a function of the time t

Page 4: CS537 Numerical Analysis - University of Kentuckyjzhang/CS537/lecture7.pdfTaylor Series Methods 8 The Taylor series of a function gives the numerical solution of x(t+h) when we truncate

Initial-Value Problem

4

A differential equation does not, in general, determine a unique solution function. Additional conditions are needed to determine a unique solution

The initial-value problem has the following standard form

Here x is a function of t, x(a) is the given initial value of the problem. We would like to determine the value of x at any time before or after a

Some examples of initial value problems and solutions

==

saxxtfx

)(),('

436)1(16'10)0(1'

SolutionValue InitialEquation

2 +−==−=−==+=ttxxtx

exxxx t

Page 5: CS537 Numerical Analysis - University of Kentuckyjzhang/CS537/lecture7.pdfTaylor Series Methods 8 The Taylor series of a function gives the numerical solution of x(t+h) when we truncate

Numerical Solution

5

Analytical solutions in closed forms are only available for special differential equations

Most differential equations encountered in practical applications do not have analytical solutions

Some analytical solutions may be too complicated to be useful in practice

A numerical solution of a differential equation is usually obtained in the form of a table of discrete values

Interpolation procedures can be used to obtain all values of the approximate numerical solution within a given interval

A large amount of data may be displayed as a solution curve on a color graphical monitor

Page 6: CS537 Numerical Analysis - University of Kentuckyjzhang/CS537/lecture7.pdfTaylor Series Methods 8 The Taylor series of a function gives the numerical solution of x(t+h) when we truncate

ODE and Intergration

6

Consider the differential equation

We integrate it from t to t+h

We obtain

Replacing the integral with one of the numerical integration rules we studied before, we obtain a formula for solving the differential equation

=

=

sax

xrfdrdx

)(

),(

++

=ht

t

ht

tdrrxrfdx ))(,(

+

+=+ht

tdrrxrftxhtx ))(,()()(

Page 7: CS537 Numerical Analysis - University of Kentuckyjzhang/CS537/lecture7.pdfTaylor Series Methods 8 The Taylor series of a function gives the numerical solution of x(t+h) when we truncate

Numerical Rules

7

Using the left rectangular approximation formula, we have

which leads to the Euler’s method

On the other hand, the trapezoid rule leads to

and the implicit formula,

because x(t+h) appears in both sides

+

≈ht

ttxthfdrrxrf ))(,())(,(

))](,())(,([2

)()( htxhtftxtfhtxhtx ++++=+

))](,())(,([2

))(,( htxhtftxtfhdrrxrfht

t+++≈

+

))(,()()( txthftxhtx +=+

Page 8: CS537 Numerical Analysis - University of Kentuckyjzhang/CS537/lecture7.pdfTaylor Series Methods 8 The Taylor series of a function gives the numerical solution of x(t+h) when we truncate

Taylor Series Methods

8

The Taylor series of a function

gives the numerical solution of x(t+h) when we truncate the series after the m+1 terms

If h is small, and if we know

the Taylor series computes very accurate value of x(t+h).

If we truncate the series after the m+1 term, the method is called the Taylor series method of order m

The Taylor series method of order 1 is known as the Euler’s method

++++

+++=+

)(!

1)(!4

1

)('"!3

1)(''!2

1)(')()(

)()4(4

32

txhm

txh

txhtxhthxtxhtx

mm

)(,),("),('),( )( txtxtxtx m

Page 9: CS537 Numerical Analysis - University of Kentuckyjzhang/CS537/lecture7.pdfTaylor Series Methods 8 The Taylor series of a function gives the numerical solution of x(t+h) when we truncate

Euler’s Method

9

The initial value problem

over the interval [a,b], we use the first two terms of the Taylor series

This gives the formula of the Euler’s method

The computation starts from t=a and stops at t=b with n steps of size

)(')()( thxtxhtx +≈+

==

axaxtxtfx

)())(,('

))(,()()( txthftxhtx +=+

nabh )( −=

Page 10: CS537 Numerical Analysis - University of Kentuckyjzhang/CS537/lecture7.pdfTaylor Series Methods 8 The Taylor series of a function gives the numerical solution of x(t+h) when we truncate

Euler’s method

10

Using Euler’s method to solve an ODE. The typical behavior of the computed solution is that it runs away from the exact solution as more steps are taken, due to the accumulation of errors at each step

Page 11: CS537 Numerical Analysis - University of Kentuckyjzhang/CS537/lecture7.pdfTaylor Series Methods 8 The Taylor series of a function gives the numerical solution of x(t+h) when we truncate

Taylor Series Method of Higher Order

11

Consider the initial-value problem

We differentiate the equation several times, as

These terms can be applied in order in the Taylor series method and the computed solution will be more accurate. For example x(2)=4.235841 from the Euler’s method. x(2)=4.3712096 from the use of the 4th

derivative. The exact value to five significant figures is x(2)=4.3712

4)1(1' 32

−=++=

xtxx

6"'6'"26''2"2'"

3'2"1'

)4(

2

32

++=++=

+=++=

xxxxxtxxxxx

txxxtxx

Page 12: CS537 Numerical Analysis - University of Kentuckyjzhang/CS537/lecture7.pdfTaylor Series Methods 8 The Taylor series of a function gives the numerical solution of x(t+h) when we truncate

Type of Errors

12

When we truncate the Taylor series, we introduce the local truncation error

The truncation error is of order h5 , or O(h5).

The second type of error is the accumulation effects of all local truncation errors

The computed value of x(t+h) is in error because x(t) is already in error, due to the previous truncation error, and the current step involves another truncation error

The roundoff error can also contribute to the accumulation of error

These errors may be magnified by succeeding steps

)(120

1 55 ξxh

Page 13: CS537 Numerical Analysis - University of Kentuckyjzhang/CS537/lecture7.pdfTaylor Series Methods 8 The Taylor series of a function gives the numerical solution of x(t+h) when we truncate

Runge-Kutta Methods

13

The high order Taylor method for solving the initial-value problem

needs the derivatives of x by differentiating the function f . This is not convenient. We need some highly accurate methods that make use of fdirectly

The Runge-Kutta methods are a class of methods using multiple evaluations of f, not its derivatives, to enhance computational accuracy

We will illustrate the procedure to derive the Runge-Kutta method of order 2, and give the formula for Runge-Kutta method of order 4, which is popularly used

We assume that f(t,x) can be computed for any (t,x), so that x(t), x(t+h), x(t+2h), …, can be computed

==

axaxxtfx

)(),('

Page 14: CS537 Numerical Analysis - University of Kentuckyjzhang/CS537/lecture7.pdfTaylor Series Methods 8 The Taylor series of a function gives the numerical solution of x(t+h) when we truncate

14

Taylor Series for f(x,y)

We can expand the Taylor series in two variables

The first few right-hand terms are

),(!1),(

0yxf

yk

xh

ikyhxf

i

i

=

∂∂+

∂∂=++

2),(

),(

),(

2

22

2

2

22

2

1

0

yfk

yxfhk

xfhyxf

yk

xh

yfk

xfhyxf

yk

xh

fyxfy

kx

h

∂∂+

∂∂∂+

∂∂=

∂∂+

∂∂

∂∂+

∂∂=

∂∂+

∂∂

=

∂∂+

∂∂

Page 15: CS537 Numerical Analysis - University of Kentuckyjzhang/CS537/lecture7.pdfTaylor Series Methods 8 The Taylor series of a function gives the numerical solution of x(t+h) when we truncate

15

Taylor Series for f(x,y)

The Taylor series can be truncated as

We now use subscripts to denote the partial derivatives

),(!

1),(!

1),(1

0yxf

yk

xh

nyxf

yk

xh

ikyhxf

nin

i

∂∂+

∂∂+

∂∂+

∂∂=++

=

( )

( )+

++++

+++

++=++

33!3

1

2!2

1

)(),(

3223

22

yyyxyyxxyxxx

yyxyxx

yx

fkfhkkfhfh

fkhkffh

kfhffkyhxf

++++=+

++++=+

yyyyyy

xxxxxx

fkfkkffkyxf

fhfhhffyhxf

!3!2),(

!3!2),(

32

32

Page 16: CS537 Numerical Analysis - University of Kentuckyjzhang/CS537/lecture7.pdfTaylor Series Methods 8 The Taylor series of a function gives the numerical solution of x(t+h) when we truncate

16

Runge-Kutta Method of Order 2

Define two function evaluations

We add the linear combination of these quantities to the value of x at t to obtain the value at t+h

or

We want to determine the constants in the above expression so that it will be as accurate as possible

In other words, we want the above expression to match the Taylor series to as many terms as possible

++==

),(),(

12

1

KxhthfKxthfK

βα

2211)()( KwKwtxhtx ++=+)),(,(),()()( 21 xthfxhthfwxthfwtxhtx βα ++++=+

Page 17: CS537 Numerical Analysis - University of Kentuckyjzhang/CS537/lecture7.pdfTaylor Series Methods 8 The Taylor series of a function gives the numerical solution of x(t+h) when we truncate

17

Euler’s Method & Runge-Kutta Method

The Euler’s method (left) requires one function evaluation at each stepThe second-order Runge-Kutta method (right) requires two function evaluations at each step

Page 18: CS537 Numerical Analysis - University of Kentuckyjzhang/CS537/lecture7.pdfTaylor Series Methods 8 The Taylor series of a function gives the numerical solution of x(t+h) when we truncate

18

Runge-Kutta Method of Order 2

Compare the intended formula

with the Taylor series

If we set w1=1 and w2=0, the two expressions agree to the h term

The formula obtained is

which is the Euler’s method, since

)),(,(),()()( 21 xthfxhthfwxthfwtxhtx βα ++++=+

++++=+ )('"!3

1)("!2

1)(')()( 2 txtxhthxtxhtx

),()()( xthftxhtx +=+

),()(' xtftx =

Page 19: CS537 Numerical Analysis - University of Kentuckyjzhang/CS537/lecture7.pdfTaylor Series Methods 8 The Taylor series of a function gives the numerical solution of x(t+h) when we truncate

19

Runge-Kutta Method of Order 2

Revisit the two variable Taylor series

Using this formula in

We have

Note that

),(21),(

2

yxfx

hft

ahhffhffhfxhtf xt

∂∂+

∂∂+++=++ ββαβα

ffftx

xf

dtdt

tf

dtxtdf

dtdxx xt +=

∂∂

∂∂+

∂∂=== ),('"

)),(,(),()()( 21 xthfxhthfwxthfwtxhtx βα ++++=+

)()()()( 322

2221 hOffhwfhwhfwwtxhtx xt +++++=+ βα

Page 20: CS537 Numerical Analysis - University of Kentuckyjzhang/CS537/lecture7.pdfTaylor Series Methods 8 The Taylor series of a function gives the numerical solution of x(t+h) when we truncate

20

Runge-Kutta Method of Order 2

The Taylor series equation

becomes

Compare the two expressions of x(t+h), we have

One solution is

Other solutions are possible, but the truncation errors will be the same

)(21

21)( 322 hOffhfhhfxhtx xt ++++=+

21

21 1 2221 ===+ wwww βα

++++=+ )('"!3

1)("!2

1)(')()( 32 txhtxhthxtxhtx

21

21 1 1 21 ==== wwβα

Page 21: CS537 Numerical Analysis - University of Kentuckyjzhang/CS537/lecture7.pdfTaylor Series Methods 8 The Taylor series of a function gives the numerical solution of x(t+h) when we truncate

21

Runge-Kutta Method of Order 2

So the Runge-Kutta Method of Order 2 is

Or equivalently

where

The solution function at t+h is computed at the expenses of two evaluations of the function f

The truncation error of the second-order Runge-Kutta method is O(h3)

)(21)()( 21 KKtxhtx ++=+

++==

),(),(

12

1

KxhthfKxthfK

)),(,(2

),(2

)()( xthfxhtfhxtfhtxhtx ++++=+

Page 22: CS537 Numerical Analysis - University of Kentuckyjzhang/CS537/lecture7.pdfTaylor Series Methods 8 The Taylor series of a function gives the numerical solution of x(t+h) when we truncate

22

Compare Euler‘s Method & Runge-Kutta Method

The initial-value problem is

1)0( ,3)(2)(' 4 =+−= − xetxtx t

Page 23: CS537 Numerical Analysis - University of Kentuckyjzhang/CS537/lecture7.pdfTaylor Series Methods 8 The Taylor series of a function gives the numerical solution of x(t+h) when we truncate

23

Errors in Euler‘s Method & Runge-Kutta Method

Implicit evaluation is done at h/2 step

Page 24: CS537 Numerical Analysis - University of Kentuckyjzhang/CS537/lecture7.pdfTaylor Series Methods 8 The Taylor series of a function gives the numerical solution of x(t+h) when we truncate

24

Runge-Kutta Method of Order 4

The most popular Runge-Kutta Method is that of order 4, as

where

This formula requires four function evaluations

The truncation error of the fourth-order Runge-Kutta method is O(h5)

( )432161)()( KKKKtxhtx ++++=+

( )

++=

++=

++=

=

34

23

12

1

,21,

21

21,

21

),(

KxhthfK

KxhthfK

KxhthfK

xthfK

Page 25: CS537 Numerical Analysis - University of Kentuckyjzhang/CS537/lecture7.pdfTaylor Series Methods 8 The Taylor series of a function gives the numerical solution of x(t+h) when we truncate

25

Fourth-Order Runge-Kutta Method

Illustration of Runge-Kutta method of order 4. Calculations are made at the initial time, two at half of the stepsize beyond the initial time and at the final time. These four calculations allow the use of larger overall stepsize with good accuracy

Page 26: CS537 Numerical Analysis - University of Kentuckyjzhang/CS537/lecture7.pdfTaylor Series Methods 8 The Taylor series of a function gives the numerical solution of x(t+h) when we truncate

26

Fourth-Order Runge-Kutta Method

The initial-value problem is 1)0( ,)(' =+= xxytx

Page 27: CS537 Numerical Analysis - University of Kentuckyjzhang/CS537/lecture7.pdfTaylor Series Methods 8 The Taylor series of a function gives the numerical solution of x(t+h) when we truncate

27

Some Amazing Simulation Results

Page 28: CS537 Numerical Analysis - University of Kentuckyjzhang/CS537/lecture7.pdfTaylor Series Methods 8 The Taylor series of a function gives the numerical solution of x(t+h) when we truncate

28

Adaptive Runge-Kutta Methods

In practical computations, we need to know the magnitude of errors involved in the computation

Given a tolerance of error, we want to be assured that the computed numerical solution must not deviate from the true solution beyond the tolerance

If a method is selected, the error tolerance dictates the allowable step size

A uniform step size may not be desirable

If the solution is smooth, a large step size may be used to reduce computational cost

It is preferable that the method can automatically adjust the step size during the computation

Page 29: CS537 Numerical Analysis - University of Kentuckyjzhang/CS537/lecture7.pdfTaylor Series Methods 8 The Taylor series of a function gives the numerical solution of x(t+h) when we truncate

29

Compute the Errors

To advance the solution curve from t to t+h, we can take one step of size husing the fourth-order Runge-Kutta method

We can also take two step of size h/2 to arrive at t+h

If there were no truncation errors, the values of the numerical solution x(t+h) computed from both procedures would be the same

The difference in the numerical results can be taken as an estimate of the local truncation error

If this error is within the prescribed tolerance, the current step size h is satisfactory. If the difference exceeds the tolerance, the step size is halved

This procedure is easily programmed but rather wasteful of computing time

Page 30: CS537 Numerical Analysis - University of Kentuckyjzhang/CS537/lecture7.pdfTaylor Series Methods 8 The Taylor series of a function gives the numerical solution of x(t+h) when we truncate

30

Fehlberg Method of Order 4

The formula is

where

5431 51

41042197

25651408

21625)()( KKKKtxhtx −+++=+

−+−++=

+−++=

+++=

++=

=

43215

3214

213

12

1

4104845

51336808

216439,

21977296

21977200

21971932,

1312

329

323,

83

41,

41

),(

KKKKxhthfK

KKKxhthfK

KKxhthfK

KxhthfK

xthfK

Page 31: CS537 Numerical Analysis - University of Kentuckyjzhang/CS537/lecture7.pdfTaylor Series Methods 8 The Taylor series of a function gives the numerical solution of x(t+h) when we truncate

31

Fehlberg Method of Order 4, RK45

The Fehlberg method of order 4 requires one more functional evaluation than the classical Runge-Kutta method, it is not very attractive

However, with one more functional evaluation, as

We can obtain the fifth-order Runge-Kutta method, i.e.,

The difference between the values of x(t+h) computed from the fourth and fifth order procedures is an estimate of the local truncation error in the

fourth-order procedure. So six functional evaluations give a fourth-order approximation, together with an error estimate

65431 552

509

5643028561

128256656

13516)()( KKKKKtxhtx +−+++=+

−+−+−+= 543216 40

1141041859

1282535442

278,

21 KKKKKxhthfK

Page 32: CS537 Numerical Analysis - University of Kentuckyjzhang/CS537/lecture7.pdfTaylor Series Methods 8 The Taylor series of a function gives the numerical solution of x(t+h) when we truncate

32

Adaptive Process

1. Given a step size h and an initial value x(t), the RK45routine computes the value x(t+h) and an error estimate

2. If , then the step size h is not changed and the next step size is taken by replacing step 1 with initial value x(t+h)

3. If , then h is replaced by 2h, provided

4. If , then h is replaced by h/2, provided

5. If , then the step size is repeated by returning to step 1 with x(t) and the new h value

Here are the maximum and minimum error tolerances allowed

are the maximum and minimum step sizes allowed

maxmin εεε ≤≤

minεε ≤ max|2| hh ≤

min|2/| hh ≥maxεε >

maxmin || hhh ≤≤

minmax,εε

minmax,hh

ε

Page 33: CS537 Numerical Analysis - University of Kentuckyjzhang/CS537/lecture7.pdfTaylor Series Methods 8 The Taylor series of a function gives the numerical solution of x(t+h) when we truncate

33

Uniform Method

Uniform method with h = 0.05 Errors in the 200 steps

Page 34: CS537 Numerical Analysis - University of Kentuckyjzhang/CS537/lecture7.pdfTaylor Series Methods 8 The Taylor series of a function gives the numerical solution of x(t+h) when we truncate

34

Adaptive Method

Adaptive method Errors in the 200 steps

Page 35: CS537 Numerical Analysis - University of Kentuckyjzhang/CS537/lecture7.pdfTaylor Series Methods 8 The Taylor series of a function gives the numerical solution of x(t+h) when we truncate

35

Step Sizes Used in Adaptive Method

Adaptive method Step sizes

Page 36: CS537 Numerical Analysis - University of Kentuckyjzhang/CS537/lecture7.pdfTaylor Series Methods 8 The Taylor series of a function gives the numerical solution of x(t+h) when we truncate

36

Adams-Bashforth-Moultin Formulas

Let us consider the first-order ordinary differential equation

If we assume the values of the unknown function have been computed at several points to the left of t, i.e., t, t-h, t-2h, …, t-(n-1)h, we want to compute x(t+h). Using the theorem of calculus, we have

where the abbreviation fj is

))(,()(' txtftx =

)(

))(,()(

)(')()(

1

=

+

+

+≈

+=

+=+

n

jjj

ht

t

ht

t

fctx

dssxsftx

dssxtxhtx

)))1((,)1(( hjtxhjtff j −−−−=

Page 37: CS537 Numerical Analysis - University of Kentuckyjzhang/CS537/lecture7.pdfTaylor Series Methods 8 The Taylor series of a function gives the numerical solution of x(t+h) when we truncate

37

Adams-Bashforth Formulas

The last step needs a rule of the form

We need to determine the n coefficients cj. We can insist on integrating each function exactly. The appropriate equation is

A linear system of the form Ax=b of n equations can be solved for nunknowns. The coefficients of the matrix A are

and the right-hand side is bi =1/i

−++−+≈1

0 21 )1()1()0()( nFcFcFcdrrF n

12 ,,,,1 −nrrr

=

−− ≤≤−=1

01

11 )1( )1(n

j

ij

i nijcdtr

1)1( −−= iij jA

Page 38: CS537 Numerical Analysis - University of Kentuckyjzhang/CS537/lecture7.pdfTaylor Series Methods 8 The Taylor series of a function gives the numerical solution of x(t+h) when we truncate

34

Adams-Moulton Formulas

In Adams-Moulton formulas, the quadrature is of the form

Use the change of variable from s to p given by s=hp-t, the new integral will be

We now have

=

−≈1

01

)2()(n

jj jGCdrrG

+1

0)( dpthpgh

[ ]

[ ])2(249)(5)(19)(924

)(

)3(9)2(37)(59)(5524

)(

htGhtGtGhtGhdrrG

htFhtFhtFtFhdrrF

ht

t

ht

t

−−−−++≈

−−−+−−≈

+

+

Page 39: CS537 Numerical Analysis - University of Kentuckyjzhang/CS537/lecture7.pdfTaylor Series Methods 8 The Taylor series of a function gives the numerical solution of x(t+h) when we truncate

Stability Analysis

39

For some initial value problems, a small error in the initial value may produce errors of large magnitude, and the computed solution is completely wrong

The truncation error in each step further deteriorates the computed solution

For other initial value problems, the situation is not that severe

Page 40: CS537 Numerical Analysis - University of Kentuckyjzhang/CS537/lecture7.pdfTaylor Series Methods 8 The Taylor series of a function gives the numerical solution of x(t+h) when we truncate

40

A Case of Diverging Solutions

The initial values are close to each other. But over the time, the solutions diverge to different paths

( )

Page 41: CS537 Numerical Analysis - University of Kentuckyjzhang/CS537/lecture7.pdfTaylor Series Methods 8 The Taylor series of a function gives the numerical solution of x(t+h) when we truncate

41

A Case of Converging Solutions

The initial values are quite different. But over the time, the solutions converge to something very close to each other

=( )