Skip to main content

12  Gaussian quadrature

Lecture 12: Chapter 4: Numerical integration

These lecture notes are mainly a record of what we discussed in class. They should not be used as a replacement for attending lectures or reading books. Please let me know if you find any errors or have any questions about the material.

You can use the Hypothesis commenting system available on the right side panel (you can login with your U/Google account). This allows you annotate the notes (private) and leave comments or reply to other people’s comments (public).

I encourage you to download the lecture notes and play around with the Julia code.

12.1 Previously…..

  • Quadrature rules: \(\int_a^b f \approx \sum_j w_j f(x_j)\) with weights \(\{w_j\}\) and nodes \(\{x_j\}\),
  • Interpolatory rules: integrate the polynomial interpolant \(p(x) = \sum_{j=0}^n f(x_j) \ell_j(x)\) of \(f\) on \(X = \{x_0,\dots,x_n\}\), which gives the weights \(w_j = \int_a^b \ell_j(x) \mathrm{d}x\); when \(X\) is equispaced this is a Newton-Cotes rule,
  • Examples: rectangular rule \((b-a) f(a)\), midpoint rule \((b-a) f\big(\tfrac{a+b}{2}\big)\), trapezoid rule \(\frac{b-a}{2}\big( f(a) + f(b) \big)\), and Simpson’s rule \(\frac{b-a}{6}\big( f(a) + 4 f\big(\tfrac{a+b}{2}\big) + f(b) \big)\),
  • A rule is exact for all polynomials of degree \(N\) if \(\int_a^b p(x) \mathrm{d}x = \sum_j w_j p(x_j)\) for all \(p \in \mathcal P_N\); interpolatory rules on \(n+1\) nodes are exact on \(\mathcal P_n\) (rectangular & midpoint exact for degree \(0\), trapezoid degree \(1\), Simpson degree \(2\))
  • We also saw that, for \(n\) even and with nodes symmetric about \(\tfrac{a+b}2\), the rule is exact on \(\mathcal P_{n+1}\) (i.e. midpoint: degree \(1\), Simpson: degree \(3\)). Proof: \(\int_{a}^b (x-\frac{a+b}2)^{n+1} \mathrm{d}x = 0\) when \(n\) is even.
  • Error estimate: if the rule is exact on \(\mathcal P_N\), then use the error estimate for polynomial interpolation to obtain \[ \left| \int_a^b f - \sum_j w_j f(x_j) \right| \leq \frac{\|f^{(N+1)}\|_{L^\infty([a,b])}}{(N+1)!} \int_a^b |\ell_{\tilde X}(x)| \mathrm{d}x \] for a suitable set of nodes \(\tilde X \supseteq X\) with \(N+1\) elements,
  • Composite rules (apply the rule on each of \(K\) subintervals of length \(h = \frac{b-a}{K}\)) have errors: rectangular \(O(h)\), midpoint and trapezoid \(O(h^2)\), Simpson \(O(h^4)\).

12.2 Gaussian quadrature

Exercise 12.1

By considering the change of variables \(y = \frac{2}{b-a} \left( x - \frac{a + b}{2} \right)\) and defining \(\tilde{f}(y) = \frac{b-a}{2} f\big( \tfrac{b-a}2 y + \tfrac{a+b}2 \big)\), show that

\[ \begin{align} \int_a^b f(x) \mathrm{d}x = \int_{-1}^1 \tilde{f}(y) \mathrm{d}y. \end{align} \]

Moreover, show that, if \(p\) is the polynomial interpolation of \(f\) on \(X \subset [a,b]\), then \(\tilde{p}\) is the polynomial interpolation of \(\tilde{f}\) on \(\tilde{X} \subset [-1,1]\) where \(\tilde{X} = \{ \frac{2}{b-a} \left( x - \frac{a + b}{2} \right) : x \in X \}\). As a result, we may without loss of generality consider the interval \([-1,1]\).

Before: Fix \(X\) (equispaced gives Newton-Cotes rules) and find \(\{w_j\}\) such that the quadrature rule

