However, sinceh= (b−a)/n, the bound simplifies ashn= (b−a).Therefore no matter how large nis, that is, how large the number of function evaluations is, the roundoff error is bounded by the same constant(b−a)which only depends on the size of the interval.
Exercise 4.2-4: (This problem shows that numerical quadrature is stable with respect to error in function values.) Assume the function values f(xi) are approximated by f(x˜ i), so that
|f(xi)−f˜(xi)|< for any xi ∈(a, b).Find an upper bound on the error of numerical quadrature Pwif(xi) when it is actually computed asP
wif˜(xi).
f(x) =x2, and f(x) =x3,we obtain the following equations:
w1+w2 = 2 w1x1+w2x2 = 0 w1x21+w2x22 = 2 3 w1x31+w2x32 = 0.
Solving the equations gives: w1 =w2= 1, x1= −
√ 3 3 , x2=
√ 3
3 .Therefore the quadrature rule is:
Z 1
−1
f(x)dx≈f −√ 3 3
! +f
√ 3 3
! . Observe that:
• The two nodes are not equally spaced on(−1,1).
• The accuracy of the rule is three, and it uses only two nodes. Recall that the accuracy of Simpson’s rule is also three but it uses three nodes. In general Gaussian quadrature gives a degree of accuracy of2n−1 using onlynnodes.
We were able to solve for the nodes and weights in the simple example above, however, as the number of nodes increases, the resulting non-linear system of equations will be very diffi- cult to solve. There is an alternative approach using the theory of orthogonal polynomials, a topic we will discuss in detail later. Here, we will use a particular set of orthogonal polynomials {L0(x), L1(x), ..., Ln(x), ...} called Legendre polynomials. We will give a definition of these poly- nomials in the next chapter. For this discussion, we just need the following properties of these polynomials:
• Ln(x) is a monic polynomial of degreenfor each n.
• R1
−1P(x)Ln(x)dx= 0 for any polynomial P(x) of degree less thann. The first few Legendre polynomials are
L0(x) = 1, L1(x) =x, L2(x) =x2− 1
3, L3(x) =x3−3
5x, L4(x) =x4−6
7x2+ 3 35.
How do these polynomials help with finding the nodes and the weights of the Gaussian quadrature rule? The answer is short: therootsof the Legendre polynomials are thenodesof the quadrature rule!
To summarize, theGauss-Legendre quadrature rule for the integral of f over (−1,1)is Z 1
−1
f(x)dx=
n
X
i=1
wif(xi)
where x1, x2, ..., xn are the roots of the nth Legendre polynomial, and the weights are computed using the following theorem.
Theorem 77. Suppose that x1, x2, ..., xn are the roots of the nth Legendre polynomial Ln(x) and the weights are given by
wi= Z 1
−1 n
Y
j=1,j6=i
x−xj
xi−xjdx.
Then the Gauss-Legendre quadrature rule has degree of accuracy 2n−1. In other words, if P(x) is any polynomial of degree less than or equal to2n−1, then
Z 1
−1
P(x)dx=
n
X
i=1
wiP(xi).
Proof. Let’s start with a polynomialP(x) with degree less than n. Construct the Lagrange inter- polant forP(x),using the nodes as x1, ..., xn
P(x) =
n
X
i=1
P(xi)li(x) =
n
X
i=1 n
Y
j=1,j6=i
x−xj xi−xj
P(xi).
There is no error term above because the error term depends on the nth derivative of P(x), but P(x) is a polynomial of degree less thann, so that derivative is zero. Integrate both sides to get
Z 1
−1
P(x)dx= Z 1
−1
n
X
i=1 n
Y
j=1,j6=i
x−xj xi−xjP(xi)
dx= Z 1
−1
n
X
i=1
P(xi)
n
Y
j=1,j6=i
x−xj xi−xj
dx
=
n
X
i=1
Z 1
−1
P(xi)
n
Y
j=1,j6=i
x−xj
xi−xjdx
=
n
X
i=1
Z 1
−1 n
Y
j=1,j6=i
x−xj
xi−xjdx
P(xi)
=
n
X
i=1
wiP(xi).
Therefore the theorem is correct for polynomials of degree less thann. Now letP(x)be a polynomial of degree greater than or equal ton, but less than or equal to2n−1.Divide P(x) by the Legendre polynomialLn(x) to get
P(x) =Q(x)Ln(x) +R(x).
Note that
P(xi) =Q(xi)Ln(xi) +R(xi) =R(xi) since Ln(xi) = 0 , for i= 1,2, ..., n.Also observe the following facts:
1. R1
−1Q(x)Ln(x)dx= 0,since Q(x) is a polynomial of degree less thann, and from the second property of Legendre polynomials.
2. R1
−1R(x)dx=Pn
i=1wiR(xi), since R(x) is a polynomial of degree less than n, and from the first part of the proof.
Putting these facts together we get:
Z 1
−1
P(x)dx= Z 1
−1
[Q(x)Ln(x) +R(x)]dx= Z 1
−1
R(x)dx=
n
X
i=1
wiR(xi) =
n
X
i=1
wiP(xi).
Table 4.1 displays the roots of the Legendre polynomials L2, L3, L4, L5 and the corresponding weights.
n Roots Weights
2 √13 = 0.5773502692 1
−√1
3 =−0.5773502692 1
3 −(35)1/2 =−0.7745966692 59 = 0.5555555556 0.0 89 = 0.8888888889 (35)1/2 = 0.7745966692 59 = 0.5555555556
4 0.8611363116 0.3478548451
0.3399810436 0.6521451549 -0.3399810436 0.6521451549 -0.8611363116 0.3478548451
5 0.9061798459 0.2369268850
0.5384693101 0.4786286705
0.0 0.5688888889
-0.5384693101 0.4786286705 -0.9061798459 0.2369268850 Table 4.1: Roots of Legendre polynomials L2 through L5
Example 78. ApproximateR1
−1cosxdxusing Gauss-Legendre quadrature with n= 3 nodes.
Solution. From Table 4.1, and using two-digit rounding, we have Z 1
−1
cosxdx≈0.56 cos(−0.77) + 0.89 cos 0 + 0.56 cos(0.77) = 1.69 and the true solution issin(1)−sin(−1) = 1.68.
So far we discussed integrating functions over the interval (−1,1). What if we have a different integration domain? The answer is simple: change of variables! To compute Rb
af(x)dx for any a < b, we use the following change of variables:
t= 2x−a−b
b−a ⇔x= 1
2[(b−a)t+a+b]
With this substitution, we have Z b
a
f(x)dx= b−a 2
Z 1
−1
f 1
2[(b−a)t+a+b]
dt.
Now we can approximate the integral on the right-hand side as before.
Example 79. ApproximateR1
0.5xxdx using Gauss-Legendre quadrature withn= 2 nodes.
Solution. Transform the integral using x= 12(0.5t+ 1.5) = 12(2t +32) = t+34 , dx= dt4 to get:
Z 1 0.5
xxdx= 1 4
Z 1
−1
t+ 3 4
t+34 dt.
Forn= 2 1 4
Z 1
−1
t+ 3 4
t+34
dt≈ 1 4
1
4√ 3 +3
4
1
4√ 3+34
+
− 1 4√
3 +3 4
− 1
4√ 3+34
= 0.410759, using six significant digits. We will next use Python for a five-node computation of the integral.
Python code for Gauss-Legendre rule with five nodes
The following code computes the Gauss-Legendre rule forR1
−1f(x)dxusingn= 5nodes. The nodes and weights are from Table 4.1.
In [1]: def gauss(f):
return 0.2369268851*f(-0.9061798459) + 0.2369268851*f(0.9061798459) + \ 0.5688888889*f(0) + 0.4786286705*f(0.5384693101) + \
0.4786286705*f(-0.5384693101)
Now we compute 14R1
−1 t+3
4
t+34
dt using the code:
In [2]: 0.25*gauss(lambda t: (t/4+3/4)**(t/4+3/4)) Out[2]: 0.41081564812239885
The next theorem is about the error of the Gauss-Legendre rule. Its proof can be found in Atkinson [3]. The theorem shows, in particular, that the degree of accuracy of the quadrature rule, using nnodes, is2n−1.
Theorem 80. Let f ∈C2n[−1,1].The error of Gauss-Legendre rule satisfies Z b
a
f(x)dx−
n
X
i=1
wif(xi) = 22n+1(n!)4 (2n+ 1) [(2n)!]2
f(2n)(ξ) (2n)!
for someξ ∈(−1,1).
Using Stirling’s formula n!∼ e−nnn(2πn)1/2, where the symbol ∼means the ratio of the two sides converges to 1 asn→ ∞, it can be shown that
22n+1(n!)4
(2n+ 1) [(2n)!]2 ∼ π 4n.
This means the error of Gauss-Legendre rule decays at an exponential rate of 1/4n as opposed to, for example, the polynomial rate of1/n4 for composite Simpson’s rule.
Exercise 4.3-1: Prove that the sum of the weights in Gauss-Legendre quadrature is 2, for any n.
Exercise 4.3-2: Approximate R1.5
1 x2logxdx using Gauss-Legendre rule with n = 2 and n= 3. Compare the approximations to the exact value of the integral.
Exercise 4.3-3: A composite Gauss-Legendre rule can be obtained similar to compos- ite Newton-Cotes formulas. Consider R2
0 exdx. Divide the interval (0,2) into two subintervals (0,1),(1,2) and apply the Gauss-Legendre rule with two-nodes to each subinterval. Compare the estimate with the estimate obtained from the Gauss-Legendre rule with four-nodes applied to the whole interval (0,2).