Numerical Methods II - Dublin Institute of Technologydmackey/lectures/NumNotes.pdf · Accuracy of...

31
Numerical Methods II Dr Dana Mackey School of Mathematical Sciences Room A305 A Email: [email protected] Dana Mackey (DIT) Maths 1 / 31

Transcript of Numerical Methods II - Dublin Institute of Technologydmackey/lectures/NumNotes.pdf · Accuracy of...

Page 1: Numerical Methods II - Dublin Institute of Technologydmackey/lectures/NumNotes.pdf · Accuracy of quadrature formulas Thedegree of precision(or accuracy) of a quadrature rule I n(f)

Numerical Methods II

Dr Dana Mackey

School of Mathematical SciencesRoom A305 A

Email: [email protected]

Dana Mackey (DIT) Maths 1 / 31

Page 2: Numerical Methods II - Dublin Institute of Technologydmackey/lectures/NumNotes.pdf · Accuracy of quadrature formulas Thedegree of precision(or accuracy) of a quadrature rule I n(f)

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)

Dana Mackey (DIT) Maths 2 / 31

Page 3: Numerical Methods II - Dublin Institute of Technologydmackey/lectures/NumNotes.pdf · Accuracy of quadrature formulas Thedegree of precision(or accuracy) of a quadrature rule I n(f)

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.

Dana Mackey (DIT) Maths 3 / 31

Page 4: Numerical Methods II - Dublin Institute of Technologydmackey/lectures/NumNotes.pdf · Accuracy of quadrature formulas Thedegree of precision(or accuracy) of a quadrature rule I n(f)

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 .

Dana Mackey (DIT) Maths 4 / 31

Page 5: Numerical Methods II - Dublin Institute of Technologydmackey/lectures/NumNotes.pdf · Accuracy of quadrature formulas Thedegree of precision(or accuracy) of a quadrature rule I n(f)

Lagrange interpolating polynomials

Let x0, x1, x2, . . .xn be n+ 1 points and fi = f (xi ) the function values atthese points. An interpolating polynomial Pn is a polynomial of degree atmost n such that Pn(xi ) = fi .

The Lagrange Form of the Interpolating polynomial is

Pn(x) =n

∑i=0

Ln,i (x)fi where Ln,i (x) =n

∏k=0k 6=i

x−xkxi −xk

Examples (for n = 1 and n = 2):

P1(x) =x−x1x0−x1

f0 +x−x0x1−x0

f1

P2(x) =(x−x1)(x−x2)

(x0−x1)(x0−x2)f0 +

(x−x0)(x−x2)

(x1−x0)(x1−x2)f1 +

(x−x0)(x−x1)

(x2−x0)(x2−x1)f2

Dana Mackey (DIT) Maths 5 / 31

Page 6: Numerical Methods II - Dublin Institute of Technologydmackey/lectures/NumNotes.pdf · Accuracy of quadrature formulas Thedegree of precision(or accuracy) of a quadrature rule I n(f)

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.

Dana Mackey (DIT) Maths 6 / 31

Page 7: Numerical Methods II - Dublin Institute of Technologydmackey/lectures/NumNotes.pdf · Accuracy of quadrature formulas Thedegree of precision(or accuracy) of a quadrature rule I n(f)

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)

Dana Mackey (DIT) Maths 7 / 31

Page 8: Numerical Methods II - Dublin Institute of Technologydmackey/lectures/NumNotes.pdf · Accuracy of quadrature formulas Thedegree of precision(or accuracy) of a quadrature rule I n(f)

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)

Dana Mackey (DIT) Maths 8 / 31

Page 9: Numerical Methods II - Dublin Institute of Technologydmackey/lectures/NumNotes.pdf · Accuracy of quadrature formulas Thedegree of precision(or accuracy) of a quadrature rule I n(f)

If n = 1, ∆x = (b−a)/3 and the points are x0 = a+ ∆x andx1 = a+ 2∆x . The open Newton-Cotes quadrature formula is

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

2[f (a+ ∆x) + f (a+ 2∆x)] .

Exercise: Derive the open Newton-Cotes formulas for n = 2 and n = 3.

Exercise: Approximate the value of the integral

I =∫ 2

1

1

xdx

using some of the previous quadrature formulas. Compare the results withthe exact value of the integral, I = ln(2) = 0.6931...

Dana Mackey (DIT) Maths 9 / 31

Page 10: Numerical Methods II - Dublin Institute of Technologydmackey/lectures/NumNotes.pdf · Accuracy of quadrature formulas Thedegree of precision(or accuracy) of a quadrature rule I n(f)

Accuracy of quadrature formulas

The degree of precision (or accuracy) of a quadrature rule In(f ) is thepositive integer m such that

• I (p) = In(p) for every polynomial p of degree ≤m

• I (p) 6= In(p) for some polynomial p of degree m+ 1

