Skip to main content

10  Newton–Cotes quadrature

Lecture 10: 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.

10.1 Previously…..

  • Hermite interpolation: given distinct nodes \(z_0,\dots,z_k\) with multiplicities \(m_0,\dots,m_k\) (forming a multiset \(X = \{\{x_0,\dots,x_n\}\}\)), find \(p \in \mathcal P_n\) with \(p^{(l)}(z_j) = f^{(l)}(z_j)\) for \(l = 0,\dots,m_j-1\),
  • There exists a unique solution (Theorem 9.1); Taylor expansion (one node repeated \(n+1\) times) and Lagrange interpolation (all \(m_j = 1\)) are the two extremes,
  • Divided differences with repeated nodes: \(f[x_0,\dots,x_n] = \frac{f^{(n)}(x_0)}{n!}\) if all nodes are equal, otherwise the usual recursion (Theorem 9.2),
  • Newton form \(p = \sum_{k=0}^n f[x_0,\dots,x_k] \, \pi_k\) (Theorem 9.3) computed from the divided difference table,
  • Error estimate: \(f(x) - p(x) = \frac{f^{(n+1)}(\xi_x)}{(n+1)!} \ell_X(x)\) where \(\ell_X(x) = (x-x_0)\cdots(x-x_n)\) (Exercise 9.6).

Exercise. Exercise 9.7

Exercise 10.1 (Recap of last week)

  1. Compute the divided difference table for \(f(x) = x^2\) on \(\{0, 1, 2\}\) and write down the Newton form of the interpolant. What do you notice?
  2. What are \(f[x_0, x_0]\) and \(f[x_0, x_0, x_0]\)?
  3. Why do we prefer the barycentric formula (8.3) to the naive Lagrange formula?
  4. What is the Hermite interpolant of \(f\) on \(\{\{a, a\}\}\)? How many conditions does the Hermite interpolant on \(\{\{0, 0, 1\}\}\) satisfy, and what is its degree?
  5. The next iterate of Newton’s method is the root of which interpolant of \(f\)? What about the secant method?

10.2 Quadrature rules

Exercise 10.2 (Why approximate integrals?)

Can you compute \(\int_0^1 e^{-x^2} \,\mathrm{d}x\) or \(\int_0^1 \frac{\sin x}{x} \,\mathrm{d}x\) exactly?

Definition 10.1 We wish to consider quadrature rules of the following form:

\[ \begin{align} \int_{a}^b f &\approx \sum_j w_j f(x_j). \end{align} \tag{10.1}\]

We call \(\{w_j\}\) the quadrature weights, \(\{x_j\}\) the quadrature nodes and \((\{w_j\}, \{x_j\})\) or \(\sum_{j} w_j f(x_j)\) a quadrature rule.

Suppose \(p = I_X f\) is the polynomial of degree at most \(n\) interpolating \(f\) on \(X = \{x_0,\dots,x_n \}\): that is \(p(x) = \sum_{j=0}^n \ell_j(x) f(x_j)\). We simply approximate the integral of \(f\) by instead integrating \(p\):

\[ \begin{align} \int_{a}^b f &\approx \int_a^b p \nonumber\\ % &= \sum_{j=0}^n \Big[\int_a^b\ell_j(x)\mathrm{d}x \Big] f(x_j) \end{align} \tag{10.2}\]

which gives a quadrature rule with weights \(w_j := \int_a^b\ell_j(x)\mathrm{d}x\) and nodes \(x_j\).

Definition 10.2 When the interpolation nodes \(x_0, \dots, x_n\) are equispaced, (10.2) is a Newton–Cotes quadrature rule.

Example 10.1 In the following, we will consider the example of integrating \(f(x) = \tfrac1{2\pi} ( x + x \sin x)\) between \(0\) and \(2\pi\). In this particular case, the integral is \(\pi-1\). We use this example so that we can analyse the error committed in the methods we will introduce.

Exercise 10.3

Show that \(\int_0^{2\pi} \frac{1}{2\pi}(x + x \sin x) \,\mathrm{d}x = \pi - 1\).

Code
a, b = 0, 2π
f = x -> x * ( 1 + sin(x) ) /( 2π )
df_max = 1 + 1/(2π)

