W10 Numerical Calculus

download W10 Numerical Calculus

of 46

Transcript of W10 Numerical Calculus

  • 8/2/2019 W10 Numerical Calculus

    1/46

    Centre for Computer Technology

    ICT114Mathematics for

    Computing

    Week 10

    Numerical Differentiation andIntegration

  • 8/2/2019 W10 Numerical Calculus

    2/46

    March 20, 2012 Copyright Box Hill Institute

    Objectives

    Review week 9

    Numerical Differentiation

    Newtons Forward Difference formula Newtons Backward Difference formula

    Numerical Integration

    Trapezoid rule

    Simpsons one third rule

  • 8/2/2019 W10 Numerical Calculus

    3/46

    March 20, 2012 Copyright Box Hill Institute

    Newton's Method

    Using an initial guess at the root and the

    slope of f(x), Newton's method usesextrapolation to estimate where f(x)crosses the x axis. This method converges

    very quickly, but it can diverge if f(x) = 0 isencountered during iterations.

    (f(x) is the differential of f(x))

  • 8/2/2019 W10 Numerical Calculus

    4/46

    March 20, 2012 Copyright Box Hill Institute

    Algorithm

    initialize: x1 = . . .

    for k= 2, 3, . . .xk= xk-1- f(xk-1)/f(xk-1)

    if converged, stop

    end

  • 8/2/2019 W10 Numerical Calculus

    5/46

    March 20, 2012 Copyright Box Hill Institute

    Secant Method

    The secant method approximates f(x)

    from the value of f(x) at two previous

    guesses at the root. It is as fast as theNewton's method but can also fail atf(x)=0.

  • 8/2/2019 W10 Numerical Calculus

    6/46

    March 20, 2012 Copyright Box Hill Institute

    Algorithm

    initialize: x1 = . . ., x2 = . . .

    for k= 2, 3 .. .

    xk+1 = xk- f(xk)(xk- xk-1)/(f(xk) - f(xk-1))If f(xk+1)

  • 8/2/2019 W10 Numerical Calculus

    7/46

    Centre for Computer Technology

    Newtons ForwardDifference Formula

  • 8/2/2019 W10 Numerical Calculus

    8/46

    March 20, 2012 Copyright Box Hill Institute

    Numerical Differentiation

    We discuss Newtons forward

    difference formula in detail. This is suitable for differentiation for

    the the values towards the beginning

    of the table

  • 8/2/2019 W10 Numerical Calculus

    9/46

    March 20, 2012 Copyright Box Hill Institute

    Forward Difference formula (1)

    For functions tabulated with constant interval h,

    E f(x) = f (x+h)

    E2 f(x)= E (E f(x)) = E f(x+h)=f(x+2h) Like this, Epf(x) = f (x + ph) Again, f(x) = f(x+h) - f(x) Hence f(x+h) = f (x) + f(x)

    = ( 1 + ) f(x) That is, E f(x) = ( 1 + ) f(x) or simply, E ( 1 + )

  • 8/2/2019 W10 Numerical Calculus

    10/46

    March 20, 2012 Copyright Box Hill Institute

    Forward Difference formula (2)

    Hence,

    f(x0 +p.h) = Epf (x0)

    = (1 +)p f(x0) = ( 1 + p +pC2

    2 +pC33+ .... ) f(x0)

    = ( 1 + p + p(p-1)/2! 2

    + p(p-1)(p-2)/3! 3+ ) f(x0)

  • 8/2/2019 W10 Numerical Calculus

    11/46

    March 20, 2012 Copyright Box Hill Institute

    Forward difference formula (3)

    Putting, x = x0 + ph,

    df df dp 1 df

    ---- = ---- . ---- = ---- . -----

    dx dp dx h dp

  • 8/2/2019 W10 Numerical Calculus

    12/46

    March 20, 2012 Copyright Box Hill Institute

    Forward difference formula (4)

    So, f/(x)= (1/h) [ + (2p-1)/2. 2

    + (3p2 - 6p +2)/6 3

    + (4p3

    -18p2

    +22p-6)/24 4

    +] Putting p = 0,

    f/(x)= (1/h) [ 1/22 ++1/3

    31/44+.]

    This is Newtons forward differenceformula for differentiation suitable forvalues given in the table

  • 8/2/2019 W10 Numerical Calculus

    13/46

    March 20, 2012 Copyright Box Hill Institute

    An example : f(x) = ex (1)

    x f(x)

    1.0 2.718282

    1.2 3.320117

    1.4 4.055200

    1.6 4.953032

    1.8 6.049646

  • 8/2/2019 W10 Numerical Calculus

    14/46

    March 20, 2012 Copyright Box Hill Institute

    x f(x)

    1.0 2.718282

    0.601835

    1.2 3.320117

    0.735083

    1.4 4.055200

    0.897832

    1.6 4.953032

    1.096615

    1.8 6.049646

    An example : f(x) = ex (2)

  • 8/2/2019 W10 Numerical Calculus

    15/46

    March 20, 2012Copyright Box Hill Institute

    x f(x) 2

    1.0 2.718282

    0.601835

    1.2 3.320117 0.133248

    0.735083

    1.4 4.055200 0.162749

    0.897832

    1.6 4.953032 0.198783

    1.096615

    1.8 6.049646

    An example : f(x) = ex (3)

  • 8/2/2019 W10 Numerical Calculus

    16/46

    March 20, 2012Copyright Box Hill Institute

    x f(x) 2 3

    1.0 2.718282

    0.601835

    1.2 3.320117 0.133248

    0.735083 0.029501

    1.4 4.055200 0.162749

    0.897832 0.036034

    1.6 4.953032 0.198783

    1.096615

    1.8 6.049646

    An example : f(x) = ex (4)

  • 8/2/2019 W10 Numerical Calculus

    17/46

    March 20, 2012Copyright Box Hill Institute

    x f(x) 2 3 4

    1.0 2.718282

    0.601835

    1.2 3.320117 0.133248

    0.735083 0.029501

    1.4 4.055200 0.162749 0.006532

    0.897832 0.036034

    1.6 4.953032 0.198783

    1.096615

    1.8 6.049646

    An example : f(x) = ex (5)

  • 8/2/2019 W10 Numerical Calculus

    18/46

    March 20, 2012Copyright Box Hill Institute

    Example (continued)

    To find the differential coefficient for x=1.0

    From the table, = 0.601835, 2 = 0.133248

    3

    = 0.029501, and 4

    = 0.006532 Here h = 0.2

    The approximate value of the diff coeff =

    (1/0.2) [ 1/2

    2 +1/3

    31/4

    4 ]

    = 2.717060

    The true value is 2.718282

  • 8/2/2019 W10 Numerical Calculus

    19/46

    March 20, 2012Copyright Box Hill Institute

    Formula for value not in table

    Suppose we want to find derivative at apoint not given in the table,say at x=1.1

    So, ph = 0.1. Then, as h=0.2, p=0.5

    Putting p=0.5,we get

    f/(x)= (1/h) [ + 0 * 20.25/6 3+ 1/24 4+]

  • 8/2/2019 W10 Numerical Calculus

    20/46

    March 20, 2012Copyright Box Hill Institute

    Forward difference formula

    So,

    f/(1.1)= (1/0.2) [ + - 0.25/6 3

    + 1/12 4+]= 5*[0.601835 - 0.029501* 0.25/6 +

    0.006532/12]

    = 3.005750The true value is = 3.004166

  • 8/2/2019 W10 Numerical Calculus

    21/46

    Centre for Computer Technology

    Newtons BackwardDifference Formula

  • 8/2/2019 W10 Numerical Calculus

    22/46

    March 20, 2012Copyright Box Hill Institute

    Backward difference formula

    Backward difference formula is given by

    f/(x)= (1/h) [ +1/22+1/3

    3

    + 1/44+..] Where, f(x) = f(x) f(x-h),

    2 f(x) = ( f(x)), and so on

    This is suitable for finding derivativetowards the end of the table

  • 8/2/2019 W10 Numerical Calculus

    23/46

    March 20, 2012Copyright Box Hill Institute

    An example : f(x) = ex

    x f(x) 2 3 4

    1.0 2.718282

    0.601835

    1.2 3.320117 0.133248

    0.735083 0.029501

    1.4 4.055200 0.162749 0.006532

    0.897832 0.036034

    1.6 4.953032 0.198783

    1.096615

    1.8 6.049646

  • 8/2/2019 W10 Numerical Calculus

    24/46

    March 20, 2012Copyright Box Hill Institute

    Backward difference formula

    To find the differential coefficient for x=1.8

    From the table, = 1.096615, 2 = 0.198783

    3

    = 0.036033, and

    4

    = 0.006532 Here h = 0.2

    The approximate value of the diff coeff =

    (1/0.2) [ +1/2

    2 +1/3

    3 +1/4

    4 ]

    = 6.048252

    The true value is 6.049647

  • 8/2/2019 W10 Numerical Calculus

    25/46

    March 20, 2012Copyright Box Hill Institute

    Some remarks

    There is a central difference formula forfinding differential coefficient when the

    values are around middle of the table. Wedid not discuss that.

    It may be remembered that at timesnumerical differentiation might be veryinaccurate when there are largefluctuations.

  • 8/2/2019 W10 Numerical Calculus

    26/46

    Centre for Computer Technology

    Trapezoid Rule

  • 8/2/2019 W10 Numerical Calculus

    27/46

    March 20, 2012

    Copyright Box Hill Institute

    Numerical Integration

    Value of a definite integral within its limitsis the area under the curve in the limits

    In numerical integration, the function isapproximated by a polynomial, and thearea under the polynomial is taken as thevalue of the integral

    We study two simple rules (1) Trapezoidalrule and (2) Simpson's one-third rule

  • 8/2/2019 W10 Numerical Calculus

    28/46

    March 20, 2012 Copyright Box Hill Institute

    Trapezoid rule (1)

    Area below the curve is approximated by aTrapezium

    f(x)

    a b

    Value of integral of f (x) between a and b is the area

    under the curve between a and b

  • 8/2/2019 W10 Numerical Calculus

    29/46

    March 20, 2012 Copyright Box Hill Institute

    Trapezoid rule (2)

    Area below the curve is approximated by aTrapezium

    f(x)

    a b

    Value of the integral is approximated as

    = area of the trapezium

    = [f(a) +f(b)] . (ba)

  • 8/2/2019 W10 Numerical Calculus

    30/46

    March 20, 2012 Copyright Box Hill Institute

    Trapezoid rule (3)

    Now two intervals

    Trapezoid Rule for two intervals

    f(x)

    a0=a a1 a2 = b

    Value of the integral is better approximated by

    = area of trapezium 1 + area of trapezium 2

    = [f(a0) +f(a1)].(a1a0)+ [f(a1) +f(a2)](a2a1)

  • 8/2/2019 W10 Numerical Calculus

    31/46

    March 20, 2012 Copyright Box Hill Institute

    Trapezoid rule (4)

    If a1=a0 + h , and, a2 =a1+ h= a0 +2h,

    The approximate area under the curve(when there are two intervals)

    = h/2 [ f(a0) + f(a1) ] +h/2 [ f(a1) + f(a2) ]

    = h/2 [ f(a0) + 2 f(a1) + f(a2) ]

  • 8/2/2019 W10 Numerical Calculus

    32/46

    March 20, 2012 Copyright Box Hill Institute

    Trapezoid rule (5)

    When there are n equidistant intervals, theapproximate value of the integral is equal

    to =(h/2) [ f(a0) + 2 f(a1) + 2f(a2) +.

    + 2 f(an-1) + f(an)]

  • 8/2/2019 W10 Numerical Calculus

    33/46

    March 20, 2012 Copyright Box Hill Institute

    An example : f(x) = ex

    x f(x)

    1.0 2.718282

    1.2 3.320117

    1.4 4.055200

    1.6 4.953032

    1.8 6.049646

  • 8/2/2019 W10 Numerical Calculus

    34/46

    March 20, 2012 Copyright Box Hill Institute

    Example (continued)

    To evaluate the value of integral

    f(x) = exp(x) between x= 1.0 and 1.8

    Suppose we take interval h = 0.4 Then a0 = 1.0, a1 =1.4, a2= 1.8

    The value is given by

    (0.4/2)[2.718282 + (2) 4.055200 + 6.049646]

    = 3.375666

  • 8/2/2019 W10 Numerical Calculus

    35/46

    March 20, 2012 Copyright Box Hill Institute

    Example (low interval length)

    Suppose we take interval h = 0.2

    Then a0 = 1.0, a1 =1.2, a2= 1.4 , a3 =1.6, a4= 1.8

    The value is given by

    (0.2/2) [2.718282 + (2) 3.320117

    + (2) 4.055200 + (2) 4.953032

    + 6.049646 ]

    = 3.342463

    Actual value is 3.331366

  • 8/2/2019 W10 Numerical Calculus

    36/46

    Centre for Computer Technology

    Simpsons One Third Rule

  • 8/2/2019 W10 Numerical Calculus

    37/46

    March 20, 2012 Copyright Box Hill Institute

    Simpsons one third rule (1)

    In trapeziod rule,the curve is approximatedby a line

    In Simpsons rule , the curve isapproximated by a second degreepolynomial

    It requires even number of intervals

  • 8/2/2019 W10 Numerical Calculus

    38/46

    March 20, 2012 Copyright Box Hill Institute

    Simpsons one third rule (2)

    Consider three equidistant points, a ,(a+b)/2 and b, and a second degreepolynomial f(x)= c

    0+c

    1x +c

    2x2

    So, we have, f(a) = c0 +c1 a +c2 a2,

    f((a+b)/2) = c0 +c1 ((a+b)/2) +c2 ((a+b)/2)2

    And, f(b) = c0 +c1 b +c2 b2

  • 8/2/2019 W10 Numerical Calculus

    39/46

    March 20, 2012 Copyright Box Hill Institute

    Simpsons one third rule (3)

    On integration of the second degreepolynomial , the value of the integralequals

    c0 (b a) +c1 (b2 a2)/2 +c2 (b

    3 - a3)/3

    This can be shown equal to

    ((b a )/ 6)[f(a) + 4 f((a+b)/2) +f(b)]= ( h /3 ) [f(a) + 4 f((a+b)/2) +f(b)]

  • 8/2/2019 W10 Numerical Calculus

    40/46

    March 20, 2012 Copyright Box Hill Institute

    Simpsons one third rule (4)

    If we denote the points as a0, a1, a2 and his the interval length, the value of theintegral is (h/3)[ f(a

    0) + 4f (a

    1) + f(a

    2)]

    In general case, the value equals

    (h/3)[ (f(a0) + 4f (a1) + f(a2))

    + (f(a2) + 4f (a3) + f(a4))+ (f(a4) + 4f (a5) + f(a6)) + .]

  • 8/2/2019 W10 Numerical Calculus

    41/46

    March 20, 2012 Copyright Box Hill Institute

    Simpsons one third rule (5)

    This may be compactly written as

    (h/3)[ ( f(a0) + f (an) )

    + 4 (f(a1) + f (a3) + f(a5)+ f(an-1) )+ 2 (f(a2) + f (a4) + f(a6) + .. f(an-2) ) ]

    Remember that the number of intervalshave to be even

  • 8/2/2019 W10 Numerical Calculus

    42/46

    March 20, 2012 Copyright Box Hill Institute

    An example : f(x) = ex

    To evaluate the value of integral

    f(x) = exp (x) between x= 1.0 and 1.8

    Suppose we take interval h = 0.4 Then a0 = 1.0, a1 =1.4, a2= 1.8

    The value is given by

    (0.4/3)[2.718282 + (4) 4.055200 + 6.049646]

    = 3.331831

  • 8/2/2019 W10 Numerical Calculus

    43/46

    March 20, 2012 Copyright Box Hill Institute

    Example (less interval length)

    Suppose we take interval h = 0.2

    Then a0 = 1.0, a1 =1.2, a2= 1.4 , a3 =1.6, a4= 1.8

    The value is given by

    (0.2/3) [ (2.718282 + 6.049646)

    + 4 ( 3.320117 + 4.953032)

    + 2 ( 4.055200) ]

    = 3.331395

    As noted earlier, the actual value is 3.331366

  • 8/2/2019 W10 Numerical Calculus

    44/46

    March 20, 2012 Copyright Box Hill Institute

    Summary

    Newtons Forward Difference Formula

    f/

    (x)= (1/h) [ 1

    /22 +

    +1

    /33

    1/44+.]

    Newtons Backward Difference Formulaf/(x)= (1/h) [ +1/22+1/3

    3

    + 1/44+..]

  • 8/2/2019 W10 Numerical Calculus

    45/46

    March 20, 2012 Copyright Box Hill Institute

    Summary

    Trapezoid Rule

    = (h/2) [ f(a0) + 2 f(a1) + 2f(a2) +.

    + 2 f(an-1) + f(an)]

    Simpsons One Third Rule (the number ofintervals have to be even)

    = (h/3)[ ( f(a0) + f (an) )+ 4 (f(a1) + f (a3) + f(a5)+ f(an-1) )

    + 2 (f(a2) + f (a4) + f(a6) + .. f(an-2) ) ]

  • 8/2/2019 W10 Numerical Calculus

    46/46

    March 20, 2012 Copyright Box Hill Institute

    References

    H L Verma and C W Gross : Introduction toQuantitative Methods,John Wiley

    JB Scarborough : Numerical MathematicalAnalysis, Jon Hopkins Hall, New Jersey

    Gerald W. Recktenwald, Numerical Methodswith MATLAB, Implementation and Application,Prentice Hall

    Murray Spiegel, John Schiller, Alu Srinivasan,

    Probability and Statistics, Schaums easyOutlines

    http://mathworld.wolfram.com