21
Numerical Analysis (MATH 292) Lecture 5- Numerical Integration and Differentiation 1- Numerical Integration Newton - Cotes formula b b n a a I f x dx f x dx Where f n (x) is a polynomial of the form 1 2 0 1 2 ... n n n f x a ax ax ax Where n is order of the polynomial The Trapezoidal rule: . 2 f b f a I b a Error in Trapezoidal rule True error: E t = I true - I approx

Numerical Analysis- MATH 292 -Lec 5- Numerical Differentiations & Intergations

Embed Size (px)

DESCRIPTION

numerical

Citation preview

  • Numerical Analysis (MATH 292)

    Lecture 5- Numerical Integration and Differentiation

    1- Numerical Integration

    Newton - Cotes formula

    b b

    na a

    I f x dx f x dx Where fn(x) is a polynomial of the form

    1 20 1 2 ...n

    n nf x a a x a x a x Where n is order of the polynomial

    The Trapezoidal rule:

    .2

    f b f aI b a

    Error in Trapezoidal rule

    True error: Et = Itrue - Iapprox

  • True percent relative error: 100true approx

    t

    true

    I I

    I

    Simple application of the trapezoidal rule

    2 3 4 50.2 25 200 675 900 400f x x x x x x From a = 0 to b = 0.8

    The exact value of the integral is 1.640533

    Solution:

    0 0.2

    0.8 0.2323

    f

    f

    0.17282

    f b f aI b a

    1.640553 0.1728 1.46775tE

  • 1.46775

    100 89.5%1.640533

    t

    In actual situations, we would have no knowledge of the true value. Therefore, an

    approximate error estimate is required

    3

    2

    1''

    12aE f b a

    n

    where

    ''''

    b

    af x dx

    fb a

    In the example above:

    2 3'' 400 4050 108000 8000f x x x x

    0.82 3

    0'' 400 4050 108000 8000

    ''0.8 0

    60

    b

    af x dx x x x dx

    fb a

  • 3 3

    2

    1 1'' 60 0.8 2.56

    12 12aE f b a

    n

    Multiple application of the trapezoidal rule

    1

    0

    1

    22

    n

    i n

    i

    b aI f x f x f x

    n

    Example:

    Use the trapezoidal rule to obtain an approximate value of the integral

    2

    315

    1

    d xwhere n

    x

    Solution:

    x 1 1.2 1.4 1.6 1.8 2

    3

    1

    1x 0.707107 0.605449 0.516811 0.442981 0.382583 0.33333

  • 2 1

    [0.707107 2(0.605449 0.516811 0.44298110

    0.382583) 0.33333] 0.493609

    b

    af x dx

    Simpsons 1/3 Rule

    1 2

    0

    1,3,5 2,4,6

    4 23

    n n

    i i n

    i i

    b aI f x f x f x f x

    n

    Example:

    Use the Simpsons 1/3 rule to obtain an approximate value for the integral from 0

    to 0.8 (n=2). 2 3 4 50.2 25 200 675 900 400f x x x x x x

    0

    1

    2

    0.2

    0.4 2.456

    0.8 0.232

    f x f a

    f x f

    f x f

  • 0.8

    0.2 4 2.456 0.232 1.3674672 3

    I

    Recall that 1.640533trueI

    1.640533 1.367467 0.2730667tE

    16.6%t Error in Simpsons rule:

    5

    4

    4180a

    b aE f

    n

    where 4f is the average fourth derivative

    0.8

    2

    4 024 900 120 400

    24000.8 0

    x x dxf

    5

    0.82400 0.273

    2880a

  • Example:

    Use Simpsons 1/3 rule with n=4 to estimate the integral

    2 3 4 50.2 25 200 675 900 400f x x x x x x

    Solution:

    0 4 23

    n

    b aI f x odd even f x

    n

    x 0 0.2 0.4 0.6 0.8

    y = f(x) 0.2 1.288 2.456 3.464 0.232

    n = 4 0.8

    0.24

    h

    0.2

    0.2 4 1.288 3.464 2 2.456 0.232 1.6234673

    I

    5

    4

    0.82400 0.017067

    180 4aE

    1.04%t

  • Simpsons 3/8 Rule: In the similar manner to the derivation of the trapezoid and Simpsons 1/3 rule, a third order Lagrange polynomial can be fit to four points and integrate

    3b b

    a aI f x dx f x dx

    To yield

    0 1 2 33

    3 38

    I h f x f x f x f x

    Where b a

    hn

    Use Simpsons 3/8 to integrate the same function for n = 3

    2 3 4 50.2 25 200 675 900 400f x x x x x x

    0.8 0

    0.26673

    b ah

    n

    0 0.2 0.5333 3.487177

    0.2667 1.432724 0.8 0.232

    f f

    f f

  • 0.2 3 1.432724 3.487177 0.2320.8 1.519170

    8I

    5

    2400 1.640533 1.51917 0.1213636480

    a

    b aE

    7.4%t

    For n = 5, use the same function to integrate the first two segments using Simpsons 1/3 and the last 3 segments using Simpsons 3/8

    0.80.16

    5h

    0 0.2f 0.16 1.296919f 0.32 1.743393f 0.48 3.186015f 0.64 3.181929f 0.8 0.323f

    0.16 0.32 0.48 0.64 0.80

    Simpsons 1/3 rule

    Simpsons 3/8 rule

  • The integration for the first two-segments is obtained using Simpsons 1/3 rule.

    0.16

    0.2 4 1.296919 1.743393 0.38032373

    I

    For the last 3 segments, the Simpsons 3/8 rule can be used to obtain

    3

    0.16 1.743393 3 3.186015 3.181929 0.323 1.2647548

    I

    The total integral

    0.3803237 1.264754 1.645077I

    0.00454383tE 0.28 %t

    Errors in numerical integration:

    t t appE I I

    100true app

    t

    true

    I I

    I

  • Approximate Error Estimate:

    1) Trapezoidal rule

    3

    2''

    12a

    b aE f

    n

    2) Simpsons 1/3 rule

    5

    4

    4180a

    b aE f

    n

    3) Simpsons 3/8 rule

    5

    4

    480a

    b aE f

    n

  • Numerical Differentiation

    Mathematically, the derivative represents the rate of change of a dependent variable with

    respect to an independent variable.

    The mathematical definition of the derivative begins with a difference approximation

    ( ) ( )i if x x f xy

    x x

    If x is allowed to approach zero, the difference becomes a derivative

    0

    ( ) ( )lim i ix

    f x x f xdy

    dx x

    The derivative dy

    dx is the slope of the tangent to the curve ( )y f x at

    ix .

  • Taylor Series:

    Taylor series is of great value in the study of numerical methods.

    By using the Taylor method we can predict a function value at one point in terms of the

    function value and its derivatives at another point.

    2

    1 1 1

    ( )

    1

    ''' ...

    2!

    !

    i

    i i i i i i i

    nni

    i i n

    f xf x f x f x x x x x

    f xx x R

    n

    where nR is the reminder term.

    It is usually convenient to simplify the Taylor series by defining a step size 1i ih x x

    Let 1i ix x h , then

    ( )2

    1

    '''

    2! !

    n

    ni i

    i i i n

    f x f xf x f x f x h h h R

    n

  • Finite divided difference approximation of derivative

    First derivative

    Taylor series can be solved for

    1

    1

    ( )

    2

    1

    ' ( ) (1)

    where ( )

    ''and

    2! !

    i i

    i

    n

    ni i

    n

    f x f xf x O h

    h

    RO h

    h

    f x f xR h h R

    n

    1( ) ( )i if x f x is the first forward difference and h is called the step size ( length of

    interval)

  • There are three ways to approximate the first derivative form Taylor series

    1- Forward difference: 1' i ii

    f x f xf x

    h

    2- Backward difference: 1' i ii

    f x f xf x

    h

    3- Centered difference: 1 1'

    2

    i ii

    f x f xf x

    h

    Example:

    Use forward, backward and centered difference approximation to estimate the first

    derivative of

    4 3 20.1 0.15 0.5 0.25 1.2f x x x x x

    at 0.5x using a step size 0.5h . Repeat the computation using 0.25h , compute the

    true percent relative error

  • Solution:

    Exact first derivative 3 2' 0.4 0.45 0.25f x x x x ' 0.5 0.9125f

    For h = 0.5

    1 1

    1 1

    0 1.2

    0.5 0.925

    1.0 0.2

    i i

    i i

    i i

    x f x

    x f x

    x f x

    Forward divided difference:

    0.2 0.925

    ' 1.45 58.9%0.5

    i tf x

    Backward divided difference:

    0.925 1.2

    ' 0.55 39.7%0.5

    i tf x

    Centered divided difference:

    0.2 1.2

    ' 1.0 9.6%1.0

    i tf x

  • For h = 0.25

    1 1

    1 1

    0.25 1.10351

    0.5 0.925

    0.75 0.6363281

    i i

    i i

    i i

    x f x

    x f x

    x f x

    Forward divided difference:

    ' 0.25 1.155 26.5%tf

    Backward divided difference:

    ' 0.5 0.714 21.7%tf

    Centered divided difference:

    ' 0.75 0.934 2.4%tf

    For both step sizes, the centered difference approximation is more accurate than the other.

  • Finite difference of higher derivative:

    Taylor Series expansion can be used to derive numerical estimates of higher derivatives.

    To do this, we write a forward Taylor expansion of 2( )if x in terms of ( )if x

    2

    2

    ''' 2 2 ...

    2!

    i

    i i i

    f xf x f x f x h h (2)

    The Taylor expansion of 1( )if x is

    2

    1

    ''' ...

    2!

    i

    i i i

    f xf x f x f x h h (3)

    Multiply equation (3) by 2 and subtract from equation (2)

    22 12 '' ...i i i if x f x f x f x h

    2 122

    '' i i iif x f x f x

    f x O hh

    (4)

    Equation (4) is called the second forward finite divided difference.

  • High-accuracy differentiation Formulas

    High-accuracy forward divided-difference

    We have

    1 '''

    2

    i i i

    i

    f x f x f xf x h

    h

    (5)

    Substitution of equation (4) into equation (5), gives

    1 2 1

    2

    2 1

    2' .

    2

    4 3

    2

    i i i i i

    i

    i i i

    f x f x f x f x f x hf x

    h h

    f x f x f x

    h

    High-accuracy backward divided-difference

    1 23 4( )

    2

    i i i

    i

    f x f x f xf x

    h

  • High-accuracy centered divided-difference

    2 1 1 28 8( )

    12

    i i i i

    i

    f x f x f x f xf x

    h

    Example:

    Find the first derivative of 4 3 20.1 0.15 0.5 0.25 1.2f x x x x x at 0.5x , use high accuracy differentiation formulas with 0.25h

    Solution:

    2

    1

    1

    2

    0 0 1.2

    0.25 0.25 1.103515

    0.5 0.5 0.925

    0.75 0.75 0.636328

    1.00 1.0 0.2

    i

    i

    i

    i

    i

    x f

    x f

    x f

    x f

    x f

  • Forward difference:

    0.2 4 0.636328 3 0.925' 0.5 0.859375

    2 0.25f

    5.82 %t

    Backward difference:

    3 0.925 4 1.103515 1.2' 0.5 0.878125

    2 0.25f

    3.77 %t

    Centered difference:

    0.2 8 0.636328 8 1.103515 1.2

    ' 0.5 0.912512

    fh

    0 %t