# maximum of |f''|, |f'''|, |f''''| on [a,b] (computed on a fine grid)
d2f = x -> ( 2cos(x) - x * sin(x) ) /( 2π )
d3f = x -> ( -3sin(x) - x * cos(x) ) /( 2π )
d4f = x -> ( -4cos(x) + x * sin(x) ) /( 2π )
X = range(a, b, 10^5)
d2f_max = maximum( abs.( d2f.(X) ) )
d3f_max = maximum( abs.( d3f.(X) ) )
d4f_max = maximum( abs.( d4f.(X) ) )
plot( f, a, b, title=L"f(x) = \frac{1}{2\pi} (x + x\sin x ) ", lw = 3, legend=false)

10.3 Rectangular rule

The simplest rule of this kind is taking \(X = \{a\}\) and considering the polynomial interpolation of degree \(0\): \(p_0(x) = f(a)\),

\[ \begin{align} \int_a^b f(x)\mathrm{d}x &\approx \int_a^b p_0(x)\mathrm{d}x \nonumber\\ &= (b-a) f(a). \end{align} \tag{10.3}\]

The rule has weights \(\{ b-a \}\) and nodes \(\{ a \}\). Notice that this rule is exact if \(f\) is a polynomial of degree \(0\).

Exercise 10.4 (Interpolation error with one node)

Write down the interpolation error formula for \(f - p\) when \(p\) interpolates \(f\) on \(n+1\) nodes (Theorem 6.2). What does it say when \(n = 0\) and \(X = \{a\}\)?

Exercise 10.5 (Error estimate)

Suppose \(f \in C^1([a,b])\). Show that

