Chapter 4 — Numerical Differentiation and Integration

Math 128A, Fall 2026 · Per-Olof Persson

Differentiation and integration are both limits, and a computer cannot take a limit. The strategy throughout this chapter is the same one as in Chapter 3: replace \(f\) by a polynomial that interpolates it, then differentiate or integrate the polynomial exactly. Where the two operations differ is in how they behave. Differentiation is unstable — it divides by a small number, so round-off eventually dominates — while integration is stable and averages errors out. That asymmetry is why §4.1 is short and §4.3 onwards is long.

4.1 Numerical Differentiation

Numerical Differentiation
Forward and Backward Differences

Inspired by the definition of derivative: \[ \begin{aligned} f'(x_0) = \lim_{h\rightarrow 0} \frac{f(x_0+h)-f(x_0)}{h}, \end{aligned} \] choose a small \(h\) and approximate \[ \begin{aligned} f'(x_0) \approx \frac{f(x_0+h)-f(x_0)}{h} \end{aligned} \] The error term for the linear Lagrange polynomial gives: \[ \begin{aligned} f'(x_0) = \frac{f(x_0+h)-f(x_0)}{h} -\frac{h}{2}f''(\xi) \end{aligned} \] Also known as the forward-difference formula if \(h>0\) and the backward-difference formula if \(h<0\)


Forward and backward differences

Start with the smallest possible case: two nodes, \(x_0\) and \(x_1 = x_0 + h\). The Lagrange polynomial with its error term is \[ f(x) = f(x_0)\frac{x - x_0 - h}{-h} + f(x_0+h)\frac{x - x_0}{h} + \frac{(x-x_0)(x-x_0-h)}{2}f''(\xi(x)). \]

Differentiating with respect to \(x\), \[ f'(x) = \frac{f(x_0+h) - f(x_0)}{h} + \frac{2(x-x_0) - h}{2}f''(\xi(x)) + \frac{(x-x_0)(x-x_0-h)}{2}\,D_x\!\left[f''(\xi(x))\right]. \]

Setting \(x = x_0\) makes the last term vanish, because the factor \((x-x_0)\) is zero there — which is convenient, since we know nothing about the derivative of \(\xi(x)\). What remains is the forward-difference formula: \[ f'(x_0) = \frac{f(x_0+h) - f(x_0)}{h} - \frac{h}{2}f''(\xi). \]

The forward-difference approximation. The secant over a step of h stands in for the tangent, and the gap between the two slopes is the O(h) error.
Figure 4.1: The forward-difference approximation. The secant over a step of \(h\) stands in for the tangent, and the gap between the two slopes is the \(O(h)\) error.

Taking \(h > 0\) gives the forward difference; \(h < 0\) gives the backward difference. Either way the error is \(O(h)\): first order, and not very good.

Example 4.1

Forward difference for \(e^x\).

Take \(f(x) = e^x\), \(x_0 = 0\) and \(h = 0.1\). Then \[ f'(0) \approx \frac{e^{0.1} - e^{0}}{0.1} \approx 1.052 . \]

For the error, \(f''(x) = e^x\), so on \([0, 0.1]\) we have \(|f''(\xi)| \le e^{0.1}\) and \[ |\text{error}| \le \frac{h}{2}e^{0.1} = \frac{e^{0.1}\cdot 0.1}{2} = 0.0553 . \] The actual error is \(1.052 - 1 = 0.052\), comfortably inside the bound and close to it.

Three-point formulas

Using three nodes instead of two gives a better formula, by the same route: write down the quadratic interpolant and differentiate it.

The three-point formulas come from differentiating the quadratic through three nodes.
Figure 4.2: The three-point formulas come from differentiating the quadratic through three nodes.
General Derivative Approximations
Differentiation of Lagrange Polynomials

Differentiate \[ \begin{aligned} f(x)=\sum_{k=0}^n f(x_k)L_k(x) + \frac{(x-x_0)\cdots (x-x_n)}{(n+1)!} f^{(n+1)}(\xi(x)) \end{aligned} \] to get \[ \begin{aligned} f'(x_j) = \sum_{k=0}^n f(x_k)L'_k(x_j)+ \frac{f^{(n+1)}(\xi(x_j))}{(n+1)!}\prod_{k\ne j} (x_j-x_k) \end{aligned} \] This is the \((n+1)\)-point formula for approximating \(f'(x_j)\).


Differentiating the Lagrange form at a node \(x_j\) gives \[ \begin{aligned} f'(x_j) = f(x_0)\frac{2x_j - x_1 - x_2}{(x_0-x_1)(x_0-x_2)} &+ f(x_1)\frac{2x_j - x_0 - x_2}{(x_1-x_0)(x_1-x_2)} \\ &+ f(x_2)\frac{2x_j - x_0 - x_1}{(x_2-x_0)(x_2-x_1)} + \frac{1}{6}f^{(3)}(\xi_j)\prod_{k\ne j}(x_j - x_k). \end{aligned} \]

This is general, but the useful cases are equally spaced nodes, \(x_1 - x_0 = x_2 - x_1 = h\). Substituting and simplifying at \(x_j = x_0\), \[ \begin{aligned} f'(x_0) &= \frac{1}{h}\left[-\tfrac32 f(x_0) + 2f(x_1) - \tfrac12 f(x_2)\right] + \frac{h^2}{3}f^{(3)}(\xi_0) \\ &= \frac{1}{h}\left[-\tfrac32 f(x_0) + 2f(x_0+h) - \tfrac12 f(x_0+2h)\right] + \frac{h^2}{3}f^{(3)}(\xi_0), \end{aligned} \] which is the three-point endpoint formula. Replacing \(h\) by \(-h\) gives the version that looks backwards: \[ f'(x_0) = \frac{1}{h}\left[\tfrac12 f(x_0-2h) - 2f(x_0-h) + \tfrac32 f(x_0)\right] + \frac{h^2}{3}f^{(3)}(\xi_0). \]

Evaluating at the middle node instead gives the three-point midpoint formula, in which the \(f(x_0)\) term drops out altogether: \[ f'(x_0) = \frac{1}{2h}\left[f(x_0+h) - f(x_0-h)\right] - \frac{h^2}{6}f^{(3)}(\xi_1). \]

Both are \(O(h^2)\), but the midpoint formula has half the error constant and uses one fewer evaluation. It is the one to reach for when the data allows it.

Commonly Used Formulas

Using equally spaced points with \(h=x_{j+1}-x_j\), we have the three-point formulas \[ \begin{aligned} f'(x_0) &= \frac{1}{2h}[-3f(x_0)+4f(x_0+h)-f(x_0+2h)]+\frac{h^2}{3}f^{(3)}(\xi_0) \\ f'(x_0) &= \frac{1}{2h}[-f(x_0-h)+f(x_0+h)] - \frac{h^2}{6}f^{(3)}(\xi_1) \\ f'(x_0) &= \frac{1}{2h}[f(x_0-2h)-4f(x_0-h)+3f(x_0)] + \frac{h^2}{3}f^{(3)}(\xi_2) \\ f''(x_0) &= \frac{1}{h^2}[f(x_0-h)-2f(x_0)+f(x_0+h)] -\frac{h^2}{12} f^{(4)}(\xi) \end{aligned} \] and the five-point formula \[ \begin{aligned} f'(x_0) &= \frac{1}{12h}[f(x_0-2h)-8f(x_0-h)+8f(x_0+h)-f(x_0+2h)] \\ &+ \frac{h^4}{30} f^{(5)}(\xi) \end{aligned} \]


Example 4.2

Comparing the two three-point formulas.

Again \(f(x) = e^x\), \(x_0 = 0\), \(h = 0.1\).

Midpoint: \[ f'(0) \approx \frac{e^{0.1} - e^{-0.1}}{0.2} = 1.0017, \qquad |\text{error}| \le \frac{0.1^2}{6}e^{0.1} = 0.0018 . \]

Left endpoint: \[ f'(0) \approx \frac{1}{0.2}\left(-3e^{0} + 4e^{0.1} - e^{0.2}\right) = 0.9964, \qquad |\text{error}| \le \frac{0.1^2}{3}e^{0.2} = 0.0041 . \]

Both beat the two-point value of \(1.052\) by more than an order of magnitude, and the midpoint formula is the better of the two — as its error constant predicts.

Second derivatives

For higher derivatives it is easier to work directly from Taylor’s theorem than from an interpolant. Expanding in both directions about \(x_0\): \[ \begin{aligned} f(x_0+h) &= f(x_0) + f'(x_0)h + \tfrac12 f''(x_0)h^2 + \tfrac16 f^{(3)}(x_0)h^3 + \tfrac{1}{24}f^{(4)}(\xi_1)h^4, \\ f(x_0-h) &= f(x_0) - f'(x_0)h + \tfrac12 f''(x_0)h^2 - \tfrac16 f^{(3)}(x_0)h^3 + \tfrac{1}{24}f^{(4)}(\xi_{-1})h^4 . \end{aligned} \]

Adding them cancels every odd-order term: \[ f(x_0+h) + f(x_0-h) = 2f(x_0) + f''(x_0)h^2 + \tfrac{1}{24}\left[f^{(4)}(\xi_1) + f^{(4)}(\xi_{-1})\right]h^4 . \]

The bracket is an average of two values of \(f^{(4)}\), so by the intermediate value theorem it equals \(2f^{(4)}(\xi)\) for some \(\xi\) between them. Solving for \(f''(x_0)\) gives the centred second-difference formula: \[ f''(x_0) = \frac{1}{h^2}\left[f(x_0+h) - 2f(x_0) + f(x_0-h)\right] - \frac{h^2}{12}f^{(4)}(\xi). \]

Example 4.3

Second derivative of \(e^x\).

With \(f(x) = e^x\), \(x_0 = 0\), \(h = 0.1\), \[ f''(0) \approx \frac{1}{0.1^2}\left(e^{0.1} - 2e^{0} + e^{-0.1}\right) = 1.00083, \] and the bound is \[ |\text{error}| \le \frac{0.1^2}{12}e^{0.1} = 0.0009 . \]

Optimal \(h\)

The bound above suggests taking \(h\) as small as possible. Round-off says otherwise: the numerator is a difference of nearly equal numbers divided by \(h^2\), so the cancellation of §1.2 is amplified as \(h\) shrinks. The total error is the sum of a truncation term that decreases with \(h\) and a round-off term that increases, and the best \(h\) is where the two balance — not zero.

4.2 Richardson’s Extrapolation

Richardson’s Extrapolation

\[ \begin{array}{cccc} \hline O(h) & O(h^2) & O(h^3) & O(h^4) \\ \hline \textbf{1:} N_1(h)\equiv N(h) \\ \textbf{2:} N_1(\frac{h}{2})\equiv N(\frac{h}{2}) & \textbf{3:} N_2(h) \\ \textbf{4:} N_1(\frac{h}{4})\equiv N(\frac{h}{4}) & \textbf{5:} N_2(\frac{h}{2}) & \textbf{6:} N_3(h) \\ \textbf{7:} N_1(\frac{h}{8})\equiv N(\frac{h}{8}) & \textbf{8:} N_2(\frac{h}{4}) & \textbf{9:} N_3(\frac{h}{2}) & \textbf{10:} N_4(h) \end{array} \]


Richardson’s extrapolation is a way of getting a high-order formula out of a low-order one, for free, by combining results at two step sizes. Suppose an approximation \(N(h)\) to an unknown \(M\) has an error expansion \[ M = N(h) + K_1h + K_2h^2 + K_3h^3 + \cdots \tag{$*$} \] with constants \(K_i\) we do not know. Halving the step, \[ M = N\!\left(\tfrac{h}{2}\right) + K_1\frac{h}{2} + K_2\frac{h^2}{4} + K_3\frac{h^3}{8} + \cdots \tag{$**$} \]

Now form \(2\cdot(**) - (*)\). The \(K_1\) terms cancel exactly: \[ M = \underbrace{2N\!\left(\tfrac{h}{2}\right) - N(h)}_{\textstyle N_2(h)} + K_2\!\left(\frac{h^2}{2} - h^2\right) + K_3\!\left(\frac{h^3}{4} - h^3\right) + \cdots \]

So defining \[ N_2(h) = N\!\left(\tfrac{h}{2}\right) + \left[N\!\left(\tfrac{h}{2}\right) - N(h)\right] \] gives an \(O(h^2)\) approximation built from two \(O(h)\) ones. The bracketed term is a computable estimate of the error in \(N(h/2)\), which is what makes the trick useful in practice as well as in theory.

The same step applies again. Writing the expansion for \(N_2\), \[ M = N_2(h) - \frac{K_2}{2}h^2 - \frac{3K_3}{4}h^3 + \cdots, \qquad M = N_2\!\left(\tfrac{h}{2}\right) - \frac{K_2}{2}\cdot\frac{h^2}{4} - \frac{3K_3}{4}\cdot\frac{h^3}{8} + \cdots \] and forming \(4\cdot(\text{second}) - (\text{first})\) eliminates \(K_2\): \[ N_3(h) = N_2\!\left(\tfrac{h}{2}\right) + \tfrac13\left[N_2\!\left(\tfrac{h}{2}\right) - N_2(h)\right], \] which is \(O(h^3)\). In general, \[ N_j(h) = N_{j-1}\!\left(\tfrac{h}{2}\right) + \frac{N_{j-1}\!\left(\tfrac{h}{2}\right) - N_{j-1}(h)}{2^{\,j-1} - 1}. \]

Richardson’s Extrapolation

The computation is organised as a tableau, each column one order better than the last, and each new entry built from two entries to its left:

\(O(h)\) \(O(h^2)\) \(O(h^3)\) \(O(h^4)\)
\(N(h)\)
\(N(h/2)\) \(N_2(h)\)
\(N(h/4)\) \(N_2(h/2)\) \(N_3(h)\)
\(N(h/8)\) \(N_2(h/4)\) \(N_3(h/2)\) \(N_4(h)\)

Each row costs one new evaluation of \(N\) at half the previous step; everything else is arithmetic.

When the error expansion contains only even powers of \(h\) — as it does for the midpoint formula and for the composite trapezoidal rule — each extrapolation gains two orders instead of one. That is exactly the situation in §4.5.

4.3 Elements of Numerical Integration

Numerical Quadrature
Integration of Lagrange Interpolating Polynomials

Select \(\{x_0,\ldots,x_n\}\) in \([a,b]\) and integrate the Lagrange polynomial \(P_n(x)=\sum_{i=0}^n f(x_i)L_i(x)\) and its truncation error term over \([a,b]\) to obtain \[ \begin{aligned} \int_a^b f(x)\, dx = \sum_{i=0}^n a_i f(x_i) + E(f) \end{aligned} \] with \[ \begin{aligned} a_i = \int_a^b L_i(x)\, dx \end{aligned} \] and \[ \begin{aligned} E(f) = \frac{1}{(n+1)!} \int_a^b \prod_{i=0}^n (x-x_i) f^{(n+1)} (\xi(x))\,dx \end{aligned} \]


The recipe is the same as for differentiation: interpolate, then integrate the interpolant exactly. Choose nodes \(x_0, \ldots, x_n\) and write \[ P_n(x) = \sum_{i=0}^n f(x_i)L_i(x). \]

Numerical quadrature: integrate the polynomial through the nodes instead of f itself.
Figure 4.3: Numerical quadrature: integrate the polynomial through the nodes instead of \(f\) itself.

Integrating \(f\) in its Lagrange-plus-remainder form, \[ \int_a^b f(x)\,dx = \int_a^b \sum_{i=0}^n f(x_i)L_i(x)\,dx + \int_a^b \prod_{i=0}^n (x - x_i)\frac{f^{(n+1)}(\xi(x))}{(n+1)!}\,dx, \] and pulling the constants out of the first integral gives a quadrature rule \[ \int_a^b f(x)\,dx = \sum_{i=0}^n a_i f(x_i) + E(f), \qquad a_i = \int_a^b L_i(x)\,dx . \]

The weights are the integrals of the basis functions — nothing more. And because the rule reproduces \(\int P_n\) exactly, it is exact for every polynomial of degree \(\le n\).

The trapezoidal rule

Trapezoidal and Simpson’s Rules
The Trapezoidal Rule

Linear Lagrange polynomial with \(x_0=a\), \(x_1=b\), \(h=b-a\), gives \[ \begin{aligned} \int_a^b f(x)\,dx = \frac{h}{2}[f(x_0)+f(x_1)]-\frac{h^3}{12} f''(\xi) \end{aligned} \]

Simpson’s Rule

Second Lagrange polynomial with \(x_0=a\), \(x_2=b\), \(x_1=a+h\), \(h=(b-a)/2\) gives \[ \begin{aligned} \int_{x_0}^{x_2} f(x)\, dx = \frac{h}{3}[f(x_0)+4f(x_1)+f(x_2)]- \frac{h^5}{90}f^{(4)}(\xi) \end{aligned} \]

Definition 4.1

The degree of accuracy, or precision, of a quadrature formula is the largest positive integer \(n\) such that the formula is exact for \(x^k\), for each \(k=0,1,\ldots, n\).


Take \(n = 1\), with \(x_0 = a\), \(x_1 = b\) and \(h = x_1 - x_0\). The basis functions are the two lines \[ L_0(x) = \frac{x - x_1}{x_0 - x_1}, \qquad L_1(x) = \frac{x - x_0}{x_1 - x_0}, \] and each weight is the area of a triangle of base \(h\) and height \(1\): \[ a_0 = \int_{x_0}^{x_1} L_0(x)\,dx = \frac{h}{2}, \qquad a_1 = \frac{h}{2}. \]

The trapezoidal rule. The weights h/2 are the areas of the triangles under the two basis lines.
Figure 4.4: The trapezoidal rule. The weights \(h/2\) are the areas of the triangles under the two basis lines.

For the error, \(g(x) = (x-x_0)(x-x_1)\) does not change sign on \([x_0,x_1]\), so the weighted mean value theorem for integrals (Theorem 1.6) lets us pull \(f''\) out: \[ E(f) = \frac12\int_{x_0}^{x_1} f''(\xi(x))\,(x-x_0)(x-x_1)\,dx = \frac{f''(\xi)}{2}\int_{x_0}^{x_1}(x-x_0)(x-x_1)\,dx . \] Evaluating that integral gives \(-h^3/6\), so \[ \int_a^b f(x)\,dx = \frac{h}{2}\left[f(x_0) + f(x_1)\right] - \frac{h^3}{12}f''(\xi). \]

Note the consequence: the rule is exact whenever \(f'' \equiv 0\), that is for all linear functions. Its degree of precision is \(1\).

Simpson’s rule

Taking \(n = 2\) with equally spaced nodes gives Simpson’s rule. Rather than integrate three basis functions, it is quicker to impose exactness and solve for the weights.

Simpson’s rule integrates the parabola through the two endpoints and the midpoint.
Figure 4.5: Simpson’s rule integrates the parabola through the two endpoints and the midpoint.
Proof of Simpson’s rule by undetermined coefficients.

Work on \([-h, h]\) and suppose \[ \int_{-h}^{h} f(x)\,dx = a_0f(-h) + a_1f(0) + a_2f(h) + k\,f^{(4)}(\xi). \] Demanding exactness for \(f(x) = 1, x, x^2, x^3, x^4\) in turn gives \[ \begin{aligned} f = 1:\quad & 2h = a_0 + a_1 + a_2, \\ f = x:\quad & 0 = -ha_0 + ha_2, \\ f = x^2:\quad & \tfrac{2h^3}{3} = h^2a_0 + h^2a_2, \\ f = x^3:\quad & 0 = -h^3a_0 + h^3a_2, \\ f = x^4:\quad & \tfrac{2h^5}{5} = h^4a_0 + h^4a_2 + 24k . \end{aligned} \]

The second equation gives \(a_0 = a_2\), and then the third gives \[ \frac{2h^3}{3} = 2a_0h^2 \qquad\Longrightarrow\qquad a_0 = a_2 = \frac{h}{3}, \] so from the first, \(a_1 = 2h - a_0 - a_2 = \tfrac{4h}{3}\). The fourth equation is then satisfied automatically. Finally the fifth gives \[ k = \left(\frac{2h^5}{5} - h^4\cdot\frac{h}{3} - h^4\cdot\frac{h}{3}\right)\Big/ 24 = \frac{h^5}{24}\left(\frac{2}{5} - \frac{2}{3}\right) = -\frac{h^5}{90}, \] which yields Simpson’s rule: \[ \int_{-h}^{h} f(x)\,dx = \frac{h}{3}\left[f(-h) + 4f(0) + f(h)\right] - \frac{h^5}{90}f^{(4)}(\xi). \] ■

The error involves \(f^{(4)}\), so Simpson’s rule is exact for cubics as well as quadratics: its degree of precision is \(3\), one better than interpolation alone would suggest. This bonus order is why Simpson’s rule, and not the quadratic rule one might expect, is the workhorse of elementary quadrature.

The Newton-Cotes Formulas
The Closed Newton-Cotes Formulas

Use nodes \(x_i=x_0+ih\), \(x_0=a\), \(x_n=b\), \(h=(b-a)/n\): \[ \begin{aligned} \int_a^b f(x)\,dx &\approx \sum_{i=0}^n a_i f(x_i) \\ a_i=\int_{x_0}^{x_n} L_i(x)\,dx &= \int_{x_0}^{x_n} \prod_{j\ne i} \frac{(x-x_j)}{(x_i-x_j)}\,dx \end{aligned} \] \(n=1\) gives the Trapezoidal rule, \(n=2\) gives Simpson’s rule.

The Open Newton-Cotes Formulas

Use nodes \(x_i=x_0+ih\), \(x_0=a+h\), \(x_n=b-h\), \(h=(b-a)/(n+2)\). Setting \(n=0\) gives the Midpoint rule: \[ \begin{aligned} \int_{x_{-1}}^{x_1} f(x)\,dx = 2hf(x_0)+\frac{h^3}{3} f''(\xi) \end{aligned} \]


4.4 Composite Numerical Integration

The obvious way to improve accuracy is to use more nodes in a single rule. It does not work well.

A high-degree Newton-Cotes rule. One polynomial through all the nodes gives complicated coefficients and oscillatory behaviour.
Figure 4.6: A high-degree Newton-Cotes rule. One polynomial through all the nodes gives complicated coefficients and oscillatory behaviour.

The coefficients become unwieldy, some of them turn negative — which invites cancellation — and the interpolant oscillates, exactly as in the Runge example of §3.5. The fix is the same as it was there: go piecewise.

Composite Rules
Theorem 4.1

Let \(f\in C^2[a,b]\), \(h=(b-a)/n\), \(x_j=a+jh\), \(\mu\in(a,b)\). The Composite Trapezoidal rule for \(n\) subintervals is \[ \begin{aligned} \int_a^b f(x)\,dx &= \frac{h}{2} \left[ f(a)+2\sum_{j=1}^{n-1} f(x_j) + f(b) \right] -\frac{b-a}{12} h^2 f''(\mu) \end{aligned} \]

Theorem 4.2

Let \(f\in C^4[a,b]\), \(n\) even, \(h=(b-a)/n\), \(x_j=a+jh\), \(\mu\in(a,b)\). The Composite Simpson’s rule for \(n\) subintervals is \[ \begin{aligned} \int_a^b f(x)\,dx &= \frac{h}{3} \left[ f(a)+2\sum_{j=1}^{(n/2)-1} f(x_{2j}) + 4\sum_{j=1}^{n/2} f(x_{2j-1}) + f(b) \right] \\ &-\frac{b-a}{180} h^4 f^{(4)}(\mu) \end{aligned} \]


The composite trapezoidal rule

Divide \([a,b]\) into \(n\) equal subintervals of width \(h = (b-a)/n\), with \(x_j = a + jh\), and apply the trapezoidal rule on each: \[ \begin{aligned} \int_a^b f(x)\,dx &\approx \frac{h}{2}\bigl(f(x_0) + f(x_1)\bigr) + \frac{h}{2}\bigl(f(x_1) + f(x_2)\bigr) + \cdots + \frac{h}{2}\bigl(f(x_{n-1}) + f(x_n)\bigr) \\ &= \frac{h}{2}\left[f(x_0) + 2\sum_{j=1}^{n-1}f(x_j) + f(x_n)\right]. \end{aligned} \]

The composite trapezoidal rule. Interior nodes belong to two panels each and so carry weight 2.
Figure 4.7: The composite trapezoidal rule. Interior nodes belong to two panels each and so carry weight \(2\).

Its error works out to \[ E(f) = -\frac{b-a}{12}h^2 f''(\mu) \] for some \(\mu \in (a,b)\): second order in \(h\).

The composite Simpson rule

Simpson’s rule consumes two subintervals at a time, so \(n\) must be even. Applying it to each consecutive pair and collecting terms, \[ \begin{aligned} \int_a^b f(x)\,dx &\approx \frac{h}{3}\left[f(x_0) + 4f(x_1) + f(x_2)\right] + \frac{h}{3}\left[f(x_2) + 4f(x_3) + f(x_4)\right] + \cdots \\ &= \frac{h}{3}\left[f(x_0) + 2\sum_{j=1}^{n/2-1}f(x_{2j}) + 4\sum_{j=1}^{n/2}f(x_{2j-1}) + f(x_n)\right]. \end{aligned} \]

The composite Simpson rule. Panel midpoints carry weight 4; the nodes where two panels meet carry weight 2.
Figure 4.8: The composite Simpson rule. Panel midpoints carry weight \(4\); the nodes where two panels meet carry weight \(2\).

The error is a sum of the individual panel errors, \[ E(f) = -\sum_{j=1}^{n/2}\frac{h^5}{90}f^{(4)}(\xi_j), \qquad \xi_j \in (x_{2j-2}, x_{2j}), \] and this can be collapsed into a single term. Since every \(f^{(4)}(\xi_j)\) lies between the minimum and maximum of \(f^{(4)}\) on \([a,b]\), so does their average: \[ \min_{x\in[a,b]} f^{(4)}(x) \;\le\; \frac{2}{n}\sum_{j=1}^{n/2}f^{(4)}(\xi_j) \;\le\; \max_{x\in[a,b]} f^{(4)}(x), \] so by the intermediate value theorem there is a \(\mu \in (a,b)\) with \[ f^{(4)}(\mu) = \frac{2}{n}\sum_{j=1}^{n/2}f^{(4)}(\xi_j). \] Substituting, and using \(n = (b-a)/h\), \[ E(f) = -\frac{h^5}{90}\cdot\frac{n}{2}\cdot f^{(4)}(\mu) = -\frac{h^4}{180}(b-a)f^{(4)}(\mu) = -\frac{b-a}{180}h^4 f^{(4)}(\mu), \] fourth order: two orders better than the composite trapezoidal rule for the same number of evaluations.

Example 4.4

Composite Simpson on \(\displaystyle\int_0^\pi \sin^2 x\,dx\) with \(n = 4\).

Here \(h = \pi/4\) and the nodes are \(0, \pi/4, \pi/2, 3\pi/4, \pi\), with values \(0, \tfrac12, 1, \tfrac12, 0\).

The five nodes and their composite Simpson weights 1, 4, 2, 4,
1.
Figure 4.9: The five nodes and their composite Simpson weights \(1, 4, 2, 4, 1\).

Applying the rule, \[ \begin{aligned} I &\approx \frac{h}{3}\left[f(x_0) + 4f(x_1) + 2f(x_2) + 4f(x_3) + f(x_4)\right] \\ &= \frac{\pi}{4\cdot 3}\left[0 + 4\cdot\tfrac12 + 2\cdot 1 + 4\cdot\tfrac12 + 0\right] = \frac{\pi}{12}\cdot 6 = \frac{\pi}{2}. \end{aligned} \] This is the exact answer — a coincidence, not a general phenomenon.

The bound does not know that. With \(f(x) = \sin^2 x = \tfrac{1-\cos 2x}{2}\) we get \(f^{(4)}(x) = -\tfrac{2^4\cos 2x}{2} = -8\cos 2x\), so \(|f^{(4)}| \le 8\) and \[ |E(f)| \le \frac{b-a}{180}h^4\left|f^{(4)}(\mu)\right| \le \frac{\pi}{180}\left(\frac{\pi}{4}\right)^{4}\cdot 8 = 0.053 . \]

Example 4.5

How many trapezoidal points for the same accuracy?

The composite trapezoidal error is \(E(f) = -\frac{b-a}{12}h^2f''(\mu)\). For \(f = \sin^2 x\) we have \(f'' = 2\cos 2x\), so \(|f''| \le 2\), and we need \[ \left|\frac{b-a}{12}h^2 f''(\mu)\right| \le 0.053 . \] With \(b - a = \pi\) and \(h = \pi/n\), \[ \frac{\pi}{12}\left(\frac{\pi}{n}\right)^{2}\cdot 2 \le 0.053 \qquad\Longrightarrow\qquad n \ge 9.86, \] so \(n = 10\). Simpson achieved the same accuracy with \(n = 4\): five function evaluations against eleven.

4.5 Romberg Integration

Romberg Integration

Romberg integration is Richardson’s extrapolation applied to the composite trapezoidal rule. What makes the pairing so effective is that the trapezoidal error expansion contains only even powers of \(h\), so each extrapolation step gains two orders rather than one.

Romberg integration. Each level halves the step, and every function value from the previous level is reused.
Figure 4.10: Romberg integration. Each level halves the step, and every function value from the previous level is reused.

Write \(R_{k,1}\) for the composite trapezoidal estimate with \(h_k = (b-a)/2^{k-1}\). Successive levels nest: level \(1\) uses only \(a\) and \(b\), level \(2\) adds the midpoint, level \(3\) adds the two quarter points, and so on. Each level therefore costs only the new points: \[ R_{k,1} = \frac12\left[R_{k-1,1} + h_{k-1}\sum_{i=1}^{2^{k-2}} f\!\left(a + \left(i - \tfrac12\right)h_{k-1}\right)\right]. \]

Romberg Integration

Extrapolating across each row, \[ R_{k,j} = R_{k,j-1} + \frac{R_{k,j-1} - R_{k-1,j-1}}{4^{\,j-1} - 1}, \] where the \(4^{j-1}\) — rather than \(2^{j-1}\) — is precisely the even-powers bonus. The entry \(R_{k,j}\) is \(O(h_k^{2j})\).

Romberg Integration
MATLAB Implementation
function R = romberg(f, a, b, n)
% Compute integral of f(x) from a to b using Romberg integration.

h = b - a;
R = zeros(n, n);
R(1, 1) = h / 2 * (f(a) + f(b));
for i = 2:n
    x = a + ((1:2^(i - 2)) - 0.5) * h;
    R(i, 1) = (R(i - 1, 1) + h * sum(f(x))) / 2;
    for j = 2:i
        R(i, j) = R(i, j - 1) + ...
            (R(i, j - 1) - R(i - 1, j - 1)) / (4^(j - 1) - 1);
    end
    h = h / 2;
end

4.6 Adaptive Quadrature Methods

Error Estimation

Composite rules spread their nodes uniformly, which is wasteful when \(f\) is smooth in most of the interval and difficult in a small part of it.

Composite rules spread nodes evenly. Adaptive quadrature puts them where the integrand actually varies.
Figure 4.11: Composite rules spread nodes evenly. Adaptive quadrature puts them where the integrand actually varies.

To place points intelligently we need to know where the error is large — but the error formula involves \(f^{(4)}(\mu)\), about which we know nothing in practice. The way out is to estimate the error from two computations at different step sizes.

Let \(S(a,b)\) denote Simpson’s rule applied on \([a,b]\). Then \[ \int_a^b f(x)\,dx = S(a,b) - \frac{h^5}{90}f^{(4)}(\mu), \] and halving the interval, applying Simpson to each half, \[ \int_a^b f(x)\,dx = S\!\left(a, \tfrac{a+b}{2}\right) + S\!\left(\tfrac{a+b}{2}, b\right) - \frac{1}{16}\cdot\frac{h^5}{90}f^{(4)}(\tilde\mu), \] the \(1/16\) arising because halving \(h\) divides \(h^5\) by \(32\) while there are now two panels.

Assuming \(f^{(4)}(\mu) \approx f^{(4)}(\tilde\mu)\) — the one leap of faith — and subtracting, \[ \frac{h^5}{90}f^{(4)}(\mu) \approx \frac{16}{15} \left[S(a,b) - S\!\left(a, \tfrac{a+b}{2}\right) - S\!\left(\tfrac{a+b}{2}, b\right)\right]. \] The error in the better (two-panel) estimate is \(1/16\) of this, so \[ \text{error} \approx \frac{1}{15} \left[S(a,b) - S\!\left(a, \tfrac{a+b}{2}\right) - S\!\left(\tfrac{a+b}{2}, b\right)\right]. \]

This is computable from quantities we already have. If it exceeds the tolerance, subdivide and recurse; if not, accept the panel.

Example 4.6

Estimating the error for \(\displaystyle\int_0^4 e^x\,dx\).

With \(n = 2\) and \(h = (4-0)/2 = 2\), \[ S(0,4) = \frac{h}{3}\left[f(0) + 4f(2) + f(4)\right] = \frac{2}{3}\left[1 + 4e^2 + e^4\right] \approx 56.7696 . \] Subdividing, with \(h = 1\) on each half, \[ S(0,2) + S(2,4) = \frac{1}{3}\left[1 + 4e^1 + 2e^2 + 4e^3 + e^4\right] \approx 53.8638 . \]

The true value is \(e^4 - 1 \approx 53.5982\), so the true error of the better estimate is \(0.2657\), while the estimate gives \[ \frac{1}{15}\left[56.7696 - 53.8638\right] = 0.1937 . \] The estimate is the right order of magnitude and conservative in the useful direction — it is smaller than the true error here, which is why practical codes apply a safety factor.

Adaptive Quadrature

4.7 Gaussian Quadrature

Gaussian Quadrature

Every rule so far has used equally spaced nodes, chosen for convenience. If we are willing to choose the nodes too, we get far more accuracy per evaluation.

The midpoint rule on [-1,1]: one node, weight 2.
Figure 4.12: The midpoint rule on \([-1,1]\): one node, weight \(2\).
Example 4.7

Degree of precision of the midpoint rule.

On \([-1,1]\) the midpoint rule is \(\int_{-1}^1 f \approx 2f(0)\). Testing it on monomials: \[ \begin{aligned} k=0: && \int_{-1}^{1}1\,dx &= 2, & 2f(0) &= 2\cdot 1 = 2 && \checkmark \\ k=1: && \int_{-1}^{1}x\,dx &= 0, & 2f(0) &= 2\cdot 0 = 0 && \checkmark \\ k=2: && \int_{-1}^{1}x^2\,dx &= \tfrac23, & 2f(0) &= 2\cdot 0 = 0 && \times \end{aligned} \] The \(k=1\) case works only because the node sits at the centre. Degree of precision \(1\), from one evaluation.

Example 4.8

The trapezoidal rule does no better.

With two evaluations, \(\frac{h}{2}[f(-1) + f(1)] = f(-1) + f(1)\): \[ \begin{aligned} k=0: && \tfrac22\left[1 + 1\right] &= 2 && \checkmark \\ k=1: && \tfrac22\left[-1 + 1\right] &= 0 && \checkmark \\ k=2: && \tfrac22\left[1 + 1\right] &= 2 \ne \tfrac23 && \times \end{aligned} \] Two evaluations, still only degree of precision \(1\). The nodes are being wasted.

Choosing the nodes

Suppose instead we allow both nodes and weights to be free: \[ \int_{-1}^{1} f(x)\,dx \approx c_1 f(x_1) + c_2 f(x_2). \] That is four unknowns, so we can hope for exactness on four monomials — degree of precision \(3\). Imposing them: \[ \begin{aligned} f = 1:\quad & 2 = c_1 + c_2, &(1)\\ f = x:\quad & 0 = c_1x_1 + c_2x_2, &(2)\\ f = x^2:\quad & \tfrac23 = c_1x_1^2 + c_2x_2^2, &(3)\\ f = x^3:\quad & 0 = c_1x_1^3 + c_2x_2^3. &(4) \end{aligned} \]

Symmetry suggests \(c_1 = c_2\) and \(x_1 = -x_2\), which satisfies \((2)\) and \((4)\) identically. Then \((1)\) gives \(2 = 2c_1\), so \(c_1 = c_2 = 1\), and \((3)\) becomes \[ \tfrac23 = x_1^2 + x_2^2 = 2x_1^2 \qquad\Longrightarrow\qquad x_1 = -\frac{1}{\sqrt3},\quad x_2 = \frac{1}{\sqrt3}, \] giving the two-point Gauss rule \[ \int_{-1}^{1} f(x)\,dx \approx f\!\left(-\tfrac{1}{\sqrt3}\right) + f\!\left(\tfrac{1}{\sqrt3}\right), \] with degree of precision \(3\) — the same as Simpson’s rule, from two evaluations instead of three.

Legendre polynomials

Legendre Polynomials

Solving for the nodes by hand does not scale, but there is a pattern: the optimal nodes are the roots of the Legendre polynomials. \(P_n\) is the monic polynomial of degree \(n\) with \[ \int_{-1}^{1} P(x)P_n(x)\,dx = 0 \qquad\text{whenever } \deg P < n . \]

The first few are \[ P_0(x) = 1, \qquad P_1(x) = x, \qquad P_2(x) = x^2 - \tfrac13, \] and indeed the roots of \(P_2\) are \(\pm 1/\sqrt3\) — the nodes we just derived.

Gaussian Quadrature
Theorem 4.3

Suppose \(x_1,\ldots,x_n\) are roots of \(P_n(x)\) and \[ \begin{aligned} c_i = \int_{-1}^1 \prod_{j\ne i}^n \frac{x-x_j}{x_i-x_j}\,dx \end{aligned} \] If \(P(x)\) is any polynomial of degree less than \(2n\), then \[ \begin{aligned} \int_{-1}^1 P(x)\,dx = \sum_{i=1}^n c_i P(x_i) \end{aligned} \]


Proof of the Gaussian quadrature theorem.

Take the nodes \(x_1, \ldots, x_n\) to be the roots of \(P_n\) and \[ c_i = \int_{-1}^{1}\prod_{j\ne i}\frac{x - x_j}{x_i - x_j}\,dx . \]

First suppose \(\deg P < n\). Then \(P\) equals its own Lagrange interpolant at the \(n\) nodes, \[ P(x) = \sum_{i=1}^{n}P(x_i)L_i(x) = \sum_{i=1}^{n}\prod_{j\ne i}\frac{x-x_j}{x_i-x_j}P(x_i), \] so integrating and exchanging the sum with the integral, \[ \int_{-1}^{1}P(x)\,dx = \sum_{i=1}^{n}\left[\int_{-1}^{1}\prod_{j\ne i}\frac{x-x_j}{x_i-x_j}\,dx\right]P(x_i) = \sum_{i=1}^{n}c_iP(x_i), \] which is the claim.

Now suppose \(n \le \deg P < 2n\). Divide by \(P_n\): \[ P(x) = Q(x)P_n(x) + R(x), \qquad \deg Q < n, \quad \deg R < n . \] At each node \(P_n(x_i) = 0\), so \[ P(x_i) = Q(x_i)\underbrace{P_n(x_i)}_{=\,0} + R(x_i) = R(x_i). \tag{$*$} \] Integrating, \[ \int_{-1}^{1}P(x)\,dx = \underbrace{\int_{-1}^{1}Q(x)P_n(x)\,dx}_{=\,0 \text{ since } \deg Q < n} + \int_{-1}^{1}R(x)\,dx = \int_{-1}^{1}R(x)\,dx, \] where the first integral vanishes by the defining property of \(P_n\). Since \(\deg R < n\), the first part of the proof applies to \(R\): \[ \int_{-1}^{1}R(x)\,dx = \sum_{i=1}^{n}c_iR(x_i) = \sum_{i=1}^{n}c_iP(x_i), \] the last step by \((*)\). So the rule is exact for every polynomial of degree less than \(2n\): degree of precision \(2n-1\) from \(n\) evaluations. ■

Computing Gaussian Quadrature Coefficients
MATLAB Implementation
function [x, c] = gaussquad(n)
% Compute Gaussian quadrature points and coefficients.
% For education only: the monomial basis is unstable for high n.

P = zeros(n + 1, n + 1);
P([1, 2], 1) = 1;
for k = 1:n - 1
    P(k + 2, 1:k + 2) = ...
        ((2 * k + 1) * [P(k + 1, 1:k + 1), 0] - ...
        k * [0, 0, P(k, 1:k)]) / (k + 1);
end
x = sort(roots(P(n + 1, 1:n + 1)));

A = zeros(n, n);
for i = 1:n
    A(i, :) = polyval(P(i, 1:i), x)';
end
c = A \ [2; zeros(n - 1, 1)];

Arbitrary intervals

Arbitrary Intervals

Transform integrals \(\int_a^b f(x)\,dx\) into integrals over \([-1,1]\) by a change of variables: \[ \begin{aligned} t=\frac{2x-a-b}{b-a} \Leftrightarrow x=\frac{1}{2}[(b-a)t+a+b] \end{aligned} \] Gaussian quadrature then gives \[ \begin{aligned} \int_a^b f(x)\,dx = \int_{-1}^1 f\left(\frac{(b-a)t+(b+a)}{2}\right) \frac{(b-a)}{2}\,dt \end{aligned} \]


Nodes and weights are tabulated only for \([-1,1]\), so an affine change of variable does the rest.

The change of variable mapping [a,b] onto [-1,1]. The shaded area is the same; only the labelling changes.
Figure 4.13: The change of variable mapping \([a,b]\) onto \([-1,1]\). The shaded area is the same; only the labelling changes.

Setting \[ x = \tfrac12\left[(b-a)t + a + b\right], \qquad\text{equivalently}\qquad t = \frac{2x - a - b}{b - a}, \] we get \[ \int_a^b f(x)\,dx = \int_{-1}^{1}f\!\left(\frac{(b-a)t + (b+a)}{2}\right)\frac{b-a}{2}\,dt . \]

Example 4.9

Gaussian quadrature for \(\displaystyle\int_0^4 e^x\,dx\).

With \(a=0\), \(b=4\) the map is \(x = 2t + 2\) and \(\frac{b-a}{2} = 2\).

\(n = 2\): nodes \(t_1 = -1/\sqrt3\), \(t_2 = 1/\sqrt3\) with weights \(w_1 = w_2 = 1\), so \[ \int_0^4 e^x\,dx \approx 2\left[1\cdot e^{2 - 2/\sqrt3} + 1\cdot e^{2 + 2/\sqrt3}\right] = 51.549 . \]

\(n = 3\): nodes \(t_1 = -\sqrt{3/5}\), \(t_2 = 0\), \(t_3 = \sqrt{3/5}\) with weights \(\tfrac59, \tfrac89, \tfrac59\), so \[ \int_0^4 e^x\,dx \approx 2\left[\tfrac59 e^{2 - 2\sqrt{3/5}} + \tfrac89 e^{2} + \tfrac59 e^{2 + 2\sqrt{3/5}}\right] = 53.5303 . \]

Against the true value \(e^4 - 1 = 53.5982\), three evaluations give two correct digits. Compare Simpson with the same three evaluations: \(56.7696\), not one correct digit.

4.8 Multiple Integrals

Double Integrals

A double integral over a rectangle is two nested one-dimensional integrals, so the rules carry over directly: apply a rule in \(y\) for each fixed \(x\), then a rule in \(x\).

The rectangular region and its grid.
Figure 4.14: The rectangular region and its grid.

Take \(R = [a,b]\times[c,d]\) with \(h = (b-a)/n\) and \(k = (d-c)/m\). Writing the inner integral first, \[ \int_a^b\!\left(\int_c^d f(x,y)\,dy\right)dx \approx \int_a^b \frac{k}{2}\Bigl(f(x,c) + 2f(x,c+k) + f(x,d)\Bigr)dx \] for \(m = 2\), and then applying the trapezoidal rule in \(x\) as well and collecting terms gives \[ \begin{aligned} \approx \frac{hk}{4}\Bigl( &f(a,c) + 2f(a{+}h,c) + f(b,c) \\ &+ 2f(a,c{+}k) + 4f(a{+}h,c{+}k) + 2f(b,c{+}k) \\ &+ f(a,d) + 2f(a{+}h,d) + f(b,d)\Bigr). \end{aligned} \]

The weights are products of the one-dimensional weights, which is why they form a recognisable stencil.

Weight stencils for the two composite double rules. Each weight is the product of the corresponding one-dimensional weights.
Figure 4.15: Weight stencils for the two composite double rules. Each weight is the product of the corresponding one-dimensional weights.
Composite Simpson’s Rule Double Integration

The Composite Simpson’s rule gives \[ \begin{aligned} \int_a^b \left(\int_c^d f(x,y)\,dy\right)\,dx = \frac{hk}{9} \sum_{i=0}^n \sum_{j=0}^m w_{i,j} f(x_i,y_j) + E \end{aligned} \] where \(x_i=a+ih\), \(y_j=c+jk\), \(w_{i,j}\) are the products of the nested Composite Simpson’s rule coefficients (see below), and the error is \[ \begin{aligned} E=-\frac{(d-c)(b-a)}{180}\left[h^4\frac{\partial^4 f}{\partial x^4} (\bar{\eta},\bar{\mu}) + k^4\frac{\partial^4 f}{\partial y^4} (\hat{\eta},\hat{\mu})\right] \end{aligned} \]

\[ \begin{array}{rlllll} d & \bullet 1\ & \bullet 4 & \bullet 2 & \bullet 4 & \bullet 1 \\ \ \\ & \bullet 4 & \bullet 16 & \bullet 8 & \bullet 16 & \bullet 4 \\ \ \\ c & \bullet 1\ & \bullet 4 & \bullet 2 & \bullet 4 & \bullet 1 \\ & a & & & & b \end{array} \]


Example 4.10

Composite Simpson on \(\displaystyle\int_0^1\!\int_0^2 e^{x+y}\,dy\,dx\).

With \(n = m = 2\), so \(h = \tfrac12\) and \(k = 1\), the \(3\times 3\) Simpson stencil gives \[ \begin{aligned} I \approx \frac{\tfrac12 \cdot 1}{9}\Bigl[ &e^{0+0} + 4e^{0.5+0} + e^{1+0} \\ &+ 4e^{0+1} + 16e^{0.5+1} + 4e^{1+1} \\ &+ e^{0+2} + 4e^{0.5+2} + e^{1+2}\Bigr] = 11.036 . \end{aligned} \]

Gaussian Double Integration

Example 4.11

Gaussian double integration on the same integral.

Using the two-point rule in each direction needs only four evaluations.

Gaussian double integration: four nodes, all of weight 1.
Figure 4.16: Gaussian double integration: four nodes, all of weight \(1\).

The maps are \(x = \tfrac12(t+1)\) and \(y = t+1\), so \[ x_1 = \tfrac12\!\left(1 - \tfrac{1}{\sqrt3}\right),\quad x_2 = \tfrac12\!\left(1 + \tfrac{1}{\sqrt3}\right),\quad y_1 = 1 - \tfrac{1}{\sqrt3},\quad y_2 = 1 + \tfrac{1}{\sqrt3}, \] and \[ I \approx \frac{(1-0)(2-0)}{4} \left[e^{x_1+y_1} + e^{x_2+y_1} + e^{x_1+y_2} + e^{x_2+y_2}\right] = 10.940 . \] The exact value is \((e-1)(e^2-1) \approx 11.026\), so four Gauss points beat nine Simpson points.

Non-rectangular regions

Non-Rectangular Regions

The same technique can be applied to double integrals of the form \[ \begin{aligned} \int_a^b \int_{c(x)}^{d(x)} f(x,y)\,dy\,dx \end{aligned} \] The step size for \(x\) is still \(h=(b-a)/n\), but for \(y\) it varies with \(x\): \[ \begin{aligned} k(x) = \frac{d(x)-c(x)}{m} \end{aligned} \]


When the region is bounded by curves \(c(x) \le y \le d(x)\), the inner limits depend on \(x\), so each vertical slice gets its own grid.

A region between two curves, and the worked example with c(x) = x and d(x) = 1 + x/2.
Figure 4.17: A region between two curves, and the worked example with \(c(x) = x\) and \(d(x) = 1 + x/2\).
Example 4.12

A non-rectangular region.

For \[ \int_0^1\!\int_x^{1+x/2} e^{x+y}\,dy\,dx, \] the MATLAB function simpsondouble gives

\(n = m\) estimate
\(2\) \(3.1098\)
\(4\) \(3.1146\)
\(16\) \(3.1149\)

converging as the grid refines.

4.9 Improper Integrals

Improper Integrals with a Singularity

The improper integral below, with a singularity at the left endpoint, converges if and only if \(0<p<1\) and then \[ \begin{aligned} \int_a^b \frac{1}{(x-a)^p}dx = \left.\frac{(x-a)^{1-p}}{1-p}\right|_a^b =\frac{(b-a)^{1-p}}{1-p} \end{aligned} \] More generally, if \[ \begin{aligned} f(x)=\frac{g(x)}{(x-a)^p},\quad 0<p<1,\quad g\text{ continuous on }[a,b], \end{aligned} \] construct the fourth Taylor polynomial \(P_4(x)\) for \(g\) about \(a\): \[ \begin{aligned} P_4(x)&=g(a)+g'(a)(x-a)+\frac{g''(a)}{2!}(x-a)^2 \\ &+\frac{g'''(a)}{3!}(x-a)^3 + \frac{g^{(4)}(a)}{4!}(x-a)^4 \end{aligned} \]


An integrand that blows up at an endpoint defeats every rule so far, since they all evaluate \(f\) there. The integral may still exist as a limit: \[ \int_0^1 \frac{1}{x^{1/2}}\,dx = \lim_{t\to 0}\int_t^1 x^{-1/2}\,dx = \lim_{t\to 0}\left[\frac{x^{1/2}}{1/2}\right]_t^1 = \lim_{t\to 0} 2\left[1 - t^{1/2}\right] = 2 . \]

An improper integral: the value is the limit of proper integrals over [t,b] as t \to a.
Figure 4.18: An improper integral: the value is the limit of proper integrals over \([t,b]\) as \(t \to a\).
Improper Integrals with a Singularity

and write \[ \begin{aligned} \int_a^b f(x)\,dx = \int_a^b \frac{g(x)-P_4(x)}{(x-a)^p}dx +\int_a^b \frac{P_4(x)}{(x-a)^p}dx \end{aligned} \] The second integral can be computed exactly: \[ \begin{aligned} \int_a^b \frac{P_4(x)}{(x-a)^p}dx &= \sum_{k=0}^4 \int_a^b \frac{g^{(k)}(a)}{k!}(x-a)^{k-p}\,dx \\ &= \sum_{k=0}^4 \frac{g^{(k)}(a)}{k!(k+1-p)}(b-a)^{k+1-p} \end{aligned} \]


The trick is to subtract off the part that causes the trouble. Replace the smooth factor by its Taylor polynomial, integrate that piece exactly — the singularity is then only \(x^{-p}\) against a polynomial, which we can do in closed form — and handle the remainder numerically, where it is well behaved.

Example 4.13

\(\displaystyle\int_0^1 \frac{\cos x}{x^{1/4}}\,dx\).

Take the fourth Taylor polynomial of \(\cos\), \[ P_4(x) = 1 - \frac{x^2}{2} + \frac{x^4}{24}. \]

The exact piece. Integrating \(P_4(x)/x^{1/4}\) term by term, \[ \int_0^1 \frac{P_4(x)}{x^{1/4}}\,dx = \int_0^1\left[\frac{1}{x^{1/4}} - \frac{x^{2 - 1/4}}{2} + \frac{x^{4 - 1/4}}{24}\right]dx, \] and evaluating as a limit, \[ = \lim_{t\to 0}\left[\frac{x^{3/4}}{3/4} - \frac{x^{11/4}}{2\cdot\tfrac{11}{4}} + \frac{x^{19/4}}{24\cdot\tfrac{19}{4}}\right]_t^1 = \frac43 - \frac{4}{22} + \frac{4}{24\cdot 19} = \frac{485}{418} \approx 1.1603 . \]

The numerical piece. Define \[ G(x) = \begin{cases} \dfrac{\cos x - P_4(x)}{x^{1/4}} & 0 < x \le 1, \\[6pt] 0 & x = 0 . \end{cases} \] Composite Simpson with \(n = 4\), \(h = \tfrac14\) gives \[ \int_0^1 G(x)\,dx = \frac{0.25}{3}\left[G(0) + 4G\!\left(\tfrac14\right) + 2G\!\left(\tfrac12\right) + 4G\!\left(\tfrac34\right) + G(1)\right] = -0.000206 . \]

Together. \[ \int_0^1 \frac{\cos x}{x^{1/4}}\,dx \approx -0.000206 + \frac{485}{418} = 1.16008 . \]

Why is \(G(0) = 0\) the right value? Subtracting the Taylor polynomial removes every term through \(x^4\), so the numerator starts at \(x^6\): \[ \frac{\left(1 - \frac{x^2}{2} + \frac{x^4}{24} - \frac{x^6}{6!} + \cdots\right) - P_4(x)} {x^{1/4}} = \frac{-\frac{x^6}{6!} + \cdots}{x^{1/4}} \longrightarrow 0 \quad\text{as } x \to 0 . \] The subtraction has turned a singular integrand into one that is not merely finite at the origin but vanishing there — which is exactly what Simpson’s rule needs.

Improper Integrals with a Singularity

For the first integral, use the Composite Simpson’s rule to compute the integral of \(G\) on \([a,b]\), where \[ \begin{aligned} G(x)= \begin{cases} \frac{g(x)-P_4(x)}{(x-a)^p}, & \text{if }a<x\le b \\ 0, & \text{if }x=a \end{cases} \end{aligned} \] Note that \(0<p<1\) and \(P_4^{(k)}(a)\) agrees with \(g^{(k)}(a)\) for each \(k=0,1,2,3,4\), so \(G\in C^4[a,b]\) and Simpson’s rule can be applied.


Singularity at the Right Endpoint

Infinite limits of integration

Infinite Limits of Integration

An integral of the form \(\int_a^\infty \frac{1}{x^p}dx\), with \(p>1\), can be converted to an integral with left endpoint singularity at \(0\) by the substitution \[ \begin{aligned} t=x^{-1},\quad dt=-x^{-2}\,dx, \text{ so }dx=-x^2 dt=-t^{-2}dt \end{aligned} \] which gives \[ \begin{aligned} \int_a^\infty \frac{1}{x^p} dx = \int_{1/a}^0 -\frac{t^p}{t^2}dt=\int_0^{1/a}\frac{1}{t^{2-p}}dt \end{aligned} \] More generally, this variable change converts \(\int_a^\infty f(x)\,dx\) into \[ \begin{aligned} \int_a^\infty f(x)\,dx = \int_0^{1/a} t^{-2} f\left(\frac{1}{t}\right)\,dt \end{aligned} \]

An infinite interval is handled by the substitution \(t = 1/x\), which maps \([1,\infty)\) onto \((0,1]\).

Example 4.14

\(\displaystyle\int_1^{\infty}\frac{1}{x^2+9}\,dx\).

With \(t = 1/x\) we have \(dx = -\frac{1}{t^2}\,dt\), and the limits \(x = 1, \infty\) become \(t = 1, 0\). So \[ \int_1^{\infty}\frac{dx}{x^2+9} = \int_1^{0}\frac{1}{(1/t)^2 + 9}\left(-\frac{1}{t^2}\right)dt = \int_0^1 \underbrace{\frac{1}{1 + 9t^2}}_{f(t)}\,dt, \] where flipping the limits absorbed the minus sign and the \(1/t^2\) cancelled against the \((1/t)^2\). The new integrand is perfectly smooth on \([0,1]\), so composite Simpson with \(n = 4\) applies directly: \[ \int_0^1 f(t)\,dt = \frac{0.25}{3}\left[f(0) + 4f\!\left(\tfrac14\right) + 2f\!\left(\tfrac12\right) + 4f\!\left(\tfrac34\right) + f(1)\right] = 0.4112649 . \]