notes-ch4

19
Lecture Notes for Math-CSE 451: Introduction to Numerical Computation Wen Shen 2011

Upload: atul-khatri

Post on 07-Feb-2016

213 views

Category:

Documents


0 download

DESCRIPTION

Notes-Ch4

TRANSCRIPT

Page 1: Notes-Ch4

Lecture Notes for Math-CSE 451:

Introduction to Numerical Computation

Wen Shen

2011

Page 2: Notes-Ch4

2

These notes are used by myself in the course. They are provided to students. Thesenotes are self-contained, and covers almost everything we go through in class. However,if a student shall be interested in more detailed discussion, he/she should consult therecommended textbook.

Text: Guide to Scientific Computing, 2nd edition, by Peter Turner. CRC Press, ISBN0-8493-1242-6.

Page 3: Notes-Ch4

Chapter 4

Numerical integration

4.1 Introduction

Problem: Given a function f(x), defined on an interval [a, b], we want to find anapproximation to the integral

I(f) =

∫ b

af(x) dx .

Main idea:

• Cut up [a, b] into smaller sub-intervals;

• In each sub-interval, find a polynomial pi(x) ≈ f(x);

• Integrate pi(x) on each sub-interval, and sum them up.

4.2 Trapezoid rule

The grid: We cut up [a, b] into n sub-intervals:

x0 = a, xi < xi+1, xn = b

On interval [xi, xi+1], approximate f(x) by a linear polynomial that interpolates the 2end points, i.e,

pi(xi) = f(xi), pi(xi+1) = f(xi+1).

See Figure 4.1. We see that on each sub-interval, the integral of pi equals to the area ofa trapezium:

∫ xi+1

xi

pi(x) dx =1

2(f(xi) + f(xi+1))(xi+1 − xi).

3

Page 4: Notes-Ch4

4 CHAPTER 4. NUMERICAL INTEGRATION

x0 = a xi xi+1 xn = b

pi(x)f(x)

Figure 4.1: Trapezoid rule: straight line approximation in each sub-interval.

Now, we use

∫ xi+1

xi

f(x) dx ≈∫ xi+1

xi

pi(x) dx =1

2(f(xi+1) + f(xi)) (xi+1 − xi) ,

and we sum up all the sub-intervals

∫ b

af(x) dx =

n−1∑

i=0

∫ xi+1

xi

f(x) dx ≈n−1∑

i=0

∫ xi+1

xi

pi(x) dx

=

n−1∑

i=0

1

2(f(xi+1) + f(xi)) (xi+1 − xi)

We now consider uniform grid, and set

h =b− a

n, xi+1 − xi = h.

In this case, we have

∫ b

af(x) dx =

n−1∑

i=0

h

2(f(xi) + f(xi+1))

=h

2[(f(x0) + f(x1)) + (f(x1) + f(x2)) + · · ·+ (f(xn−1) + f(xn))]

=h

2

[

f(x0) + 2

n−1∑

i=1

f(xi) + f(xn)

]

= h

[

1

2f(x0) +

n−1∑

i=1

f(xi) +1

2f(xn)

]

︸ ︷︷ ︸

T (f ;h)

Page 5: Notes-Ch4

4.2. TRAPEZOID RULE 5

so we can write∫ b

af(x) ≈ T (f ;h)

Example 1: Let f(x) =√x2 + 1, and we want to compute

I =

∫ 1

−1f(x) dx

by Trapezoid rule. If we take n = 10, we can set up the data

i xi fi0 −1 1.41421361 −0.8 1.28062482 −0.6 1.16619043 −0.4 1.0770334 −0.2 1.01980395 0 1.06 0.2 1.01980397 0.4 1.0770338 0.6 1.16619049 0.8 1.280624810 1 1.4142136

Here h = 2/10 = 0.2. By the formula, we get

T = h

[

(f0 + f10)/2 +

9∑

i=1

fi

]

= 2.3003035.

Sample codes. Here are some possible ways to program trapezoid rule in Matlab.Let a, b, n be given. The function f(x) is also defined, such that it takes a vector ‘x’ andreturns a vector with same size as ‘x’. For example, f(x) = x2+sin(x) could be definedas:

function v=func(x)

v=x.^2 + sin(x);

end

In the following code, the integral value is stored in the variable ‘T’.

h=(b-a)/n;