\[ \begin{align} \int_{-1}^{+1} f(x)\mathrm{d}x \approx \sum_{j=0}^n w_j f(x_j) \end{align} \]

is exact on \(\mathcal P_N\) for maximal \(N\).

Idea: Choose \(X\) and \(\{w_j\}\).

Exercise 12.2

Choose \(w_0, w_1\) and \(x_0, x_1\) such that

\[ \begin{align} \int_{-1}^1 f \approx w_0 f(x_0) + w_1 f(x_1) \end{align} \]

is exact for all polynomials of degree \(3 = 2n+1\).

Exercise 12.2 gives the quadrature rule

\[ \begin{align} \int_{-1}^1 f \approx f\big( -\tfrac{\sqrt3}{3} \big) + f\big( \tfrac{\sqrt3}{3} \big) \end{align} \]

which is exact for all polynomials of degree \(\leq 3\). This is Gauss (or Gaussian) quadrature with \(n=1\).

Exercise 12.3

For \(n=1\), use the fact that Gauss quadrature is exact for all polynomials of degree \(2n+1 = 2(1) + 1 = 3\) to show that \[ \begin{align} \left| \int_{-1}^1 f(x) \mathrm{d}x - \left( f(-\tfrac{\sqrt3}{3}) + f(\tfrac{\sqrt3}{3}) \right) \right| % \leq \frac{1}{135} \max_{x\in [-1,1]} \big| f^{(4)}(x) \big|. \end{align} \]

Exercise 12.4

Approximate \(\log 2 = \int_1^2 \frac{1}{x} \mathrm{d}x\) using Gauss quadrature and evaluate an upper bound for the error.

Exercise 12.5

Compare this to the rectangular rule and its composite rule with 2 sub-intervals, and the trapezoid and Simpson rules.

Question: How to generalise this?

Answer: It turns out that one can choose the interpolation nodes and weights so that the quadrature rule

\[ \begin{align} \int_{-1}^1 f(x) \mathrm{d}x \approx \sum_{j=0}^n w_j f(x_j) \end{align} \]

is exact for all polynomials of degree \(2n+1\). The solution in the general case involves a set of orthogonal polynomials:

12.3 Legendre polynomials

In the following, we will write \[ (f,g)_{L^2} := (f,g)_{L^2([-1,1])} := \int_{-1}^{+1} f(x) g(x) \mathrm{d}x \] for the \(L^2\) inner product and \(\|f\|_{L^2}\) for the corresponding norm: \[ \begin{align} \|f\|_{L^2} := \left( \int_{-1}^1 |f(x)|^2 \mathrm{d}x \right)^{1/2}. \end{align} \]

(This generalises the same definitions for vectors \(x, y \in \mathbb R^d\) with the Euclidean inner product (or dot product) defined by \((x,y) := x \cdot y = x^\top y = \sum_i x_i y_i\) and Euclidean norm \(|x| = \sqrt{\sum_{i} |x_i|^2}\)).

Definition 12.1 We say that \(f\) is orthogonal to \(g\) with respect to the inner product \((\,\cdot\,,\,\cdot\,)\) if \((f,g)_{L^2} = 0\).

Definition 12.2 Let the Legendre polynomial \(P_n\) be the monic polynomial of degree \(n\) such that \((P_n, q)_{L^2([-1,1])} = 0\) for all \(q \in \mathcal P_{n-1}\).

Equivalently, \(\{P_n\}\) is defined as the sequence of monic polynomials with \((P_n, P_m)_{L^2} = 0\) for all \(n \not= m\) (see Exercise 12.7).

It may not be immediately clear that such polynomials exist, but we can compute the first few explicitly:

Exercise 12.6

Show that \(P_0(x) = 1\), \(P_1(x) = x\), and \(P_2(x) = x^2 - \frac13\). What are the roots of \(P_2\) and compare to the nodes in Exercise 12.2.

Theorem 12.1 The Legendre polynomials exist for all \(n\).

Proof. Let \(p_0 = 1\) and \(p_1 = x\).

If \(p_0, p_1, \dots, p_n\) have been defined, we define

