29
Numerical Differentiation and Integration Lecture 8: Numerical Differentiation MTH2212 – Computational Methods and Statistics

Lecture08 - Numerical Differentiation

  • Upload
    na2ry

  • View
    1.043

  • Download
    43

Embed Size (px)

Citation preview

Page 1: Lecture08 - Numerical Differentiation

Numerical Differentiation and Integration

Lecture 8:Numerical Differentiation

MTH2212 – Computational Methods and Statistics

Page 2: Lecture08 - Numerical Differentiation

Dr. M. HrairiDr. M. Hrairi MTH2212 - Computational Methods and StatisticsMTH2212 - Computational Methods and Statistics 22

Objectives

Introduction Taylor Series Forward difference method Backward difference method Central difference method

Page 3: Lecture08 - Numerical Differentiation

Dr. M. HrairiDr. M. Hrairi MTH2212 - Computational Methods and StatisticsMTH2212 - Computational Methods and Statistics 33

Introduction

Calculus is the mathematics of change. Because engineers must continuously deal with systems and processes that change, calculus is an essential tool of engineering.

Standing in the heart of calculus are the mathematical concepts of differentiation and integration:

b

a

iix

ii

dxxfI

x

xfxxf

dx

dyx

xfxxf

x

y

)(

)()(lim

)()(

0

Page 4: Lecture08 - Numerical Differentiation

Dr. M. HrairiDr. M. Hrairi MTH2212 - Computational Methods and StatisticsMTH2212 - Computational Methods and Statistics 44

Introduction

Integration and differentiation are closely linked processes. They are, in fact, inversely related.

Types of functions to be differentiated or integrated:1. Simple polynomial, exponential, trigonometric

analytically2. Complex function numerically3. Tabulated function of experimental data numerically

Page 5: Lecture08 - Numerical Differentiation

Dr. M. HrairiDr. M. Hrairi MTH2212 - Computational Methods and StatisticsMTH2212 - Computational Methods and Statistics 55

Applications

Differentiation has so many engineering applications (heat transfer, fluid dynamics, chemical reaction kinetics, etc…)

Integration is equally used in engineering (compute work in ME, nonuniform force in SE, cross-sectional area of a river, etc…)

Page 6: Lecture08 - Numerical Differentiation

Dr. M. HrairiDr. M. Hrairi MTH2212 - Computational Methods and StatisticsMTH2212 - Computational Methods and Statistics 66

Differentiation

The finite difference becomes a derivative as Δx approaches zero.

x

xfxxf

x

y ii

)()(

x

xfxxf

dx

dy ii

x

)()(lim

0

Page 7: Lecture08 - Numerical Differentiation

Dr. M. HrairiDr. M. Hrairi MTH2212 - Computational Methods and StatisticsMTH2212 - Computational Methods and Statistics 77

Integration

Page 8: Lecture08 - Numerical Differentiation

Dr. M. HrairiDr. M. Hrairi MTH2212 - Computational Methods and StatisticsMTH2212 - Computational Methods and Statistics 88

Differentiation vs. Integration

Page 9: Lecture08 - Numerical Differentiation

Dr. M. HrairiDr. M. Hrairi MTH2212 - Computational Methods and StatisticsMTH2212 - Computational Methods and Statistics 99

Taylor Series Expansion

Non-elementary functions such as trigonometric, exponential, and others are expressed in an approximate fashion using Taylor series when their values, derivatives, and integrals are computed.

Any smooth function can be approximated as a polynomial. Taylor series provides a means to predict the value of a function at one point in terms of the function value and its derivatives at another point.

Page 10: Lecture08 - Numerical Differentiation

Dr. M. HrairiDr. M. Hrairi MTH2212 - Computational Methods and StatisticsMTH2212 - Computational Methods and Statistics 1010

Numerical Application of Taylor Series

If f (x) and its first n+1 derivatives are continuous on an interval containing xi+1 and xi , then:

Where the remainder Rn is defined as:

nn

iii

n

iii

iii

iiiii

Rxxn

xfxx

xf

xxxf

xxxfxfxf

13

1

3

21

''

1'

1

! ...

!3

!2

1

1

1

!1

n

ii

n

n xxn

fR

Page 11: Lecture08 - Numerical Differentiation

Dr. M. HrairiDr. M. Hrairi MTH2212 - Computational Methods and StatisticsMTH2212 - Computational Methods and Statistics 1111

Numerical Application of Taylor Series

The series is built term by term

Continuing the addition of more terms to get better approximation we have:

ii xfxf 1

iiiii xxxfxfxf 1'

1

21

''

1'

1 !2 iii

iiiii xxxf

xxxfxfxf

zero order approximation

1st order approximation

2nd order approximation

Page 12: Lecture08 - Numerical Differentiation

Dr. M. HrairiDr. M. Hrairi MTH2212 - Computational Methods and StatisticsMTH2212 - Computational Methods and Statistics 1212

Numerical Application of Taylor Series

Finally

where

ξ is a value of x that lies somewhere between xi and xi+1.

nn

iii

n

iii

iii

iiiii

Rxxn

xfxx

xf

xxxf

xxxfxfxf

13

1

3

21

''

1'

1

! ...

!3

!2

1

1

1

!1

n

ii

n

n xxn

fR

Page 13: Lecture08 - Numerical Differentiation

Dr. M. HrairiDr. M. Hrairi MTH2212 - Computational Methods and StatisticsMTH2212 - Computational Methods and Statistics 1313

Taylor Series ξ in the Remainder Term

Limitations ξ is not exactly known but lies somewhere between xi and xi+1

To evaluate Rn, the (n+1) derivative of f (x) has to be determined. To do this f (x) must be known

if f (x) was known there would be no need to perform the Taylor series expansion!!!

ModificationRn = O(hn+1) the truncation error is of the order of hn+1 . (h = xi+1

– xi) If the error is O(h), halving the step size will halve the error. If the error is O(h2), halving the step size will quarter the

error. In general, the truncation error is decreased by addition of

more terms in the Taylor series.

Page 14: Lecture08 - Numerical Differentiation

Dr. M. HrairiDr. M. Hrairi MTH2212 - Computational Methods and StatisticsMTH2212 - Computational Methods and Statistics 1414

Forward Difference Formulas- 1st derivative

2nd order Taylor series expansion of f(x) can be written as:

Then, the first derivative can be expressed as:

Given that f“(x) can be approximated by:

)(2

)('')(')()( 32

1 hOhxf

hxfxfxf iiii

)(2

)('')()()(' 21 hOh

xf

h

xfxfxf iiii

)()()(2)(

)(''2

12 hOh

xfxfxfxf iiii

(1)

(2)

(3)

Page 15: Lecture08 - Numerical Differentiation

Dr. M. HrairiDr. M. Hrairi MTH2212 - Computational Methods and StatisticsMTH2212 - Computational Methods and Statistics 1515

Forward Difference Formulas- 1st derivative

Substituting equation (3) into equation (2):

Collecting terms and simplifying, we have:

Note that the inclusion of the second-derivative term has improved the accuracy to O(h2).

)(2

)()(2)()()()(' 2

2121 hOh

h

xfxfxf

h

xfxfxf iiiiii

)(2

)(3)(4)()(' 212 hO

h

xfxfxfxf iiii

Page 16: Lecture08 - Numerical Differentiation

Dr. M. HrairiDr. M. Hrairi MTH2212 - Computational Methods and StatisticsMTH2212 - Computational Methods and Statistics 1616

Forward Difference Formulas- 2nd derivative

Start with Lagrange interpolation polynomial for f(x) based on the four points xi, xi+1, xi+2 and xi+3 .

Differentiate the products in the numerators twice Substitute x = xi and consider the fact that xj - xi

=(j- i)h The expression of the second derivative is then:

)()(2)(5)(4)(

)('' 22

123 hOh

xfxfxfxfxf iiiii

Page 17: Lecture08 - Numerical Differentiation

Dr. M. HrairiDr. M. Hrairi MTH2212 - Computational Methods and StatisticsMTH2212 - Computational Methods and Statistics 1717

Backward Difference Formulas- 1st derivative

Using backward difference in the Taylor series expansion,

And given that f“(x) can be approximated by:

The second-order estimate of f’(x) can be obtained:)(

2

)()(4)(3)(' 221 hO

h

xfxfxfxf iiii

)(2

)('')(')()( 32

1 hOhxf

hxfxfxf iiii

)()()(2)(

)(''2

21 hOh

xfxfxfxf iiii

Page 18: Lecture08 - Numerical Differentiation

Dr. M. HrairiDr. M. Hrairi MTH2212 - Computational Methods and StatisticsMTH2212 - Computational Methods and Statistics 1818

Backward Difference Formulas- 2nd derivative

Start with Lagrange interpolation polynomial for f(x) based on the four points xi, xi-1, xi-2 and xi-3 .

Differentiate the products in the numerators twice Substitute x = xi and consider the fact that xj - xi

=(j- i)h The expression of the second derivative is then:

)()()(4)(5)(2

)('' 22

321 hOh

xfxfxfxfxf iiiii

Page 19: Lecture08 - Numerical Differentiation

Dr. M. HrairiDr. M. Hrairi MTH2212 - Computational Methods and StatisticsMTH2212 - Computational Methods and Statistics 1919

Centered Difference Formulas- 1st derivative [O(h2)]

Start with the 2nd degree Taylor expansions about x for f(x+h) and f(x-h):

Subtract (5) from (4)

Hence

)(2

)('')(')()( 32

1 hOhxf

hxfxfxf iiii

)(2

)('')(')()( 32

1 hOhxf

hxfxfxf iiii

(4)

(5)

)()('2)()( 311 hOhxfxfxf iii

)(2

)()()(' 211 hO

h

xfxfxf iii

Page 20: Lecture08 - Numerical Differentiation

Dr. M. HrairiDr. M. Hrairi MTH2212 - Computational Methods and StatisticsMTH2212 - Computational Methods and Statistics 2020

Centered Difference Formulas- 1st derivative [O(h4)]

Start with the difference between the 4th degree Taylor expansions about x for f(x+h) and f(x-h):

Use the step size 2h, instead of h, in (6)

Multiply equation (6) by 8, subtract (7) from it, and solve for f’(x)

)(!3

)('''2)('2)()( 53

11 hOhxf

hxfxfxf iiii (6

)

)(!3

)('''16)('4)()( 53

22 hOhxf

hxfxfxf iiii (7

)

)(12

)()(8)(8)()(' 42112 hO

h

xfxfxfxfxf iiiii

Page 21: Lecture08 - Numerical Differentiation

Dr. M. HrairiDr. M. Hrairi MTH2212 - Computational Methods and StatisticsMTH2212 - Computational Methods and Statistics 2121

Centered Difference Formulas- 2nd derivative [O(h2)]

Start with the 3rd degree Taylor expansions about x for f(x+h) and f(x-h):

Add equations (8) and (9), and solve for f’’(x)

)(!3

)('''

2

)('')(')()( 432

1 hOhxf

hxf

hxfxfxf iiiii (8

)

)()()(2)(

)('' 22

11 hOh

xfxfxfxf iiii

)(!3

)('''

2

)('')(')()( 432

1 hOhxf

hxf

hxfxfxf iiiii (9

)

Page 22: Lecture08 - Numerical Differentiation

Dr. M. HrairiDr. M. Hrairi MTH2212 - Computational Methods and StatisticsMTH2212 - Computational Methods and Statistics 2222

Centered Difference Formulas- 2nd derivative [O(h4)]

Start with the addition between the 5th degree Taylor expansions about x for f(x+h) and f(x-h):

Use the step size 2h, instead of h, in (10)

Multiply equation (10) by 16, subtract (11) from it, and solve for f’’(x)

)(!4

)(2

!2

)(''2)(2)()( 64

)4(2

11 hOhxf

hxf

xfxfxf iiiii (10

)

(11)

)(12

)()(16)(30)(16)()('' 4

22112 hO

h

xfxfxfxfxfxf iiiiii

)(!4

)(32

!2

)(''8)(2)()( 64

)4(2

22 hOhxf

hxf

xfxfxf iiiii

Page 23: Lecture08 - Numerical Differentiation

Dr. M. HrairiDr. M. Hrairi MTH2212 - Computational Methods and StatisticsMTH2212 - Computational Methods and Statistics 2323

Error Analysis

Generally, if numerical differentiation is performed, only about half the accuracy of which the computer is capable is obtained unless we are fortunate to find an optimal step size.

The total error has part due to round-off error plus a part due to truncation error.

Page 24: Lecture08 - Numerical Differentiation

Dr. M. HrairiDr. M. Hrairi MTH2212 - Computational Methods and StatisticsMTH2212 - Computational Methods and Statistics 2424

Total Numerical Error

Total numerical error = truncation error + round-off error.

Trade-off between truncation and round-off errors

Page 25: Lecture08 - Numerical Differentiation

Dr. M. HrairiDr. M. Hrairi MTH2212 - Computational Methods and StatisticsMTH2212 - Computational Methods and Statistics 2525

Example 1

Estimate the first and second derivatives of:

at x = 0.5 and h = 0.25 using

a) forward finite-divided difference