T = (func(a)+func(b))/2;

for i=1:n-1

x = a+i*h;

Page 6: Notes-Ch4

6 CHAPTER 4. NUMERICAL INTEGRATION

T = T + func(x);

end

T = T*h;

Or, one may use directly the Matlab vector function ‘sum’, and the code could be veryshort:

h=(b-a)/n;

x=[a+h:h:b-h]; % inner points

T = ((f(a)+f(b))/2 + sum(f(x)))*h;

Error estimates. We define the error:

ET (f ;h)=I(f)− T (f ;h) =

n−1∑

i=0

∫ xi+1

xi

[f(x)− pi(x)

]dx =

n−1∑

i=0

ET,i(f ;h),

where

ET,i(f ;h) =

∫ xi+1

xi

[f(x)− pi(x)

]dx, (i = 0, 1, · · · , n− 1)

is the error on each sub-interval.

We know from polynomial interpolation that

f(x)− pi(x) =1

2f ′′(ξi)(x− xi)(x− xi+1), (xi < ξi < xi+1)

Error estimate on each sub-interval:

ET,i(f ;h) =1

2f ′′(ξi)

∫ xi+1

xi

(x− xi)(x− xi+1) dx = − 1

12h3f ′′(ξi).

(You may work out the details of the integral!)

The total error is:

ET (f ;h) =

n−1∑

i=0

ET,i(f ;h) =

n−1∑

i=0

− 1

12h3f ′′(ξi) = − 1

12h3

[n−1∑

i=0

f ′′(ξi)

]

· 1n

︸ ︷︷ ︸

· b− a

h︸ ︷︷ ︸

= f ′′(ξ) = n

which gives

ET (f ;h) = − b− a

12h2f ′′(ξ), ξ ∈ (a, b).

Error bound

|ET (f ;h)| ≤ b− a

12h2 max

x∈(a,b)

∣∣f ′′(x)

∣∣ .

Page 7: Notes-Ch4

4.3. SIMPSON’S RULE 7

Example 2. Consider function f(x) = ex, and the integral

I(f) =

∫ 2

0ex dx

What is the minimum number of points to be used in the trapezoid rule to ensure enerror ≤ 0.5× 10−4?

Answer. We have

f ′(x) = ex, f ′′(x) = ex, a = 0, b = 2

somaxx∈(a,b)

∣∣f ′′(x)

∣∣ = e2.

By error bound, it is sufficient to require

|ET (f ;h)| ≤1

6h2e2 ≤ 0.5 × 10−4

⇒ h2 ≤ 0.5 × 10−4 × 6× e−2 ≈ 4.06 × 10−5

⇒ 2

n= h ≤

4.06× 10−5 = 0.0064

⇒ n ≥ 2

0.0064≈ 313.8

We need at least 314 points.

4.3 Simpson’s rule

We now explorer possibility of using higher order polynomials.

We cut up [a, b] into 2n equal sub-intervals

x0 = a, x2n = b, h =b− a

2n, xi+1 − xi = h

Consider the interval [x2i, x2i+2]. We will find a 2nd order polynomial that interpolatesf(x) at the points

x2i, x2i+1, x2i+2

See Figure 4.2. Note that in each sub-interval there is a point in the interior, namely,x2i+1.

We now use the Lagrange form, and get

pi(x) = f(x2i)(x− x2i+1)(x− x2i+2)

(x2i − x2i+1)(x2i − x2i+2)+ f(x2i+1)

(x− x2i)(x− x2i+2)

(x2i+1 − x2i)(x2i+1 − x2i+2)

+f(x2i+2)(x− x2i)(x− x2i+1)

(x2i+2 − x2i)(x2i+2 − x2i+1)

Page 8: Notes-Ch4

8 CHAPTER 4. NUMERICAL INTEGRATION

x2i x2i+1 x2i+2

pi(x)f(x)

Figure 4.2: Simpson’s rule: quadratic polynomial approximation (thick line) in eachsub-interval.

With uniform nodes, this becomes

pi(x) =1

2h2f(x2i)(x− x2i+1)(x− x2i+2)−

1

h2f(x2i+1)(x− x2i)(x− x2i+2)

+1

2h2f(x2i+2)(x− x2i)(x− x2i+1)

We work out the integrals (try to fill in the details yourself!)∫ x2i+2