\[ \begin{align} a_n &:= \frac{ \int_{-1}^1 x p_{n}(x)^2 \mathrm{d}x }{\|p_{n}\|_{L^2}^2} \qquad \text{and} \nonumber \\ b_{n} &:= \frac{ \int_{-1}^1 x p_{n}(x) p_{n-1}(x) \mathrm{d}x }{\|p_{n-1}\|_{L^2}^2} \nonumber \end{align} \]

and thus \(p_{n+1}(x) := (x - a_n) p_n(x) - b_n p_{n-1}(x)\) for all \(n \geq 1\).

We claim that \(p_n(x)\) is the \(n^\text{th}\) Legendre polynomial. We prove this by induction: the statement is true for \(n=0\) and \(n=1\). Suppose the statement is true for \(p_0, p_1, \dots, p_n\).

Then, notice that \(p_{n+1}(x)\) is a monic polynomial of degree \(n+1\).

Moreover, we have: for all \(j < n-1\),

\[ \begin{align} \int_{-1}^1 p_{n+1}(x) p_{j}(x) \mathrm{d}x % &= \int_{-1}^1 \left[ (x - a_n) p_{n}(x) - b_n p_{n-1}(x) \right] p_j(x) \mathrm{d}x \nonumber \\ % &= \int_{-1}^1 p_{n}(x) \left[ (x - a_n) p_j(x) \right] \mathrm{d}x - b_n \int_{-1}^1 p_{n-1}(x) p_j(x) \mathrm{d}x \nonumber \\ % &= 0. \nonumber \end{align} \]

Here, we have used the fact that \((x-a_n) p_j\) is a polynomial of degree \(j+1 < n\), \(p_j\) is a polynomial of degree \(j < n-1\), and \(p_n\), \(p_{n-1}\) are the \(n^\text{th}\) and \((n-1)^\text{th}\) Legendre polynomials.

Moreover, we have

\[ \begin{align} \int_{-1}^1 p_{n+1}(x) p_{n-1}(x) \mathrm{d}x % &= \int_{-1}^1 \left[ (x - a_n) p_{n}(x) - b_n p_{n-1}(x) \right] p_{n-1}(x) \mathrm{d}x \nonumber \\ % &= \int_{-1}^1 x p_{n}(x) p_{n-1}(x) \mathrm{d}x - b_n \int_{-1}^1 | p_{n-1}(x) |^2 \mathrm{d}x \nonumber \\ % &= 0 \nonumber \end{align} \]

and

\[ \begin{align} \int_{-1}^1 p_{n+1}(x) p_{n}(x) \mathrm{d}x % &= \int_{-1}^1 \left[ (x - a_n) p_{n}(x) - b_n p_{n-1}(x) \right] p_{n}(x) \mathrm{d}x \nonumber \\ % &= \int_{-1}^1 (x - a_n) p_{n}(x)^2 \mathrm{d}x \nonumber \\ % &= 0. \nonumber \end{align} \]

As a result, \(p_{n+1}\) is monic and orthogonal to all polynomials of degree \(\leq n\).

Remark 12.1. The roots of \(P_2\) are \(\{ -\tfrac{\sqrt3}{3}, \tfrac{\sqrt3}{3} \}\) (which are the nodes used in the quadrature rule of Exercise 12.2).

Recall \(P_n\) are the Legendre polynomials and define the set of Legendre points \(X\) by

\[ \begin{align} X := \{ \text{zeros of } P_{n+1} \}. \end{align} \tag{12.1}\]

Lemma 12.1 \(X\) given by (12.1) is a set of \(n+1\) distinct points in \([-1,1]\).

Proof. Notice that \(P_{n+1}(x) = \prod_{j=0}^n(x-x_j)\) (i.e. the monic polynomial with roots \(X\)). Here, we assume that the roots \(x_j\) are real (see Exercise 12.10 for an argument which also shows this).

Suppose that \(x_0 = x_1\) and define \(q(x) := \prod_{j=2}^n (x - x_j)\). Then, since \(P_{n+1}\) is orthogonal to \(q\) (as \(q\) is of degree \(n-1\)), we have

