review of numerical integrationdmackey/lectures/integrationii.pdf · 2016-03-04 · 1. use the...

28
Review of Numerical Integration The fundamental problem of numerical integration is the following: Given the function f continuous on [a, b], approximate I (f )= Z b a f (x ) dx Recall that exact integration can be performed using the Fundamental Theorem of Calculus: If F is an antiderivative of f , that is F 0 (x )= f (x ), then I (f )= Z b a f (x ) dx = F (b) - F (a) March 4, 2016 1 / 28

Upload: others

Post on 26-May-2020

2 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Review of Numerical Integrationdmackey/lectures/IntegrationII.pdf · 2016-03-04 · 1. Use the composite trapezoidal rule with n = 4 to approximate the integral Z 2 2 x3ex dx The

Review of Numerical Integration

The fundamental problem of numerical integration is the following:Given the function f continuous on [a, b], approximate

I (f ) =

∫ b

af (x) dx

Recall that exact integration can be performed using the FundamentalTheorem of Calculus:If F is an antiderivative of f , that is F ′(x) = f (x), then

I (f ) =

∫ b

af (x) dx = F (b)− F (a)

March 4, 2016 1 / 28

Page 2: Review of Numerical Integrationdmackey/lectures/IntegrationII.pdf · 2016-03-04 · 1. Use the composite trapezoidal rule with n = 4 to approximate the integral Z 2 2 x3ex dx The

Most numerical integration formulas, also known as quadrature formulas,are of the form

I (f ) ≈ In(f ) =n∑

i=0

wi f (xi )

where xi are the quadrature points or abscissas, and the wi are calledthe quadrature weights.

There are two types of quadrature formulas:

Newton-Cotes Quadrature: the quadrature points xi are fixed and then theweights are obtained by fitting a function to the f (xi ) data;

Gaussian Quadrature: given the number of data points, the weights andquadrature points are selected for maximum accuracy.

March 4, 2016 2 / 28

Page 3: Review of Numerical Integrationdmackey/lectures/IntegrationII.pdf · 2016-03-04 · 1. Use the composite trapezoidal rule with n = 4 to approximate the integral Z 2 2 x3ex dx The

Newton-Cotes Quadrature FormulasThe basic procedure is the following:

1 Fix the abscissas x0, x1, x2 . . . xn in [a, b];

2 Interpolate the function f at these points by the polynomial Pn(x);

3 Integrate the interpolating polynomial to get

I (f ) ≈ In(f ) ≡ I (Pn)

We use the Lagrange interpolating polynomial

Pn(x) =n∑

i=0

Ln,i (x)f (xi )

so the Newton-Cotes quadrature formula is

In(f ) =n∑

i=0

wi f (xi ) where wi =

∫ b

aLn,i (x) dx .

March 4, 2016 3 / 28

Page 4: Review of Numerical Integrationdmackey/lectures/IntegrationII.pdf · 2016-03-04 · 1. Use the composite trapezoidal rule with n = 4 to approximate the integral Z 2 2 x3ex dx The

Closed Newton-Cotes formulas

In this case, the abscissas xi include the endpoints of the interval, a and b.We let ∆x denote the length of the (equal) intervals between abscissasand then

∆x =b − a

nand xi = a + i∆x

If n = 1 then x0 = a and x1 = b and the Lagrange polynomials are

L1,0(x) =b − x

b − a, L1,1(x) =

x − a

b − a

The closed Newton-Cotes formula for n = 1 is

I (f ) ≈ I1(f ) =∆x

2[f (a) + f (b)] =

b − a

2[f (a) + f (b)]

This is known as the trapezoidal rule.

March 4, 2016 4 / 28

Page 5: Review of Numerical Integrationdmackey/lectures/IntegrationII.pdf · 2016-03-04 · 1. Use the composite trapezoidal rule with n = 4 to approximate the integral Z 2 2 x3ex dx The

If n = 2 then x0 = a, x1 = a + ∆x = a+b2 and x2 = b. The quadrature

formula for n = 2

I (f ) ≈ I2(f ) =b − a

6

[f (a) + 4f (

a + b

2) + f (b)

]is known as Simpson’s Rule.

Exercise: Prove the quadrature formulas for n = 3 and n = 4

I3(f ) =b − a