\[ \begin{align} \left| \int_{a}^b f - (b-a) f(a) \right| \leq \| f'\|_{L^\infty([a,b])} \frac{(b-a)^2}{2}. \end{align} \]

Composite rule. This error bound is only useful if \([a,b]\) is a small interval (or \(f'\) is small). It therefore makes sense to split the interval \([a,b]\) into \(n\) subintervals and consider the rectangular rule on each one: consider \(x_j = a + j\frac{b-a}{n}\) for \(j = 0,\dots,n\) and write

\[ \begin{align} \int_a^b f &= \sum_{j=0}^{n-1} \int_{x_j}^{x_{j+1}} f \nonumber\\ % &\approx \sum_{j=0}^{n-1} (x_{j+1} - x_j) f(x_j). \end{align} \]

This is a quadrature rule with weights \(w_j = x_{j+1} - x_j = \frac{b-a}{n}\) and nodes \(x_j\) for \(j = 0,\dots,n-1\).

Exercise 10.6

Approximate \(\int_0^1 x \,\mathrm{d}x\) by the composite rectangular rule with \(n\) subintervals of equal length. What is the error?

Theorem 10.1 Suppose \(f \in C^1([a,b])\). Then the composite rectangular rule satisfies

\[ \begin{align} \left| \int_a^b f - \sum_{j=0}^{n-1} \frac{b-a}{n} f(x_j) \right| &\leq \frac{\| f'\|_{L^\infty([a,b])}}{2} \frac{ (b-a)^2 }{ n }. \end{align} \]

Proof. Using Exercise 10.5 on each subinterval \([x_j, x_{j+1}]\) and summing over \(j\), we obtain

\[ \begin{align} \left| \int_a^b f - \sum_{j=0}^{n-1} \frac{b-a}{n} f(x_j) \right| &\leq \| f'\|_{L^\infty([a,b])} \sum_{j=0}^{n-1} \frac12 \Big(\frac{b-a}{n}\Big)^2 \nonumber\\ % &= \frac{\| f'\|_{L^\infty([a,b])}}{2} \frac{ (b-a)^2 }{ n }. \nonumber \end{align} \]

Remark 10.1. Notice that this quadrature rule is exact if \(f\) is a polynomial of degree \(0\).

Numerics. We now test this numerically:

Code
N = 50
R = []

anim = @animate for n ∈ 1:N
    
    h = (b-a)/n
    x = [a + j*(b-a)/n for j in 0:n]
    
    push!( R, sum( [ ( x[j+1] - x[j] ) * f( x[j] ) for j in 1:n] ) )
    
    plot( f, a, b, label=L"x( 1 + \sin(x) )/2π", lw = 3)

    for m ∈ 1:n 
        plot!([x[m], x[m+1]], [f(x[m]), f(x[m])], lw=3, color="red", primary=false)
        plot!([x[m], x[m]], [0, f(x[m])], lw=3, color="red", primary=false)
        plot!([x[m+1], x[m+1]], [0, f(x[m])], lw=3, color="red", primary=false)
    end

end

gif(anim, "../images/Rectangular.gif", fps = 1, show_msg = false)
Code
scatter( abs.(R .- (π -1)), xaxis=:log, yaxis=:log, markersize=5, label="error", title="Error estimate" )
plot!( (1/2)*df_max*(b-a)^2*(1:N).^(-1), linestyle=:dash,label="error bound" )

plot!( 4*(1:N).^(-1), linestyle=:dash, label=L"4n^{-1}" )

We can see here that the error decays with rate \(O(n^{-1})\) and the error bound (proved above) is in fact an upper bound!

10.4 Trapezoid rule

The next simplest rule of this kind is taking \(X = \{a, b\}\) and considering the polynomial interpolation \(p_1\) of degree \(1\):

\[ \begin{align} \int_a^b f(x) \mathrm{d}x % &\approx \int_a^b p_1(x) \mathrm{d}x \nonumber\\ % &= \frac{b-a}{2} \big( f(a) + f(b) \big). \end{align} \]

When \(f(a), f(b) \geq 0\), this is the area of the trapezium with vertices \((a,0), (a,f(a)), (b, f(b)), (b,0)\): width \(b-a\) times the average height \(\frac{f(a) + f(b)}{2}\).

This rule has weights \(\{ \frac{b-a}{2}, \frac{b-a}{2} \}\) and nodes \(\{ a, b \}\). Notice that this rule is exact if \(f\) is a polynomial of degree \(1\) (because \(f\) agrees with \(p_1\) in this case).

Exercise 10.7

  1. Write down the polynomial \(p_1\) of degree at most \(1\) interpolating \(f\) on \(X = \{a, b\}\).
  2. Show that \(\int_a^b p_1(x) \,\mathrm{d}x = \frac{b-a}{2} \big( f(a) + f(b) \big)\).

Exercise 10.8

What is the sign of \((x-a)(x-b)\) on \([a,b]\)? Compute \(\int_a^b |(x-a)(x-b)| \,\mathrm{d}x\).

Theorem 10.2 (Error estimate) Suppose \(f \in C^2([a,b])\). Then

\[ \begin{align} \left| \int_{a}^b f - \frac{b-a}{2} \big( f(a) + f(b) \big) \right| \leq \frac{\| f''\|_{L^\infty([a,b])}}{12} (b-a)^3. \end{align} \]

Proof. Since \(p_1\) is a degree one interpolating polynomial, we may apply the error bound we had for interpolation (Theorem 6.2):

\[ \begin{align} f(x) - p_1(x) = \frac{f''(\xi_x)}{2} (x - a)(x-b) \end{align} \]

for some \(\xi_x \in [a, b]\). Therefore, integrating this over \([a,b]\), we have the bound

\[ \begin{align} \left| \int_{a}^b \big[ f(x) - p_1(x) \big] \mathrm{d}x \right| % &\leq \frac{\| f''\|_{L^\infty([a,b])}}{2} \int_a^b |(x - a)(x-b)| \mathrm{d}x \nonumber\\ &= \frac{\| f''\|_{L^\infty([a,b])}}{2} \frac{(b-a)^3}{6} \nonumber \end{align} \]

(see Exercise 10.8 for the last integral).

Composite rule. This error bound is only useful if \([a,b]\) is a small interval (or \(f''\) is small). It therefore makes sense to split the interval \([a,b]\) into \(n\) subintervals and consider the trapezium rule on each one: consider \(x_j = a + j\frac{b-a}{n}\) for \(j = 0,\dots,n\) and write

\[ \begin{align} \int_a^b f(x) \mathrm{d}x &= \sum_{j=0}^{n-1} \int_{x_j}^{x_{j+1}} f(x)\mathrm{d}x \nonumber\\ % &\approx \sum_{j=0}^{n-1} \frac{x_{j+1} - x_j}{2} \big[ f(x_j) + f(x_{j+1}) \big]. \end{align} \]

This is a quadrature rule with weights \(w_0 = w_n = \frac{x_{1} - x_0}{2} = \frac{b-a}{2n}\) and \(w_j = \frac{b-a}{n}\) for \(j=1,\dots,n-1\).

Theorem 10.3 Suppose \(f \in C^2([a,b])\). Then the composite trapezoid rule satisfies

\[ \begin{align} \left| \int_a^b f - \frac{b-a}{n} {\sum_{0 \leq j \leq n}}^\prime f(x_j) \right| &\leq \frac{\| f''\|_{L^\infty([a,b])}}{12} \frac{ (b-a)^3 }{ n^2 } \end{align} \]

where \(\sum^\prime\) indicates that the first and last terms must be multiplied by \(\frac12\).

Proof. Using Theorem 10.2 on each subinterval and summing over \(j\), we obtain

\[ \begin{align} \left| \int_a^b f - \frac{b-a}{n} {\sum_{0 \leq j \leq n}}^\prime f(x_j) \right| &\leq \| f''\|_{L^\infty([a,b])} \sum_{j=0}^{n-1} \frac1{12} \left(\frac{b-a}{n}\right)^3 \nonumber\\ % &= \frac{\| f''\|_{L^\infty([a,b])}}{12} \frac{ (b-a)^3 }{ n^2 }. \nonumber \end{align} \]

Remark 10.2. Notice that this quadrature rule is exact if \(f\) is a polynomial of degree \(1\).

Numerics. We now test this numerically:

Code
N = 30
T = []

anim = @animate 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] ) )
    
    plot( f, a, b, label=L"x( 1 + \sin(x) )/2π", lw = 3)

    for m ∈ 1:n 
        plot!([x[m], x[m+1]], [f(x[m]), f(x[m+1])], lw=3, color="red", primary=false)
        plot!([x[m], x[m]], [0, f(x[m])], lw=3, color="red", primary=false)
        plot!([x[m+1], x[m+1]], [0, f(x[m+1])], lw=3, color="red", primary=false)
    end

end

gif(anim, "../images/Trapezium.gif", fps = 1, show_msg = false)
Code
scatter( abs.(T .- (π -1)), xaxis=:log, yaxis=:log, markersize=5, label="error" )
plot!( (1/12)*d2f_max*(b-a)^3*(1:N).^(-2), linestyle=:dash, label="error bound" )
plot!( 5*(1:N).^(-2), linestyle=:dash, label=L"5n^{-2}" )  

We can see here that the error decays with rate \(O(n^{-2})\) and the error bound (proved above) is in fact an upper bound!

10.5 Summary

  1. Rectangular rule is given by \((b-a) f(a)\) and leads to a composite rule with error \(C \|f'\|_{L^\infty([a,b])} n^{-1}\) (Theorem 10.1),
  2. Trapezium rule is given by \(\frac{b-a}{2} \left( f(a) + f(b) \right)\) and leads to a composite rule with error \(C \|f''\|_{L^\infty([a,b])} n^{-2}\) (Theorem 10.3).

10.6 Next time….

  • Simpson’s rule: integrating the quadratic interpolant on \(\{a, \tfrac{a+b}{2}, b\}\),
  • Simpson’s rule is exact for cubics, and an improved error estimate using Hermite interpolation.
TipTO-DO
  • Exercises throughout the notes,
  • (Sign-up to office hours if you like)

10.7 Extra exercises

Exercise 10.9 (Midpoint rule)

The midpoint rule is \(\int_a^b f \approx (b-a) f\big(\tfrac{a+b}{2}\big)\).

  1. Show that the midpoint rule is exact for all polynomials of degree at most \(1\).
  2. Suppose \(f \in C^2([a,b])\). Show that \[ \left| \int_a^b f - (b-a) f\big(\tfrac{a+b}{2}\big) \right| \leq \frac{\|f''\|_{L^\infty([a,b])}}{24} (b-a)^3. \]
  3. Write down the composite midpoint rule on \(n\) subintervals of equal length and show that its error is bounded by \(\frac{\|f''\|_{L^\infty([a,b])}}{24} \frac{(b-a)^3}{n^2}\). Compare with Theorem 10.3.

Exercise 10.10 (Properties of interpolatory weights)

Let \(x_0, \dots, x_n \in [a,b]\) be distinct and let \(w_j = \int_a^b \ell_j\) be the weights in (10.2).

  1. Show that \(\{w_j\}\) are the unique weights for which \(\sum_{j=0}^n w_j f(x_j)\) is exact for all \(f \in \mathcal P_n\).
  2. Show that \(\sum_{j=0}^n w_j = b - a\).
  3. Let \(\hat x_j \in [0,1]\) and let \(\hat w_j\) be the corresponding weights on \([0,1]\). Show that the nodes \(x_j = a + (b-a) \hat x_j\) on \([a,b]\) have weights \(w_j = (b-a) \hat w_j\).
  4. Suppose the nodes are symmetric: \(x_{n-j} = a + b - x_j\) for all \(j\). Show that \(w_{n-j} = w_j\).
Timer
00:00
min