\[ \begin{align} 0 &= \int_{-1}^1 P_{n+1}(x) q(x) \mathrm{d}x \nonumber \\ % &= \int_{-1}^1 \left(\prod_{j=0}^n (x - x_j) \right) \left( \prod_{j=2}^n (x - x_j) \right) \mathrm{d}x \nonumber \\ % &= \int_{-1}^1 (x - x_0)^2 \prod_{j=2}^n (x - x_j)^2 \mathrm{d}x >0, \nonumber \end{align} \]

a contradiction (the integral of a non-negative polynomial, which is not identically zero, over an interval of positive length is positive). By relabeling the indices in \(X = \{x_0,\dots,x_n\}\), we see that \(X\) is a set of pairwise distinct nodes.

Suppose \(x_k \not\in [-1,1]\). Then, on defining \(q(x) := \prod_{\substack{0\leq j\leq n : \\ j \not= k }} (x - x_j)\), a polynomial of degree \(n\), we have

\[ \begin{align} 0 &= \int_{-1}^1 P_{n+1}(x) q(x) \mathrm{d}x \nonumber \\ % &= \int_{-1}^1 \left(\prod_{j=0}^n (x - x_j) \right) \left( \prod_{j\not=k} (x - x_j) \right) \mathrm{d}x \nonumber \\ % &= \int_{-1}^1 (x - x_k) \prod_{j\not=k} (x - x_j)^2 \mathrm{d}x. \nonumber \end{align} \]

Now, since \(x_k \not\in [-1,1]\), we have \((x - x_k)\) is either strictly positive or strictly negative for \(x \in [-1,1]\). Therefore, \(\int_{-1}^1 (x - x_k) \prod_{j\not=k} (x - x_j)^2\mathrm{d}x\) is either strictly positive or strictly negative, a contradiction. As a result, each \(x_k\) is in the interval \([-1,1]\).

Definition 12.3 (Gaussian quadrature) Let \(p\) be the polynomial interpolation of \(f\) on the \(n+1\) Legendre points. Then, we may define the quadrature rule

\[ \begin{align} \int_{-1}^1 f(x) \mathrm{d}x \approx \int_{-1}^{1} p(x) \mathrm{d}x = \sum_{j=0}^n w_j f(x_j) \end{align} \]

where \(w_j = \int_{-1}^1 \ell_j(x) \mathrm{d}x\) and \(\ell_j\) are the Lagrange polynomials for the interpolation set (12.1).

Theorem 12.2 Gaussian quadrature is exact for polynomials of degree \(2n+1\).

Proof. First, note that we have already seen that Gaussian quadrature is exact for all polynomials of degree \(n\).

Let \(X = \{x_0,\dots,x_n\}\) be the zeros of \(P_{n+1}\), \(w_j\) the corresponding quadrature weights, and let \(P\) be a polynomial of degree \(2n+1\). On dividing \(P\) by \(P_{n+1}\), there exist \(q_n, r_n \in \mathcal P_n\) such that

\[ \begin{align} P(x) = P_{n+1}(x) q_n(x) + r_n(x). \end{align} \]

Notice that \(P(x_j) = P_{n+1}(x_j) q_n(x_j) + r_n(x_j) = r_n(x_j)\) so that \(r_n\) is a degree \(n\) polynomial interpolant of \(P\) on \(X\). Using this, combined with the fact that \(P_{n+1}\) is orthogonal to \(q_n\), we have

\[ \begin{align} \int_{-1}^1 P &= \int_{-1}^{1} \left[ P_{n+1}(x) q_n(x) + r_n(x) \right] \mathrm{d}x \nonumber \\ &= 0 + \int_{-1}^{1} r_n(x) \mathrm{d}x \nonumber \\ % &= \sum_{j=0}^n w_j r_n(x_j) \nonumber \\ % &= \sum_{j=0}^n w_j P(x_j). \nonumber \end{align} \]

Therefore, Gaussian quadrature is exact for all polynomials of degree \(\leq 2n+1\).