x2i

(x− x2i+1)(x− x2i+2) dx =2

3h3,

∫ x2i+2

x2i

(x− x2i)(x− x2i+2) dx = −4

3h3,

∫ x2i+2

x2i

(x− x2i)(x− x2i+1) dx =2

3h3,

Then∫ x2i+2

x2i

pi(x) dx =1

2h2f(x2i)

∫ x2i+2

x2i

(x− x2i+1)(x− x2i+2) dx

− 1

h2f(x2i+1)

∫ x2i+2

x2i

(x− x2i)(x− x2i+2) dx

+1

2h2f(x2i+2)

∫ x2i+2

x2i

(x− x2i)(x− x2i+1) dx

we have ∫ x2i+2

x2i

pi(x) dx =h

3[f(x2i) + 4f(x2i+1) + f(x2i+2)] .

We now sum them up

∫ b

af(x) dx ≈ S(f ;h) =

n−1∑

i=0

∫ x2i+2

x2i

pi(x) dx =h

3

n−1∑

i=0

[f(x2i) + 4f(x2i+1) + f(x2i+2)] .

Page 9: Notes-Ch4

4.3. SIMPSON’S RULE 9

x2i−2 x2i−1 x2i x2i+1 x2i+2

1 4 1

1 4 1= 2

Figure 4.3: Simpson’s rule: adding the constants in each node.

See Figure 4.3 for the counting of coefficients on each node. We see that for x0, x2n weget 1, and for odd indices we have 4, and for all remaining even indices we get 2.

The algorithm looks like:

S(f ;h) =h

3

[

f(x0) + 4n∑

i=1

f(x2i−1) + 2n−1∑

i=1

f(x2i) + f(x2n)

]

Example 3: Let f(x) =√x2 + 1, and we want to compute

I =

∫ 1

−1f(x) dx

by Simpson’s rule. If we take n = 5, which means we take 2n + 1 = 11 points, we canset up the data (same as in Example 1):

i xi fi0 −1 1.41421361 −0.8 1.28062482 −0.6 1.16619043 −0.4 1.0770334 −0.2 1.01980395 0 1.06 0.2 1.01980397 0.4 1.0770338 0.6 1.16619049 0.8 1.280624810 1 1.4142136

Here h = 2/10 = 0.2. By the formula, we get

S(f ; 0.2) =h

3[f0 + 4(f1 + f3 + f5 + f7 + f9) + 2(f2 + f4 + f6 + f8) + f10] = 2.2955778.

(This is somewhat smaller than the number we get with trapezoid rule, and it is actuallymore accurate. Could you intuitively explain that for this particular example?)

Sample codes: Let a, b, n be given, and let the function ’func’ be defined. To find theintegral with Simpson’s rule, one could possibly follow the following algorithm:

Page 10: Notes-Ch4

10 CHAPTER 4. NUMERICAL INTEGRATION

h=(b-a)/2/n;

xodd=[a+h:2*h:b-h]; % x_i with odd indices

xeven=[a+2*h:2*h:b-2*h]; % x_i with even indices

SI=(h/3)*(func(a)+4*sum(func(xodd))+2*sum(func(xeven))+func(b));

Error estimate. One can prove that the basic error on each sub-interval is: (see theproof below)

ES,i(f ;h) = − 1

90h5f (4)(ξi), ξi ∈ (x2i, x2i+2) . (4.1)

Then, the total error is

ES(f ;h) = I(f)− S(f ;h) = − 1

90h5

n−1∑

i=0

f (4)(ξi)1

n· b− a

2h= −b− a

180h4f (4)(ξ), ξ(a, b)

This gives us the error bound

|ES(f ;h)| ≤b− a

180h4 max

x∈(a,b)

∣∣∣f (4)(x)

∣∣∣ .

Proof. for (4.1): (optional) For notation simplicity, let’s consider an interval [a, a+2h],and approximate the integral by

∫ a+2h

af(x) dx ≈ h

3

[

f(a) + 4f(a+ h) + f(a+ 2h)]

.

Use Taylor expansions for f :

f(a+ h) = f(a) + hf ′(a) +h2

2f ′′(a) +

h3

6f ′′′(a) +

h4

24f (4)(a) + · · ·

f(a+ 2h) = f(a) + 2hf ′(a) + 2h2f ′′(a) +4h3