In other words, the degree of precision is given by the largest integer msuch that all polynomials of degree less than m are exactly integrated bythe rule.

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.

Dana Mackey (DIT) Maths 10 / 31

Page 11: Numerical Methods II - Dublin Institute of Technologydmackey/lectures/NumNotes.pdf · Accuracy of quadrature formulas Thedegree of precision(or accuracy) of a quadrature rule I n(f)

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.

The following result (given without proof) gives a formula for the errorterm associated with each Newton-Cotes quadrature rule. This allows forthe degree of precision to be calculated.

Dana Mackey (DIT) Maths 11 / 31

Page 12: Numerical Methods II - Dublin Institute of Technologydmackey/lectures/NumNotes.pdf · Accuracy of quadrature formulas Thedegree of precision(or accuracy) of a quadrature rule I n(f)

Error terms

Let In(f ) denote a Newton-Cotes quadrature rule (open or closed) withn+ 1 abscissas.

If n is even then there exists a constant ce and a number ξe betweena and b such that

I (f ) = In(f )− ce(b−a)n+3f (n+2)(ξe)

Hence the degree of precision of In(f ) is n+ 1.

If n is odd, then there exists a constant co and a number ξo betweena and b such that

I (f ) = In(f )− co(b−a)n+2f (n+1)(ξo)

Hence the degree of precision of In(f ) is n.

Dana Mackey (DIT) Maths 12 / 31

Page 13: Numerical Methods II - Dublin Institute of Technologydmackey/lectures/NumNotes.pdf · Accuracy of quadrature formulas Thedegree of precision(or accuracy) of a quadrature rule I n(f)

Composite Newton-Cotes quadrature

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

Example: The trapezoidal rule can be written as

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

2[f (a) + f (b)]− c(b−a)3 f ′′(ξ)

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.

Dana Mackey (DIT) Maths 13 / 31

Page 14: Numerical Methods II - Dublin Institute of Technologydmackey/lectures/NumNotes.pdf · Accuracy of quadrature formulas Thedegree of precision(or accuracy) of a quadrature rule I n(f)

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

I (f ) =n

∑i=1

∫ xi

xi−1f (x)dx

=h

2

[f (a) + 2

n−1

∑i=1

f (xi ) + f (b)

]− c(b−a)h2 f ′′(ξ)

where ξ is a number between a and b.

This is the formula for the composite trapezoidal rule.

Dana Mackey (DIT) Maths 14 / 31

Page 15: Numerical Methods II - Dublin Institute of Technologydmackey/lectures/NumNotes.pdf · Accuracy of quadrature formulas Thedegree of precision(or accuracy) of a quadrature rule I n(f)

Rates of convergence

Note that Th(f ), the composite trapezoidal rule approximation to theintegral I (f ) with subintervals of length h, forms a sequence whichconverges to I (f ) as h→ 0.

Recall that, if a sequence xn converges to a limit L such that

|xn−L| ≤ C |yn|, for all sufficiently large n

where C is a constant and yn is a sequence which converges to 0 then wesay that xn converges to L with rate of convergence O(yn).

Easy to see that the rate of convergence for the composite trapezoidal ruleis O(h2).

Dana Mackey (DIT) Maths 15 / 31

Page 16: Numerical Methods II - Dublin Institute of Technologydmackey/lectures/NumNotes.pdf · Accuracy of quadrature formulas Thedegree of precision(or accuracy) of a quadrature rule I n(f)

Exercises

1 Derive the composite Simpson’s Rule:

I (f ) =h

3