Lemma 12.2 The Gaussian quadrature weights satisfy

  • \(\sum_{j=0}^n w_j = 2\),
  • \(w_j > 0\) for all \(j\).

Proof. Notice that \(\sum_{k=0}^n \ell_k(x)\) is a polynomial of degree \(n\) which is equal to \(1\) on the set of \(n+1\) points \(X\) and so \(\sum_{k=0}^n \ell_k(x)= 1\) for all \(x\). Therefore, because the quadrature rule is exact for polynomials of degree \(2n+1\), we have

\[ \begin{align} 2 &= \int_{-1}^1 \mathrm{d}x \nonumber \\ &= \int_{-1}^1 \sum_{k=0}^n \ell_k(x) \mathrm{d}x \nonumber \\ &= \sum_{j=0}^n w_j \left[ \sum_{k=0}^n \ell_k(x_j) \right] \nonumber \\ &= \sum_{j=0}^n w_j. \nonumber \end{align} \]

Moreover, \(\ell_k(x)^2\) is a polynomial of degree \(2n\) and the quadrature rule is exact for all polynomials of degree \(2n +1\), so we have

\[ \begin{align} 0 &< \int_{-1}^1 \ell_k(x)^2 \mathrm{d}x \nonumber \\ &= \sum_j w_j \left[ \ell_k(x_j)^2 \right] \nonumber \\ &= w_k. \nonumber \end{align} \]

Therefore, the weights are positive and sum to \(2 = \int_{-1}^1 1 \mathrm{d}x\).

We may use a recursion of this form to compute the Legendre polynomials (the code below uses the standard normalisation \(P_n(1) = 1\) rather than monic polynomials, which does not change the roots): here we plot \(P_n\) for \(n \leq 8\) and the set of Legendre points for \(n = 8\).

Next we approximate the integral

\[ \begin{align} \int_{-1}^{+1} \frac{1}{1 + e^{10(x - 0.5)}} \mathrm{d}x \end{align} \]

by using (i) the composite trapezoid rule, (ii) the composite Simpson rule (from last time) and (iii) Gaussian quadrature.

What rates of convergence do you expect to see?

Code
a, b = -1, 1
f = x -> 1/( 1 + exp( 10*(x-.5) ))
df_max = 5/2
d2f_max = 50/(3*sqrt(3))
d3f_max = 125/3

exact = (5 + log( 1 - exp(5) + exp(10) ) ) / 10

plot(f, a, b, label=L"f(x) = (1 + e^{10x})^{-1}")
Code
N = 30
T = []  # Composite trapezoid rule 
S = []  # Simpson
G = []  # Gaussian rule

for n ∈ 1:N
    h = (b-a)/n
    x = [a + j*(b-a)/n for j in 0:n]
    push!( T, sum( [ (1/2)*( x[j+1] - x[j] ) * ( f(x[j]) + f(x[j+1]) ) for j in 1:n] ) )
      
    h = (b-a)/(2*n)
    y = [a + j*(b-a)/(2*n) for j in 0:(2*n)]
    
    p = 0
    for j in 0:2*n
        if j==0
            p = p + f( y[j+1] )
        elseif j==2*n 
            p = p + f( y[j+1] )
        elseif mod(j,2)==0
            p = p + 2*f( y[j+1] )
        else
            p = p + 4*f( y[j+1] )
        end
    end
    push!( S, h*p/3 )

    r = roots( Legendre(n) )
    p = fit( r, f.(r) )
    I = integrate( p )
    push!( G, I(1) - I(-1) )

end

scatter( abs.(T .- exact ), xlabel=L"n", xaxis=:log, yaxis=:log, markersize=5, label="Composite Trapezoid" )
scatter!( abs.(S .- exact), markersize=5, label="Composite Simpson")
scatter!( abs.(G .- exact ), markersize=5, label="Gaussian quadrature" )