3f ′′′(a) +

2h4

3f (4)(a) + · · ·

we get

f(a)+4f(a+h)+f(a+2h) = 6f(a)+6hf ′(a)+4h2f ′′(a)+2h3f ′′′(a)+5

6h4f (4)(a)+ · · ·

therefore

h

3

[

f(a)+4f(a+h)+f(a+2h)]

= 2hf(a)+2h2f ′(a)+4

3h3f ′′(a)+

2

3h4f ′′′(a)+

5

18h5f (4)(a)+· · ·

Now we go back to the original integral. We observe that

∫ a+2h

af(x) dx =

∫ 2h

0f(a+ s) ds.

Page 11: Notes-Ch4

4.4. RECURSIVE TRAPEZOID RULE 11

Using the Taylor expansion

f(a+ s) = f(a) + sf ′(a) +s2

2f ′′(a) +

s3

6f ′′′(a) +

s4

24f (4)(a) + · · ·

we get, by assuming the integral of each term in the Taylor series:

∫ 2h

0f(a+ s) ds =

∫ 2h

0

[

f(a) + sf ′(a) +s2

2f ′′(a) +

s3

6f ′′′(a) +

s4

24f (4)(a) + · · ·

]

ds

= 2hf(a) + f ′(a)

∫ 2h

0s ds+ f ′′(a)

∫ 2h

0

s2

2ds+ f ′′′(a)

∫ 2h

0

s3

6ds

+f (4)(a)

∫ 2h

0

s4

24ds+ · · ·

= 2hf(a) + 2h2f ′(a) +4

3h3f ′′(a) +

2

3h4f ′′′(a) +

4

15h5f (4)(a) + · · ·

Comparing this with the Simpson’s rule, we get the error

ES,i =[ 4

15− 5

18

]

h5f (4)(a)+ · · · = − 1

90h5f (4)(a)+ · · · = − 1

90h5f (4)(ξ), ξ ∈ (a, a+2h)

which proves (4.1).

Example 4. With f(x) = ex in [0, 2], we now use Simpson’s rule. In order to achievean error ≤ 0.5× 10−4, how many points must we take?

Answer. We have

|ES(f ;h)| ≤ 2

180h4e2 ≤ 0.5× 10−4

⇒ h4 ≤ 0.5−4 × 180/e2 = 1.218 × 10−3

⇒ h ≤ 0.18682

⇒ n =b− a

2h= 5.3 ≈ 6

We need at least 2n+ 1 = 13 points.

Recall: With trapezoid rule, we need at least 314 points. The Simpson’s rule uses muchfewer points.

4.4 Recursive trapezoid rule

These are also called composite schemes.

Divide [a, b] into 2n equal sub-intervals, see Figure 4.4.

hn =b− a

2n, hn+1 =

1

2h

Page 12: Notes-Ch4

12 CHAPTER 4. NUMERICAL INTEGRATION

n = 1

n = 2

n = 3

n = 4

n = 5

n = 6

n = 7

Figure 4.4: Recursive division of intervals, first few levels

So

T (f ;hn) = hn ·[

1

2f(a) +

1

2f(b) +

2n−1∑

i=1

f(a+ ihn)

]

T (f ;hn+1) = hn+1 ·

1

2f(a) +

1

2f(b) +

2n+1−1∑

i=1

f(a+ ihn+1)

We can re-arrange the terms in T (f ;hn+1):

T (f ;hn+1) =hn2

1

2f(a) +

1

2f(b) +

2n−1∑

i=1

f(a+ ihn) +2n−1∑

j=0

f(a+ (2j + 1)hn+1)

=1

2T (f ;hn) + hn+1

2n−1∑

j=0

f(a+ (2j + 1)hn+1)

Advantages:

1. One can keep the computation for a level n. If this turns out to be not accurateenough, then add one more level to get better approximation. ⇒ flexibility.

2. This formula allows us to compute a sequence of approximations to a define integralusing the trapezoid rule without re-evaluating the integrand at points where it hasalready been evaluated. ⇒ efficiency.

4.5 Romberg Algorithm

Assuming now we have generated a sequence of approximations, say by Trapezoid rule,with different values of h. Call them T (f ;h), T (f ;h/2), T (f ;h/4), · · · One could combinethese numbers in particular ways to get much higher order approximations.