8[f (a) + 3f (a + ∆x) + 3f (a + 2∆x) + f (b)]

(The ”three-eights rule”)

I4(f ) =b − a

90[7f (a) + 32f (a + ∆x) + 12f (a + 2∆x) + 32f (a + 3∆x)

+7f (b)] (Boole’s rule)

March 4, 2016 5 / 28

Page 6: Review of Numerical Integrationdmackey/lectures/IntegrationII.pdf · 2016-03-04 · 1. Use the composite trapezoidal rule with n = 4 to approximate the integral Z 2 2 x3ex dx The

Error terms

The error term associated with the trapezoidal rule is

I (f )− I1(f ) = −(b − a)3

12f ′′(ξ)

where a < ξ < b.

The error term associated with Simpson’s rule is

I (f )− I2(f ) = −(b − a)5

2880f (4)(ξ)

where a < ξ < b.

March 4, 2016 6 / 28

Page 7: Review of Numerical Integrationdmackey/lectures/IntegrationII.pdf · 2016-03-04 · 1. Use the composite trapezoidal rule with n = 4 to approximate the integral Z 2 2 x3ex dx The

Open Newton-Cotes Formulas

Open formulas do not include the endpoints of the integration intervalamong abscissas. We have

∆x =b − a

n + 2and xi = a + (i + 1)∆x , i = 1, n

If n = 0, ∆x = (b − a)/2 and x0 = (a + b)/2. The open Newton-Cotesquadrature formula is

I (f ) ≈ I0(f ) = (b − a)f

(a + b

2

)(The midpoint rule)

The associated error term is

I (f )− I0(f ) =(b − a)3

24f ′′(ξ), a < ξ < b.

March 4, 2016 7 / 28

Page 8: Review of Numerical Integrationdmackey/lectures/IntegrationII.pdf · 2016-03-04 · 1. Use the composite trapezoidal rule with n = 4 to approximate the integral Z 2 2 x3ex dx The

Composite numerical integration

A composite Newton-Cotes quadrature rule consists of subdividing theintegration interval [a, b] into subintervals and then applying low-orderintegration formulas on each of the subintervals.

Example:

1 Use Simpson’s rule to approximate∫ 40 ex dx and calculate the error of

this approximation.

2 Approximate the integral in part (i) by approximating the integrals∫ 20 ex dx and

∫ 42 ex dx and then adding the answers. Calculate the

error of this approximation and compare with the first method.

March 4, 2016 8 / 28

Page 9: Review of Numerical Integrationdmackey/lectures/IntegrationII.pdf · 2016-03-04 · 1. Use the composite trapezoidal rule with n = 4 to approximate the integral Z 2 2 x3ex dx The

Composite trapezoidal rule

The trapezoidal rule can be written as

I (f ) = I1(f ) + error =b − a

2[f (a) + f (b)]− (b − a)3

12f ′′(ξ)

We split the integration interval [a, b] into n subintervals

a = x0 ≤ x1 ≤ x2 ≤ · · · xn−1 ≤ xn = b

where xi = a + ih for all 0 ≤ i ≤ n, and h = (b − a)/n.

March 4, 2016 9 / 28

Page 10: Review of Numerical Integrationdmackey/lectures/IntegrationII.pdf · 2016-03-04 · 1. Use the composite trapezoidal rule with n = 4 to approximate the integral Z 2 2 x3ex dx The

Apply trapezoidal rule on each interval [xi−1, xi ]:

I (f ) =n∑

i=1

∫ xi

xi−1

f (x) dx

=n∑

i=1

xi − xi−12