b) Centered finite-divided difference

c) backward finite-divided difference?

432 1.015.05.025.02.1)( xxxxxf

Page 26: Lecture08 - Numerical Differentiation

Dr. M. HrairiDr. M. Hrairi MTH2212 - Computational Methods and StatisticsMTH2212 - Computational Methods and Statistics 2626

Example 1 - Solution

a) Forward difference 1st derivative computationThe data needed is:

First derivative:

2.0)( 1

636328.0)( 75.0

925.0)( 5.0

22

11

ii

ii

ii

xfx

xfx

xfx

859375.0)25.0(2

)5.0(3)75.0(4)1()5.0('

2

)(3)(4)()(' 12

ffff

h

xfxfxfxf iiii

εt = 5.82%

True value=-0.9125

Page 27: Lecture08 - Numerical Differentiation

Dr. M. HrairiDr. M. Hrairi MTH2212 - Computational Methods and StatisticsMTH2212 - Computational Methods and Statistics 2727

Example 1 - Solution

Second derivative computationThe data needed is:

Second derivative:

94336.1)( 25.1

2.0)( 1

636328.0)( 75.0

925.0)( 5.0

33

22

11

ii

ii

ii

ii

xfx

xfx

xfx

xfx

6.39)25.0(

)925.0(2)636328.0(5)2.0(4)94336.1()5.0(''

)(2)(5)(4)()(''

2

2123

f

h

xfxfxfxfxf iiiii

Page 28: Lecture08 - Numerical Differentiation

Dr. M. HrairiDr. M. Hrairi MTH2212 - Computational Methods and StatisticsMTH2212 - Computational Methods and Statistics 2828

Example 1 - Solution

b) Centered finite-divided difference The data needed is:

First derivative:2.0)( 1

636328.0)( 75.0

103516.1)( 25.0

2.1)( 0

22

11

11

22

ii

ii

ii

ii

xfx

xfx

xfx

xfx

εt = 0%

True value=-0.91259125.0)('

)25.0(12

)2.1()103516.1(8)636328.0(8)2.0()('

12

)()(8)(8)()(' 2112

i

i

iiiii

xf

xf

h

xfxfxfxfxf

Page 29: Lecture08 - Numerical Differentiation

Dr. M. HrairiDr. M. Hrairi MTH2212 - Computational Methods and StatisticsMTH2212 - Computational Methods and Statistics 2929

Example 1 - Solution

c) Backward finite-divided difference The data needed is:

First derivative :

925.0)( 5.0

103516.1)( 25.0

2.1)( 0

11

22

ii

ii

ii

xfx

xfx

xfx

εt = 3.77%

True value=-0.9125

878125.0)25.0(2

)2.1()103516.1(4)925.0(3)('

2

)()(4)(3)(' 21

i

iiii

xf

h

xfxfxfxf