The particular form of the eventual algorithm depends on the error formula. Considerthe trapezoid rule. If f (n) exists and is bounded, then one can prove that the error

Page 13: Notes-Ch4

4.5. ROMBERG ALGORITHM 13

satisfies the Euler MacLaurin’s formula

E(f ;h) = I(f)− T (f ;h) = a2h2 + a4h

4 + a6h6 + · · ·+ anh

n

Here an depends on the derivatives f (n). Note that we only have the even power termsof h in the error! Due to symmetry of the methods, all terms with hk where k is odd,would be cancelled out in the error formula!

When we half the grid size h, the error formula becomes

E(f ;h

2) = I(f)− T (f ;

h

2) = a2(

h

2)2 + a4(

h

2)4 + a6(

h

2)6 + · · ·+ an(

h

2)n

We have

(1) I(f) = T (f ;h) + a2h2 + a4h

4 + a6h6 + · · ·

(2) I(f) = T (f ;h

2) + a2(

h

2)2 + a4(

h

2)4 + a6(

h

2)6 + · · ·+ an(

h

2)n

The goal is to use the 2 approximations T (f ;h) and T (f ; h2 ) to get one that’s moreaccurate, i.e., we wish to cancel the leading error term, the one with h2.

Multiplying (2) by 4 and subtract (1), we get

3 · I(f) = 4 · T (f ;h/2) − T (f ;h) + a′4h4 + a′6h

6 + · · ·

⇒ I(f) =4

3T (f ;h/2)− 1

3T (f ;h)

︸ ︷︷ ︸

+a4h4 + a6h

6 + · · ·

U(h)

U(h) is of 4-th order accuracy! Much better than T (f ;h). We now write:

U(h) = T (f ;h/2) +T (f ;h/2)− T (f ;h)

22 − 1

This idea is called the Richardson extrapolation.

Then, we could compute U(h/2), U(h/4), U(h/8), · · · .We can now go to the next level:

(3) I(f) = U(h) + a4h4 + a6h

6 + · · ·(4) I(f) = U(h/2) + a4(h/2)

4 + a6(h/2)6 + · · ·

To cancel the term with h4, we do: (4) × 24 − (3)

(24 − 1)I(f) = 24U(h/2) − U(h) + a′6h6 + · · ·

Let

V (h) =24U(h/2) − U(h)

24 − 1= U(h/2) +

U(h/2) − U(h)

24 − 1.

Page 14: Notes-Ch4

14 CHAPTER 4. NUMERICAL INTEGRATION

ThenI(f) = V (h) + a′6h

6 + · · ·

So V (h) is even better than U(h).

One can keep doing this several layers, until desired accuracy is reached.

This gives the Romberg Algorithm: Set H = b− a, define:

R(0, 0) = T (f ;H) =H

2(f(a) + f(b))

R(1, 0) = T (f ;H/2)

R(2, 0) = T (f ;H/(22))

...

R(n, 0) = T (f ;H/(2n))

Here R(n, 0)’s are computed by the recursive trapezoid formula.

Romberg triangle: See Figure 4.5.

R(n, 0) R(n, 1) R(n, 3) R(n, n)

R(3, 0) R(3, 1) R(3, 2)

R(2, 0) R(2, 1) R(2, 2)

R(1, 0) R(1, 1)

R(0, 0)

s-

s-

s-

s-

s-

s-

Figure 4.5: Romberg triangle

The entry R(n,m) is computed as

R(n,m) = R(n,m− 1) +R(n,m− 1)−R(n− 1,m− 1)

22m − 1

Accuracy:

I(f) = R(n,m) +O(h2(m+1)), h =H

2m.

Algorithm can be done either column-by-column or row-by-row.

Here we give some pseudo-code, using column-by-column.

R =romberg(f, a, b, n)

R = n× n matrix

Page 15: Notes-Ch4

4.6. ADAPTIVE SIMPSON’S QUADRATURE SCHEME 15

h = b− a; R(1, 1) = [f(a) + f(b)] ∗ h/2;for i = 1 to n− 1 do %1st column recursive trapezoid

R(i+ 1, 1) = R(i, 1)/2;

h = h/2;

for k = 1 to 2i−1 do

R(i+ 1, 1) = R(i+ 1, 1) + h ∗ f(a+ (2k − 1)h)

