Skip to main content

11  Simpson’s rule

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

11.1 Previously…..

  • Quadrature rules \(\int_a^b f \approx \sum_j w_j f(x_j)\) (Definition 10.1): integrating the interpolating polynomial gives the weights \(w_j = \int_a^b \ell_j\) ((10.2)), and equispaced nodes give the Newton–Cotes rules (Definition 10.2),
  • Error estimates: integrate the interpolation error (Theorem 6.2),
  • Rectangular rule \((b-a) f(a)\): exact for polynomials of degree \(0\); the composite rule has error at most \(\frac{\|f'\|_{L^\infty([a,b])}}{2} \frac{(b-a)^2}{n}\) (Theorem 10.1),
  • Trapezoid rule \(\frac{b-a}{2} \big( f(a) + f(b) \big)\): exact for polynomials of degree \(1\); the composite rule has error at most \(\frac{\|f''\|_{L^\infty([a,b])}}{12} \frac{(b-a)^3}{n^2}\) (Theorem 10.3).

Exercise. Exercise 10.10

We continue with the function from Example 10.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)

11.2 Simpson’s rule

Rectangular, and trapezoid rules were exact for polynomials of degree \(0\) and \(1\), respectively. Simpson’s rule is exact for degree \(2\) polynomials:

Exercise 11.1

Suppose you know \(f(0)\), \(f(\tfrac12)\) and \(f(1)\). Find weights \(\hat w_0, \hat w_1, \hat w_2\) so that

\[\begin{align} \int_0^1 f(x) \mathrm{d}x % &\approx \hat w_0 f(0) + \hat w_1 f(\tfrac{1}2) + \hat w_2 f(1) \end{align}\]

is exact for \(f = 1\), \(x\) and \(x^2\).

More generally, for \(X = \{a, \frac{a+b}{2}, b\}\), we let \(p_2\) be the quadratic interpolation of \(f\) on \(X\) and approximate

\[ \begin{align} \int_a^b f(x) \mathrm{d}x &\approx \int_a^b p_2(x) \mathrm{d}x \nonumber\\ % &= \frac{b-a}{6} \left[ f(a) + 4 f\big(\tfrac{a+b}{2}\big) + f(b) \right]. \end{align} \tag{11.1}\]

Here, you can write out the polynomial interpolation using the Lagrange polynomials and evaluate the weights \(w_j = \int_a^b \ell_j\) explicitly, or do the same in the Newton basis. Instead, notice that \(a, \frac{a+b}{2}, b\) are the images of the nodes \(0, \frac12, 1\) under \(t \mapsto a + (b-a) t\). By Exercise 11.1, the weights \(\hat w_0 = \hat w_2 = \frac16\) and \(\hat w_1 = \frac23\) give a rule on \([0,1]\) that is exact for all polynomials of degree at most \(2\), and so, by Exercise 10.10 (1), they are the weights \(\hat w_j = \int_0^1 \hat \ell_j\) of the interpolatory rule on \(\{0, \frac12, 1\}\). Therefore, by Exercise 10.10 (3), the weights on \([a,b]\) are \(w_j = (b-a) \hat w_j\), which gives (11.1).

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

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

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

\[ \begin{align} f(x) - p_2(x) = \frac{f'''(\xi_x)}{3!} (x - a)\big( x - \tfrac{a+b}{2} \big) (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_2(x) \big] \mathrm{d}x \right| % \nonumber\\ &\leq \frac{\| f'''\|_{L^\infty([a,b])}}{6} \int_a^b \left|(x - a)\big( x - \tfrac{a+b}{2} \big) (x-b)\right| \mathrm{d}x \nonumber\\ &= \frac{\| f'''\|_{L^\infty([a,b])}}{6} \frac{(b-a)^4}{32}. \nonumber \end{align} \]

For the last integral, make the substitution \(t := x - \frac{a+b}{2}\) and define \(h := b - a\), so that \((x-a)(x-b) = t^2 - \frac{h^2}{4} \leq 0\) for \(t \in [-\frac{h}{2}, \frac{h}{2}]\). Then, since the integrand is even,

\[ \begin{align} &\int_a^b \left|(x - a)\big( x - \tfrac{a+b}{2} \big) (x-b)\right| \mathrm{d}x % \nonumber\\ &= \int_{-h/2}^{h/2} |t| \big( \tfrac{h^2}{4} - t^2 \big) \mathrm{d}t \nonumber\\ % &= 2 \int_{0}^{h/2} \big( \tfrac{h^2}{4} t - t^3 \big) \mathrm{d}t \nonumber\\ % &= 2 \Big( \frac{h^4}{32} - \frac{h^4}{64} \Big) = \frac{(b-a)^4}{32}, \nonumber \end{align} \]

as required.

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 subintervals and consider the Simpson rule on each one. We will now consider \(n\) even and \(x_j = a + j\frac{b-a}{n}\) for \(j = 0,\dots,n\) and write

\[ \begin{align} \int_a^b f &= \sum_{j=0 : \text{ even}}^{n-2} \int_{x_j}^{x_{j+2}} f \nonumber\\ % &\approx \sum_{j=0 : \text{ even}}^{n-2} \frac{x_{j+2} - x_j}{6} \big[ f(x_j) + 4 f(x_{j+1}) + f(x_{j+2}) \big] \nonumber\\ % &= \frac{b-a}{3n} \left[ f(x_0) + 4 f(x_{1}) + 2 f(x_{2}) + 4 f(x_3) + 2 f(x_{4}) + \dots + 4 f(x_{n-1}) + f(x_n) \right] \end{align} \]

(we have changed notation here slightly so that we are interpolating \(f\) only at points in \(X = \{x_0, \dots, x_n\}\)). This is a quadrature rule with weights \(w_0 = w_n = \frac{x_{n} - x_{n-2}}{6} = \frac{b-a}{3 n}\), and \(w_{2j+1} = \frac{b-a}{3n} \cdot 4\) and \(w_{2j} = \frac{b-a}{3n} \cdot 2\) otherwise.

Theorem 11.2 Suppose \(f \in C^3([a,b])\) and \(n\) is even. Then the composite Simpson rule satisfies

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

Proof. Using Theorem 11.1 on each of the \(\frac n2\) subintervals \([x_j, x_{j+2}]\) (which have length \(2 \frac{b-a}{n}\)) and summing over \(j\), we obtain

\[ \begin{align} \left| \int_a^b f - \sum_{j = 0}^n w_j f(x_j) \right| &\leq \| f'''\|_{L^\infty([a,b])} \sum_{j=0 : \text{ even}}^{n-2} \frac1{192} \left(2 \cdot \frac{b-a}{n}\right)^4 \nonumber\\ % &= \frac{\| f'''\|_{L^\infty([a,b])}}{24} \frac{(b-a)^4}{n^3}. \nonumber \end{align} \]

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

Numerics. We now test this numerically:

Code
N = 15
S = []

anim = @animate for n ∈ 1:N
    
    h = (b-a)/(2*n)
    x = [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( x[j+1] )
        elseif j==2*n 
            p = p + f( x[j+1] )
        elseif mod(j,2)==0
            p = p + 2*f( x[j+1] )
        else
            p = p + 4*f( x[j+1] )
        end
    end
    push!( S, h*p/3 )
    
    plot( f, a, b, label=L"x( 1 + \sin(x) )/2π", lw = 3)

    for m ∈ 1:2:2n

        p = fit( [x[m], x[m+1], x[m+2]], [f(x[m]), f(x[m+1]), f(x[m+2])] )

        plot!( p, x[m], x[m+2], lw=3, color="red", primary=false)
        plot!([x[m], x[m]], [0, f(x[m])], lw=3, color="red", primary=false)
        plot!([x[m+2], x[m+2]], [0, f(x[m+2])], lw=3, color="red", primary=false)
    end

end

gif(anim, "../images/Simpson.gif", fps = 1, show_msg = false)
Code
scatter( abs.(S .- (π -1)), xaxis=:log, yaxis=:log, markersize=5, label="error")
plot!( (1/24)*d3f_max*(b-a)^4*(2*(1:N)).^(-3), linestyle=:dash, color=:red, label="error bound" )   # S[n] uses 2n subintervals
plot!( (1:N).^(-4), linestyle=:dash, label=L"n^{-4}" )

Therefore the error bound we have derived is an upper bound in this case! However, it seems that the error is \(O(n^{-4})\) not \(O(n^{-3})\) (which is what our error bound says). We will see now that we actually get an improved error bound.

11.3 Improved error estimate for Simpson’s rule

Recall that we are considering the (composite) Simpson rule:

\[ \begin{align} \int_a^b f &\approx \sum_{j=0 : \text{ even}}^{n-2} \frac{b-a}{3n} \big[ f(x_j) + 4 f(x_{j+1}) + f(x_{j+2}) \big] \end{align} \]

(with \(n\) even). We have seen that the quadrature rule is exact when \(f\) is a quadratic polynomial (degree 2). We will now see that it is also exact when \(f\) is a cubic polynomial (degree 3).

Exercise 11.2

  1. What is the quadratic interpolant of \(f(x) = x^2\) on \(\{-1, 0, 1\}\)?
  2. What is the cubic interpolant \(p\) of \(f(x) = x^3\) on \(\{-1, 0, 1\}\)? Compare \(\int_{-1}^1 f\) and \(\int_{-1}^1 p\).
  3. Why is \(\int_a^b \big(x - \tfrac{a+b}{2}\big)^3 \,\mathrm{d}x = 0\)?

Theorem 11.3 Simpson’s rule (11.1) (and therefore also the composite Simpson rule) is exact for all polynomials of degree at most \(3\).

Proof. We may write \(f(x) = \alpha \big( x - \tfrac{a+b}{2} \big)^3 + g(x)\) where \(g\) is a quadratic polynomial. Since \(t \mapsto t^3\) is an odd function, we have

\[ \begin{align} 0 &= \int_{a}^{b} \big( x - \tfrac{a+b}{2} \big)^3 \mathrm{d}x \nonumber\\ % &= \frac{b-a}{6} \Big[ \big( a - \tfrac{a+b}{2} \big)^3 + 4 \big( \tfrac{a+b}{2} - \tfrac{a+b}{2} \big)^3 + \big( b - \tfrac{a+b}{2} \big)^3 \Big] \end{align} \]

and thus

\[ \begin{align} \int_{a}^b f &= \int_{a}^b \Big[ \alpha \big( x - \tfrac{a+b}{2} \big)^3 + g(x) \Big] \mathrm{d}x \nonumber\\ % &= 0 + \frac{b-a}{6} \big[ g(a) + 4 g\big(\tfrac{a+b}{2}\big) + g(b) \big] \nonumber\\ % &= \frac{b-a}{6} \big[ f(a) + 4 f\big(\tfrac{a+b}{2}\big) + f(b) \big]. \end{align} \]

As a result, Simpson’s quadrature rule is exact for all cubic functions. The composite rule is the sum of Simpson’s rule on each subinterval and so this is also exact on polynomials of degree \(3\).

We now turn our attention to error estimates.

Theorem 11.4 Suppose \(f \in C^4([a,b])\). Then

\[ \begin{align} \left| \int_a^b f - \frac{b-a}{6} \big[ f(a) + 4 f\big(\tfrac{a+b}{2}\big) + f(b) \big] \right| \leq \frac{\| f'''' \|_{L^\infty([a,b])}}{2880} (b-a)^5 \end{align} \]

and, for \(n\) even, the composite Simpson rule satisfies

\[ \begin{align} \left| \int_a^b f - \frac{b-a}{3n} \sum_{j=0 : \text{ even}}^{n-2} \big[ f(x_j) + 4 f(x_{j+1}) + f(x_{j+2}) \big] \right| \leq \frac{\| f'''' \|_{L^\infty([a,b])}}{180} \frac{(b-a)^5}{n^4}. \end{align} \]

Proof. Let \(p\) be the (degree less than or equal to \(3\)) Hermite polynomial interpolation of \(f\) on \(\{\{ a, b, \tfrac{a+b}2, \tfrac{a+b}2 \}\}\). Since \(p = f\) at \(a, \tfrac{a+b}{2}, b\) and Simpson’s rule is exact for \(p\) (Theorem 11.3), using the error estimate for Hermite interpolation (Exercise 9.6), there exists \(\xi_x \in [a,b]\) such that

\[ \begin{align} &\left| \int_a^b f - \frac{b-a}{6} \big[ f(a) + 4 f\big(\tfrac{a+b}{2}\big) + f(b) \big] \right| % \nonumber\\ &= \left| \int_a^b f - \frac{b-a}{6} \big[ p(a) + 4 p\big(\tfrac{a+b}{2}\big) + p(b) \big] \right| \nonumber\\ % &= \left| \int_a^b \big( f - p \big) \right| \nonumber\\ % &= \left| \int_a^b \frac{f''''(\xi_x)}{4!} (x-a)\big( x - \tfrac{a+b}2 \big)^2 (x-b) \mathrm{d}x \right| \nonumber\\ % &\leq \frac{\| f'''' \|_{L^\infty([a,b])}}{4!} \left| \int_a^b (x-a)\big( x - \tfrac{a+b}2 \big)^2 (x-b) \mathrm{d}x \right| \nonumber\\ % &= \frac{\| f'''' \|_{L^\infty([a,b])}}{4!} \frac{(b-a)^5}{120}. \nonumber \end{align} \]

Here, we have used the fact that \(x \mapsto (x-a)\big( x - \tfrac{a+b}2 \big)^2 (x-b)\) doesn’t change sign on \([a,b]\). For the last integral, as before, we make the substitution \(t := x - \frac{a+b}{2}\) and define \(h := b - a\), so that

\[ \begin{align} &\int_a^b (x-a)\big( x - \tfrac{a+b}2 \big)^2 (x-b) \mathrm{d}x % \nonumber\\ &= \int_{-h/2}^{h/2} t^2 \big( t^2 - \tfrac{h^2}{4} \big) \mathrm{d}t \nonumber\\ % &= 2 \Big[ \frac{t^5}{5} - \frac{h^2 t^3}{12} \Big]_0^{h/2} \nonumber\\ % &= 2 \Big( \frac{h^5}{160} - \frac{h^5}{96} \Big) = -\frac{(b-a)^5}{120}. \nonumber \end{align} \]

Then, the error for the composite rule is bounded above as follows:

\[ \begin{align} &\left| \int_a^b f - \frac{b-a}{3n} \sum_{j=0 : \text{ even}}^{n-2} \big[ f(x_j) + 4 f(x_{j+1}) + f(x_{j+2}) \big] \right| \nonumber\\ % &\leq \sum_{j=0 : \text{ even}}^{n-2} \left| \int_{x_j}^{x_{j+2}} f - \frac{x_{j+2}-x_{j}}{6} \big[ f(x_j) + 4 f\big(x_{j+1}\big) + f(x_{j+2}) \big] \right| \nonumber\\ % &\leq \frac{\| f'''' \|_{L^\infty([a,b])}}{4!} \frac{n}{2} \frac{1}{120} \left(2\frac{b-a}{n}\right)^5 % = \frac{\| f'''' \|_{L^\infty([a,b])}}{180} \frac{(b-a)^5}{n^4}, \nonumber \end{align} \]

as required.

This gives us the correct rate:

Code
scatter( abs.(S .- (π -1)), xaxis=:log, yaxis=:log, markersize=5, label="error")
plot!( (1/24)*d3f_max*(b-a)^4*(2*(1:N)).^(-3), linestyle=:dash, color=:red, label="old error bound" )
plot!( (1:N).^(-4), linestyle=:dash, label=L"n^{-4}" )
plot!( (1/180)*d4f_max*(b-a)^5*(2*(1:N)).^(-4), linestyle=:dash, label="new error bound" )

11.4 Summary

  1. Simpson’s rule is given by \(\frac{b-a}{6} \left( f(a) + 4 f(\tfrac{a+b}2) + f(b) \right)\) and is exact for all polynomials of degree at most \(3\) (Theorem 11.3),
  2. Integrating the interpolation error gives a composite rule with error \(C \|f'''\|_{L^\infty([a,b])} n^{-3}\) (Theorem 11.2), which is not sharp,
  3. Using Hermite interpolation, the composite rule has error \(C \|f''''\|_{L^\infty([a,b])} n^{-4}\) (Theorem 11.4),
  4. Rectangular, trapezoid and Simpson rules: \(O(n^{-1})\), \(O(n^{-2})\) and \(O(n^{-4})\).

11.5 Next time….

  • Gaussian quadrature: choosing the nodes and the weights so that the rule is exact for polynomials of degree \(2n+1\),
  • Orthogonal polynomials.
TipTO-DO
  • Exercises throughout the notes,
  • (Sign-up to office hours if you like)

11.6 Extra exercises

Exercise 11.3 (The error bounds are sharp)

  1. Compute the error of the trapezoid rule for \(f(x) = x^2\) on \([0,1]\) and compare it with the bound in Theorem 10.2.
  2. Compute the error of Simpson’s rule for \(f(x) = x^4\) on \([0,1]\) and compare it with the bound in Theorem 11.4.
  3. Explain why this shows that Simpson’s rule is not exact for all polynomials of degree \(4\).

Exercise 11.4 (Simpson’s 3/8 rule)

Let \(h = \frac{b-a}{3}\) and consider the Newton–Cotes rule with the four nodes \(a, a + h, a + 2h, b\).

  1. Show that the rule is \[ \int_a^b f \approx \frac{b-a}{8} \big[ f(a) + 3 f(a+h) + 3 f(a+2h) + f(b) \big]. \]
  2. Show that this rule is exact for all polynomials of degree at most \(3\), but not for \(x^4\).
  3. Compute the error of this rule for \(f(x) = x^4\) on \([0,1]\) and compare it with the error of Simpson’s rule from Exercise 11.3.

Exercise 11.5 (Choosing the nodes as well)

Consider rules on \([0,1]\) of the form \(\int_0^1 f \approx w_0 f(0) + w_1 f(x_1)\) where \(w_0, w_1\) and \(x_1 \in (0,1]\) are free.

  1. Find \(w_0, w_1, x_1\) so that the rule is exact for all polynomials of degree at most \(2\).
  2. Show that the resulting rule is not exact for \(x^3\).
  3. Compare with the trapezoid rule (which also uses two nodes, one of which is \(0\)). What is the largest degree \(d\) for which you could hope to find a rule \(w_0 f(y_0) + w_1 f(y_1)\) (with both nodes free) that is exact on \(\mathcal P_d\)?

Exercise 11.6

In this exercise, we see why we use composite rules rather than Newton–Cotes rules with many nodes.

  1. Write a Julia function nc_weights(n) which returns the weights \(\hat w_0, \dots, \hat w_n\) of the Newton–Cotes rule on \([0,1]\) with nodes \(\hat x_j = \frac{j}{n}\). (You may solve the linear system obtained from exactness on \(1, x, \dots, x^n\), or integrate the Lagrange polynomials using Polynomials.jl.)
  2. For \(n = 1, \dots, 12\), print the weights. For which \(n\) do negative weights first appear? Plot \(\sum_j |\hat w_j|\) against \(n\).
  3. Suppose that the values \(f(x_j)\) are only known up to errors \(|\varepsilon_j| \leq \varepsilon\). Show that the computed quadrature differs from \(\sum_j w_j f(x_j)\) by at most \(\varepsilon \sum_j |w_j|\). Why is it desirable to have positive weights?
  4. Apply the Newton–Cotes rule with \(n + 1\) nodes on \([-1,1]\) to Runge’s function \(f(x) = \frac{1}{1 + 25x^2}\) (Example 6.2) for \(n = 2, 4, \dots, 20\) and plot the error (the exact integral is \(\frac25 \arctan 5\)). Compare with the composite Simpson rule using the same nodes.
Timer
00:00
min