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.
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). \]
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.
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.
Using three nodes instead of two gives a better formula, by the same route: write down the quadratic interpolant and differentiate it.
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.
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.
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). \]
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 . \]
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.
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}. \]
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.
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). \]
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\).
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}. \]
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\).
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.
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 obvious way to improve accuracy is to use more nodes in a single rule. It does not work well.
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.
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} \]
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\).
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 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.
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\).
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 . \]
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.
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.
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]. \]
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})\).
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.
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.
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.
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.
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.
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.
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.
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.
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. ■
Nodes and weights are tabulated only for \([-1,1]\), so an affine change of variable does the rest.
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 . \]
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.
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\).
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.
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 on the same integral.
Using the two-point rule in each direction needs only four evaluations.
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.
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 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.
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 . \]
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.
\(\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.
An infinite interval is handled by the substitution \(t = 1/x\), which maps \([1,\infty)\) onto \((0,1]\).
\(\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 . \]