end

end

for j = 2 to n do %2 to n column

for i = j to n do

R(i, j) = R(i, j − 1) + 14j−1

[R(i, j − 1)−R(i− 1, j − 1)]

end

end

4.6 Adaptive Simpson’s quadrature scheme

It is intuitive that the error for numerical integration would be larger where the functionchanges a lot, i.e., the derivatives f (k) are big. If one uses uniform grid, then the error isnot evenly distributed, and we have little control over the accuracy. Therefore, it wouldbe desirable to insert more points where error is otherwise large.

We illustrate this idea using Simpson’s rile.

For interval [a, b], h = b−a2 ,

S1[a, b] =b− a

6

[

f(a) + 4f(a+ b

2) + f(b)

]

Error form:

E1[a, b] = − 1

90h5f (4)(ξ), ξ(a, b)

ThenI(f) = S1[a, b] + E1[a, b]

Divide [a, b] up in the middle, let c = a+b2 .

I(f)[a, b] = I(f)[a, c] + I(f)[c, b]

= S1[a, c] +E1[a, c] + S1[c, b] + E1[c, b]

= S2[a, b] + E2[a, b]

where

S2[a, b] = S1[a, c] + S1[c, b]

E2[a, b] = E1[a, c] + E1[c, b] = − 1

90(h/2)5

[

f (4)(ξ1) + f (4)(ξ2)]

Page 16: Notes-Ch4

16 CHAPTER 4. NUMERICAL INTEGRATION

Assume f (4) does NOT change much, then E1[a, c] ≈ E1[c, b], and

E2[a, b] ≈ 2E1[a, c] = 21

25E1[a, b] =

1

24E1[a, b]

This gives

S2[a, b]− S1[a, b] = (I − E2[a, b])− (I − E1[a, b]) = E1 − E2 = 24E2 − E2 = 15E2

This means, we can compute the error E2:

E2 =1

15(S2 − S1)

If we wish to have |E2| ≤ ε, we only need to require

S2 − S1

24 − 1≤ ε

This gives the idea of an adaptive recursive formula:

(A) I = S1 + E1

(B) I = S2 + E2 = S2 +1

24E1

(B) ∗ 24 − (A) gives(24 − 1)I = 24S2 − S1

⇒ I =24S2 − S1

24 − 1= S2 +

S2 − S1

15

Note that this gives the best approximation when f (4) ≈ const.

Pseudocode: f : function, [a, b] interval, ε: tolerance for error

answer=Simpson(f, a, b, ε)

compute S1 and S2

If |S2 − S1| < 15ε

answer= S2 + (S2 − S1)/15;

else

c = (a+ b)/2;

Lans=Simpson(f, a, c, ε/2);

Rans=Simpson(f, c, b, ε/2);

answer=Lans+Rans;

end

Page 17: Notes-Ch4

4.7. GAUSSIAN QUADRATURE FORMULAS 17

In Matlab, one can use quad to compute numerical integration. Try help quad, it willgive you info on it. One can call the program by using:

a=quad(’fun’,a,b,tol)

It uses adaptive Simpson’s formula.

See also quad8, higher order method.

Remark: One could also design an adaptive trapezoid method. Would you like to try itand work out the algorithm?

Of course, due to the higher accuracy of Simpson’s rule, it is the mostly commonly usedone, even in adaptive form.

4.7 Gaussian quadrature formulas

We notice that all the numerical integration rule follow the form∫ b

af(x) dx ≈ A0f(x0) +A1f(x1) + · · ·+Anf(xn) . (4.2)

Here xi ∈ [a, b] are called the nodes, and Ai’s are the weights.

For example, the trapezoid rule is:

x0 = a, x1 = b, A0 = A1 = 1

and the Simpson’s rule corresponds to

x0 = a, x1 =a+ b

2, x2 = b, A0 = A2 = (b− a)

1

6, A1 = (b− a)

2

3.

In these rules, we fix the points xi, then we adjust the weights Ai.

If now we allow the nodes xi also to be adjusted, we gain higher degree of freedom, andhigher accuracy as well. We will explain the details below.

Gaussian quadrature: One chooses the nodes xi and weight Ai (i = 0, 1, 2, · · · , N)such that (4.2) gives the exact value for polynomial functions f(x) of highest possibledegree m:

∫ b