[f (a) + 4

m

∑i=1

f (x2i−1 + 2m−1

∑i=1

f (2xi ) + f (b)

]−c(b−a)h4 f (4)(ξ),

where n = 2m. Show that its rate of convergence is O(h4).

2 Consider the integral

I (f ) =∫

π

0sin(x)dx

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

Dana Mackey (DIT) Maths 16 / 31

Page 17: Numerical Methods II - Dublin Institute of Technologydmackey/lectures/NumNotes.pdf · Accuracy of quadrature formulas Thedegree of precision(or accuracy) of a quadrature rule I n(f)

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−2f (x)dx

=h

3

[f (x0) + 4

m

∑j=1

f (x2j−1) + 2m−1

∑j=1

f (x2j) + f (x2m)

]− c(b−a)h4 f (4)(ξ)

Dana Mackey (DIT) Maths 17 / 31

Page 18: Numerical Methods II - Dublin Institute of Technologydmackey/lectures/NumNotes.pdf · Accuracy of quadrature formulas Thedegree of precision(or accuracy) of a quadrature rule I n(f)

Numerical verification of rate of convergence

We verified numerically, using the integral

I (f ) =∫

π

0sin(x)dx = 2,

that the composite trapezoidal rule has rate of convergence O(h2) andand the composite Simpson’s rule has rate of convergence O(h4).

This was achieved by showing thate2heh→ 4 as h→ 0, for the trapezoidal

rule ande2heh→ 16 as h→ 0 for Simpson’s rule.

Dana Mackey (DIT) Maths 18 / 31

Page 19: Numerical Methods II - Dublin Institute of Technologydmackey/lectures/NumNotes.pdf · Accuracy of quadrature formulas Thedegree of precision(or accuracy) of a quadrature rule I n(f)

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.

Dana Mackey (DIT) Maths 19 / 31

Page 20: Numerical Methods II - Dublin Institute of Technologydmackey/lectures/NumNotes.pdf · Accuracy of quadrature formulas Thedegree of precision(or accuracy) of a quadrature rule I n(f)

Example:

Using the integral

I (f ) =∫ 1

0

√1 + x3 dx

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

Dana Mackey (DIT) Maths 20 / 31

Page 21: Numerical Methods II - Dublin Institute of Technologydmackey/lectures/NumNotes.pdf · Accuracy of quadrature formulas Thedegree of precision(or accuracy) of a quadrature rule I n(f)

Application: Using error terms

It can be shown that the error term for the composite trapezoidal rule canbe more accurately expressed as

eh =(b−a)h2

12f ′′(ξ) =

(b−a)3

12n2f ′′(ξ), a≤ ξ≤ b,

while the error term for the composite Simpson’s rule is

eh =(b−a)h4

180f ′′(ξ) =

(b−a)5

180n4f ′′(ξ), a≤ ξ≤ b,

Dana Mackey (DIT) Maths 21 / 31

Page 22: Numerical Methods II - Dublin Institute of Technologydmackey/lectures/NumNotes.pdf · Accuracy of quadrature formulas Thedegree of precision(or accuracy) of a quadrature rule I n(f)

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

Dana Mackey (DIT) Maths 22 / 31

Page 23: Numerical Methods II - Dublin Institute of Technologydmackey/lectures/NumNotes.pdf · Accuracy of quadrature formulas Thedegree of precision(or accuracy) of a quadrature rule I n(f)

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.

Dana Mackey (DIT) Maths 23 / 31

Page 24: Numerical Methods II - Dublin Institute of Technologydmackey/lectures/NumNotes.pdf · Accuracy of quadrature formulas Thedegree of precision(or accuracy) of a quadrature rule I n(f)

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) and w1, 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.

Dana Mackey (DIT) Maths 24 / 31

Page 25: Numerical Methods II - Dublin Institute of Technologydmackey/lectures/NumNotes.pdf · Accuracy of quadrature formulas Thedegree of precision(or accuracy) of a quadrature rule I n(f)

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

)

Dana Mackey (DIT) Maths 25 / 31

Page 26: Numerical Methods II - Dublin Institute of Technologydmackey/lectures/NumNotes.pdf · Accuracy of quadrature formulas Thedegree of precision(or accuracy) of a quadrature rule I n(f)

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

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:

Dana Mackey (DIT) Maths 26 / 31

Page 27: Numerical Methods II - Dublin Institute of Technologydmackey/lectures/NumNotes.pdf · Accuracy of quadrature formulas Thedegree of precision(or accuracy) of a quadrature rule I n(f)

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

)

Dana Mackey (DIT) Maths 27 / 31

Page 28: Numerical Methods II - Dublin Institute of Technologydmackey/lectures/NumNotes.pdf · Accuracy of quadrature formulas Thedegree of precision(or accuracy) of a quadrature rule I n(f)

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

Dana Mackey (DIT) Maths 28 / 31

Page 29: Numerical Methods II - Dublin Institute of Technologydmackey/lectures/NumNotes.pdf · Accuracy of quadrature formulas Thedegree of precision(or accuracy) of a quadrature rule I n(f)

Example: 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: Using the two-point Gaussian quadrature formula, approximatethe integrals

(i)∫ 1

−1e−x dx ; (ii)

∫π

0sin(x)dx

and find the absolute error in each case.

Dana Mackey (DIT) Maths 29 / 31

Page 30: Numerical Methods II - Dublin Institute of Technologydmackey/lectures/NumNotes.pdf · Accuracy of quadrature formulas Thedegree of precision(or accuracy) of a quadrature rule I n(f)

Composite two-point Gaussian rule

Let h = (b−a)/n and xi = a = ih for i = 1, . . .n. Applying the basicGaussian rule on each interval [xi−1,xi ] show that the composite Gaussianquadrature rule is

∫ b

af (x)dx =

h

2

n

∑i=1

[f

(xi −

h

2−√

1

3

h

2

)+ f

(xi −

h

2+

√1

3

h

2

)]

+(b−a)h4

4320f (4)(ξ)

Dana Mackey (DIT) Maths 30 / 31

Page 31: Numerical Methods II - Dublin Institute of Technologydmackey/lectures/NumNotes.pdf · Accuracy of quadrature formulas Thedegree of precision(or accuracy) of a quadrature rule I n(f)

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.

Dana Mackey (DIT) Maths 31 / 31