#plot!( (1/12)*d2f_max*(b-a)^3*(1:N).^(-2), linestyle=:dash, label="error bound (Trapezoid)" )
plot!( (1/25)*(1:N).^(-2), linestyle=:dash, label=L"O(n^{-2})" ) 
#plot!( (1/(27))*d3f_max*(b-a)^4*(1:N).^(-3), linestyle=:dash, color=:red, label="error bound (Simpson)" )
plot!( (1/15)*(1:N).^(-4), linestyle=:dash, label=L"O(n^{-4})" )
# plot!( (1/2880)*d4f_max*(b-a)^5*(1:N).^(-4), linestyle=:dash, label="n^{-4}" )
Code
scatter( abs.(G .- exact ), yaxis=:log, markersize=5, label="Gaussian quadrature" )
plot!( exp.(-(1:N)* 3/4 ), linestyle=:dash, label=L"\exp( - μ N )" )

One way to see exponential convergence comes from the following error estimate. Let \((w_j, x_j)_{j=0}^{n+1}\) be the weights and nodes from Gaussian quadrature and let \(I_Xf\) be the polynomial interpolation of \(f\) on \(X = \{x_j\}_{j=0}^n\). Then, because \(\int_{-1}^{+1} P_{2n+1} = \sum_{j=0}^n w_j P_{2n+1}\) for all polynomials \(P_{2n+1}\) of degree \(2n+1\), we have \[ \begin{align} &\Big|\int_{-1}^{+1} f(x) \mathrm{d}x - \sum_{j=0}^n w_j f(x_j)\Big| \nonumber\\ % &\leq \Big|\int_{-1}^{+1} \left( f(x) - P_{2n+1}(x) \right) \mathrm{d}x\Big| % + \Big|\sum_{j=0}^n w_j \left( f(x_j) - P_{2n+1}(x_j) \right) \Big| \nonumber\\ % &\leq 4 \max_{x\in [-1,1]}| f(x) - P_{2n+1}(x) | =: 4 \|f-P_{2n+1}\|_{L^\infty}. \end{align} \] Here, we have used the fact that \(|\int_{-1}^{+1} g| \leq 2 \|g\|_{L^\infty}\) and \(|\sum_{j=0}^n w_j g(x_j)| \leq \sum_{j=0}^n |w_j| |g(x_j)| \leq 2 \|g\|_{L^\infty}\) (because the weights are positive and sum to \(2\)). We obtain the error estimates as seen in the plot above by showing that polynomial approximations to \(f(x) = (1 + e^{10(x-\tfrac12)})^{-1}\) on \([-1,1]\) converge exponentially as \(n\to\infty\); a possible presentation topic. (We may also return to this in Chapter 6: Approximation theory, time permitting).

12.4 How to compute the roots of \(P_{n+1}\)?

Recall, \(P_0(x) = 1, P_1(x) = x\), and

\[ \begin{align} xP_n(x) = a_n P_n(x) + P_{n+1}(x) + b_n P_{n-1}(x). \end{align} \]

It turns out that \(X = \{ \text{zeros of } P_{n+1} \}\) is the set of eigenvalues of a tridiagonal matrix \(T\) (see: Assignment 7). Next chapter, we will see methods for finding the eigenvalues of this matrix!

12.5 Next time….

  • Chapter 5: Linear systems of equations,
  • Motivation: where linear systems come from (polynomial interpolation, differential equations),
  • Cramer’s rule, Gaussian elimination and the LU decomposition,
  • Reading: Section 6.1 of Burden et al. (2016).
TipTO-DO

12.6 Extra exercises

Exercise 12.7

Show that the two definitions in Definition 12.2 are equivalent.

Exercise 12.8

Show that Gauss quadrature with nodes \(X = \{x_0, \dots, x_n\}\) is never exact for all polynomials of degree \(N > 2n+1\).

Exercise 12.9

  1. Write down the Gaussian quadrature rule with \(n=2\).
  2. What is the absolute error in approximating \(\log 2\) using the \(n=2\) scheme and following the same arguments as in class (see Exercise 12.4)?
  3. How would you prove an error bound for the \(n=2\) rule in general?

Exercise 12.10

Show that the roots of \(P_{n+1}\) are real, distinct, and lie in the open interval \((-1,1)\) (compare with the proof of Lemma 12.1).

Timer
00:00
min