af(x) dx = A0f(x0) +A1f(x1) + · · ·+Anf(xm) , if f ∈ Pm. (4.3)

Here, m is called the degree of precision.

To fix the idea, we consider now the interval [−1, 1]. Let’s start with N = 1, i.e., wehave 2 nodes x0, x1 and 2 weights A0, A1. If (4.2) gives the exact value for polynomialsof degree m, then it must be exact for functions 1, x, x2, x3, · · · , xm. We will need 4equations, therefore we must have m = 2N + 1 = 3. Recall that

∫ 1

−1xk dx =

{2

k+1 , k even

0, k odd.

Page 18: Notes-Ch4

18 CHAPTER 4. NUMERICAL INTEGRATION

This leads to

f(x) = 1 : A0 +A1 = 2

f(x) = x : A0x0 +A1x1 = 0

f(x) = x2 : A0x20 +A1x

21 =

2

3f(x) = x3 : A0x

30 +A1x

31 = 0

One could solve this system and find

x0 = − 1√3, x1 =

1√3, A0 = 1, A1 = 1.

Note the symmetry: x0 = −x1 and A0 = A1, which should be expected and could beused to simply the computation! Then, we have the Gaussian quadrature rule for n = 1,

∫ 1

−1f(x) dx ≈ f

(

− 1√3

)

+ f

(1√3

)

.

This will have degree of precision 3.

Now, let’s consider N = 2, so we have nodes x0, x1, x2 and weights A0, A1, A2, and therule should give exact solution for polynomials of degree m = 5. By symmetry, weimmediately have

x0 = −x2, x1 = 0, A0 = A2,

which reduces the number of unknowns to 3 and simplifies a lot the computation. Let’snow check it with f(x) = 1, x2, x4:

f(x) = 1 : 2A0 +A1 = 2

f(x) = x2 : 2A0x20 =

2

3

f(x) = x4 : 2A0x40 =

2

5

We get

x0 =√

3/5, x1 = 0, x2 = −√

3/5, A0 = A2 = 5/9, A1 = 8/9.

This gives the Gaussian quadrature rule:

∫ 1

−1f(x) dx ≈ 1

9

[

5f(

−√

3/5)

+ 8f(0) + 5f(√

3/5)]

.

This will have degree of precision 5.

(Optional discussion) This is a well-studied problem. It turns out that the nodes xi areroots of Legendre polynomials qn+1(x). On the interval [−1, 1], they are

q0(x) = 1, q1(x) = x, q2(x) =3

2x2 − 1

2, q3(x) =

5

2x3 − 3

2x.

Page 19: Notes-Ch4

4.7. GAUSSIAN QUADRATURE FORMULAS 19

Their roots areq1 : 0, q2 : ±1/

√3, q3 : 0, ±

3/5.

The weights Ai can be computed as

Ai =

∫ b

−ali(x) dx, li(x) =

n∏

j=0,j 6=i

x− xjxi − xj

.

See Table 4.1 for a list of Gaussian quadrature nodes and weights for n ≤ 5.

Table 4.1: Gaussian Quadrature Nodes and Weights

n Nodes xi Weights Ai

1 0 2

2 ±√

1/3 1

3 ±√0.6 5/90 8/9

4 ±√

(3−√4.8)/7 (18 +

√30)/36

±√

(3 +√4.8)/7 (18−

√30)/36

5 ±√

(5−√

40/7)/9 (322 + 13√70)/900

0 128/225

±√

(5 +√

40/7)/9 (322 − 13√70)/900

(Required) For general interval [a, b], use the transformation:

t =2x− (a+ b)

b− a, x =

1

2(b− a)t+

1

2(a+ b)

so for −1 ≤ t ≤ 1 we have a ≤ x ≤ b.

Then, for the node and weight (ti, Ai) on [−1, 1], (as in the table), one can change theminto new node and weight (xi, Ai) for interval [a, b] by

xi =1

2(b− a)ti +

1

2(a+ b), Ai =

b− a

2Ai.

Advantage: (1). Higher order accuracy. (2). Since all nodes are in the interior of theinterval, these formulas can handle integrals of function that tends to infinite value atone end of the interval (provided that the integral is defined). Examples:

∫ 1

0x−1 dx,

∫ 1

0(x2 − 1)1/3

sin(ex − 1) dx.