[f (xi−1 − f (xi )]−n∑

i=1

(xi − xi−1)3

12f ′′(ξi )

=h

2

[f (a) + 2

n−1∑i=1

f (xi ) + f (b)

]− h3

12

n∑i=1

f ′′(ξi )

where each ξi is a number between xi−1 and xi .

Using the Extreme Value Theorem and the Intermediate ValueTheorem (next slide), the error term can be rewritten as

Error = −nh3

12f ′′(ξ) = −(b − a)h2

12f ′′(ξ)

where a < ξ < b.

March 4, 2016 10 / 28

Page 11: Review of Numerical Integrationdmackey/lectures/IntegrationII.pdf · 2016-03-04 · 1. Use the composite trapezoidal rule with n = 4 to approximate the integral Z 2 2 x3ex dx The

The formula for the composite trapezoidal rule becomes

I (f ) =h

2

[f (a) + 2

n−1∑i=1

f (xi ) + f (b)

]− (b − a)h2

12f ′′(ξ)

Note:

The Extreme Value Theorem:If f is continuous on [a, b] then there exist numbers c1 and c2 in [a, b]such that

f (c1) ≤ f (x) ≤ f (c2)

for all x in [a, b].

The Intermediate Value Theorem:If f is continuous on [a, b] and k is any number between f (a) and f (b)then there exists a number c in (a, b) such that f (c) = k .

March 4, 2016 11 / 28

Page 12: Review of Numerical Integrationdmackey/lectures/IntegrationII.pdf · 2016-03-04 · 1. Use the composite trapezoidal rule with n = 4 to approximate the integral Z 2 2 x3ex dx The

Exercises:

1. Use the composite trapezoidal rule with n = 4 to approximate theintegral ∫ 2

−2x3ex dx

The exact value is 2e2 + 38e−2.

2. Determine the number of subintervals required to approximate∫ 2

0

1

x + 4dx

to within 10−5 using the composite trapezoidal rule.

March 4, 2016 12 / 28

Page 13: Review of Numerical Integrationdmackey/lectures/IntegrationII.pdf · 2016-03-04 · 1. Use the composite trapezoidal rule with n = 4 to approximate the integral Z 2 2 x3ex dx The

Composite Simpson’s Rule

Divide the interval of integration [a, b] into an even number ofsubintervals, n = 2m. Then

h =b − a

n=

b − a

2mand xi = a + ih, (0 ≤ i ≤ 2m)

Apply Simpson’s rule m times on each interval of the form [x2j−2, x2j ] for jbetween 1 and m.

I (f ) =m∑j=1

∫ x2j

x2j−2

f (x) dx

=h

3

f (x0) + 4m∑j=1

f (x2j−1) + 2m−1∑j=1

f (x2j) + f (x2m)

− (b − a)h4

180f (4)(ξ), with a < ξ < b.

March 4, 2016 13 / 28

Page 14: Review of Numerical Integrationdmackey/lectures/IntegrationII.pdf · 2016-03-04 · 1. Use the composite trapezoidal rule with n = 4 to approximate the integral Z 2 2 x3ex dx The

Examples

1. Use the composite Simpson’s rule with n = 4 to approximate theintegral ∫ 2

−2x3ex dx

The exact value is 2e2 + 38e−2.

2. Consider the integral

I =

∫ 1

0(x + 1) ln(x + 1) dx .

Determine the number of intervals needed in the composite Simpson’s rulesuch that, when approximating the value of this integral, the error is lessthan 10−4.

March 4, 2016 14 / 28

Page 15: Review of Numerical Integrationdmackey/lectures/IntegrationII.pdf · 2016-03-04 · 1. Use the composite trapezoidal rule with n = 4 to approximate the integral Z 2 2 x3ex dx The

Example

Using the integral ∫ 1

0

1

1 + x2dx =

π

4

determine the number of subintervals needed for the composite trapezoidalrule and the composite Simpson’s rule in order to approximate π to fourdecimal places.

We must haveeh = |I (f )− Th(f )| < 1.25× 10−5

and we can show that, if f (x) = 11+x2

then

maxx∈[0,1]

|f ′′(x)| = 2 and maxx∈[0,1]

|f (4)(x)| = 24

March 4, 2016 15 / 28

Page 16: Review of Numerical Integrationdmackey/lectures/IntegrationII.pdf · 2016-03-04 · 1. Use the composite trapezoidal rule with n = 4 to approximate the integral Z 2 2 x3ex dx The

For the trapezoidal rule we must select n so that

eh =(1− 0)3

12n2· 2 < 1.25× 10−5

so n ≥ 116.

For Simpson’s rule we must select n so that

eh =(1− 0)5

180n4· 24 < 1.25× 10−5

so n ≥ 12.

March 4, 2016 16 / 28

Page 17: Review of Numerical Integrationdmackey/lectures/IntegrationII.pdf · 2016-03-04 · 1. Use the composite trapezoidal rule with n = 4 to approximate the integral Z 2 2 x3ex dx The

Rates of convergence

Note that Tn(f ), the composite trapezoidal rule approximation to theintegral I (f ) with n subintervals of length h = (b− a)/n, forms a sequencewhich converges to I (f ) as n→∞, or h→ 0.

In general, if a function f (h) converges to a limit L as h→ 0, such that

|f (h)− L| ≤ C |g(h)|, for all sufficiently small h

where C is a constant and g(h) is a function which converges to 0 then wesay that f (h) converges to L with rate of convergence O(g(h)) (order g).

The error term shows that the rate of convergence for the compositetrapezoidal rule is O(h2).

March 4, 2016 17 / 28

Page 18: Review of Numerical Integrationdmackey/lectures/IntegrationII.pdf · 2016-03-04 · 1. Use the composite trapezoidal rule with n = 4 to approximate the integral Z 2 2 x3ex dx The

Numerical verification of rate of convergence

Example: Consider the integral

I (f ) =

∫ π

0sin(x) dx

Compute a sequence of approximations Th(f ) (composite trapezoidal rule)and Sh(f ) (composite Simpson’s rule) which shows clearly the convergenceto I (f ) and the rates of convergence in each case.

The rate of convergence for the trapezoidal rule (Simpson’s rule is similar)can be determined by calculating

eh = |I (f )− Th(f )|

and showing thate2heh→ 4 as h→ 0 (or

e2heh→ 16 for Simpson’s).

March 4, 2016 18 / 28

Page 19: Review of Numerical Integrationdmackey/lectures/IntegrationII.pdf · 2016-03-04 · 1. Use the composite trapezoidal rule with n = 4 to approximate the integral Z 2 2 x3ex dx The

If the exact value of the integral I (f ) is not known then the rates ofconvergence can be verified by showing that

Th(f )− Th/2(f )

Th/2(f )− Th/4(f )→ 4 as h→ 0

for the composite trapezoidal rule, and

Sh(f )− Sh/2(f )

Sh/2(f )− Sh/4(f )→ 16 as h→ 0

for the composite Simpson’s rule.

March 4, 2016 19 / 28

Page 20: Review of Numerical Integrationdmackey/lectures/IntegrationII.pdf · 2016-03-04 · 1. Use the composite trapezoidal rule with n = 4 to approximate the integral Z 2 2 x3ex dx The

Example:

Using the integral

I (f ) =

∫ 1

0

√1 + x3 dx

verify numerically that the composite trapezoidal rule has rate ofconvergence O(h2).

March 4, 2016 20 / 28

Page 21: Review of Numerical Integrationdmackey/lectures/IntegrationII.pdf · 2016-03-04 · 1. Use the composite trapezoidal rule with n = 4 to approximate the integral Z 2 2 x3ex dx The

Accuracy of integration

The degree of precision (or accuracy) of a quadrature rule In(f ) is definedas the largest integer m such that all polynomials of degree less than mare exactly integrated by the rule (i.e. such that the error is zero).

Note: In practice, we only need to check whether a rule integrates thepowers of x exactly. So, if a rule integrates 1, x and x2 exactly but fails tointegrate x3 exactly, the degree of precision is 2.

Examples

1 The trapezoidal rule integrates 1 and x exactly but fails to integratex2 exactly, hence the degree of precision is 1.

2 Verify that Simpson’s rule has degree of precision equal to 3.

March 4, 2016 21 / 28

Page 22: Review of Numerical Integrationdmackey/lectures/IntegrationII.pdf · 2016-03-04 · 1. Use the composite trapezoidal rule with n = 4 to approximate the integral Z 2 2 x3ex dx The

Gaussian quadrature

Recall that Newton-Cotes formulae are based on equally spacedquadrature points of the form xi = a + ih, where h = (b − a)/n. Bycontrast, Gaussian quadrature rules make an adaptive choice of nodes thatminimizes the approximation error and therefore has maximum possibledegree of precision for any rule using n points.

To develop such rule, we use the method of undetermined coefficents:We need to find the numbers x1, x2, . . . xn (the abscissas) andw1, w2, . . .wn (the weights) such that the approximation

I (f ) =

∫ b

af (x) dx ≈

n∑i=0

wi f (xi )

has degree of precision 2n− 1, in other words it integrates the polynomials1, x , x2, . . ., x2n−1 exactly.

March 4, 2016 22 / 28

Page 23: Review of Numerical Integrationdmackey/lectures/IntegrationII.pdf · 2016-03-04 · 1. Use the composite trapezoidal rule with n = 4 to approximate the integral Z 2 2 x3ex dx The

One-point Gaussian quadrature rule

The approximation formula∫ b

af (x) dx = w1f (x1)

has degree of precision equal to 1 if it integrates 1 and x exactly, that is

b − a = w1 and1

2(b2 − a2) = w1x1

We get w1 = b − a and x1 = (a + b)/2 so the quadrature rule is themidpoint rule: ∫ b

af (x) dx = (b − a) f

(a + b

2

)

March 4, 2016 23 / 28

Page 24: Review of Numerical Integrationdmackey/lectures/IntegrationII.pdf · 2016-03-04 · 1. Use the composite trapezoidal rule with n = 4 to approximate the integral Z 2 2 x3ex dx The

Two-point Gaussian quadrature rule

First convert the integral∫ ba f (x) dx into an integral over [−1, 1].

The change of variables

x =b − a

2t +

a + b

2

gives∫ b

af (x) dx =

b − a

2

∫ 1

−1f (

b − a

2t +

a + b

2) dt =

b − a

2

∫ 1

−1F (t) dt

The two-point quadrature rule∫ 1

−1F (t) dt ≈ w1F (t1) + w2F (t2)

has degree of precision 2 · 2− 1 = 3 if the following equations hold:

March 4, 2016 24 / 28

Page 25: Review of Numerical Integrationdmackey/lectures/IntegrationII.pdf · 2016-03-04 · 1. Use the composite trapezoidal rule with n = 4 to approximate the integral Z 2 2 x3ex dx The

w1 + w2 = 2

w1t1 + w2t2 = 0

w1t21 + w2t

22 =

2

3w1t

31 + w2t

32 = 0

The two-point quadrature rule becomes∫ 1

−1F (t) dt ≈ F

(−√

1

3

)+ F

(√1

3

)

March 4, 2016 25 / 28

Page 26: Review of Numerical Integrationdmackey/lectures/IntegrationII.pdf · 2016-03-04 · 1. Use the composite trapezoidal rule with n = 4 to approximate the integral Z 2 2 x3ex dx The

The error term can be shown to be equal to

1

135F (4)(ξ), where − 1 < ξ < 1.

Converting back to the original interval we get∫ b

af (x) dx =

b − a

2

[f

(a + b

2−√

1

3

b − a

2

)+ f

(a + b

2+

√1

3

b − a

2

)]

+(b − a)5

4320f (4)(ξ̂), where a < ξ̂ < b

March 4, 2016 26 / 28

Page 27: Review of Numerical Integrationdmackey/lectures/IntegrationII.pdf · 2016-03-04 · 1. Use the composite trapezoidal rule with n = 4 to approximate the integral Z 2 2 x3ex dx The

Examples

Example 1: Consider the integral

I =

∫ 1

−1ex sin(πx) dx =

π(e2 − 1)

e(π2 + 1)≈ 0.679326

Compare the errors obtained when approximating this integral withSimpson’s rule and the two-point Gaussian rule above.

Example 2: Using the two-point Gaussian quadrature formula,approximate the integrals

(i)

∫ 1

−1e−x dx ; (ii)

∫ π

0sin(x) dx

and find the absolute error in each case.

March 4, 2016 27 / 28

Page 28: Review of Numerical Integrationdmackey/lectures/IntegrationII.pdf · 2016-03-04 · 1. Use the composite trapezoidal rule with n = 4 to approximate the integral Z 2 2 x3ex dx The

Three-point Gaussian rule

Derive the three-point Gaussian rule:∫ 1

−1f (x) dx ≈ 5

9f

(−√

3

5

)+

8

9f (0) +

5

9f

(√3

5

)

and convert this formula to the general integration interval [a, b].

Example: Using the three-point Gaussian quadrature formula,approximate the integrals

(i)

∫ 1

−1e−x dx ; (ii)

∫ π

0sin(x) dx

and find the absolute error in each case.

March 4, 2016 28 / 28