5.4.24 · D5Scientific Computing (Python)

Question bank — Implementing numerical integration from scratch — trapezoidal, Simpson's

1,501 words7 min readBack to topic

Before you start, one shared vocabulary reminder so no symbol is unearned:


True or false — justify

Trapezoidal rule is always an overestimate.
False — it overestimates only when the curve is convex (bends up, like ), because the straight chord sits above the curve; for a concave curve the chord sits below and it underestimates.
Simpson's rule with more slices always beats Simpson's with fewer.
Mostly true but not guaranteed — smaller shrinks the term, yet for very rough or non-smooth the error constant can misbehave, and rounding error grows with more evaluations.
If a rule is exact for degree-2 polynomials it must be exact for degree-3.
True for Simpson specifically — its symmetric 1-4-1 stencil kills the odd (cubic) error term by cancellation, a free bonus, so it is exact through degree 3 despite only fitting a parabola.
Trapezoidal is a special case of Simpson's.
False — they are different Newton-Cotes members (Newton-Cotes formulas): trapezoid fits a degree-1 line, Simpson a degree-2 parabola. Neither contains the other.
Doubling roughly halves the trapezoidal error.
False — trapezoid error is , and doubling halves , so the error drops to about one quarter, not one half.
For a straight-line function , trapezoid and Simpson give identical, exact answers.
True — both are exact for degree-1 polynomials, so both return the true area with zero error regardless of .
Simpson's rule needs to be even because odd makes negative.
False — is positive for any positive ; the real reason is each parabola spans a pair of slices, so an odd count leaves one dangling slice with no partner.
The weights in composite trapezoid always sum to .
True — the pattern over nodes sums to , and , which is exactly the width the average height must multiply.
As every method converges to the true integral.
True in exact arithmetic — both are Riemann-type sums (Riemann sums) whose limit is the definite integral; in floating point, rounding eventually stops improvement.

Spot the error

A student sets Simpson weights to . What's wrong?
Only the odd-index (pair-centre) nodes get weight 4; even interior nodes get weight 2 because they are shared between two adjacent parabolas. Blanket-4 double-counts the shared ones.
A student writes for i in range(1, n+1) to sum interior trapezoid nodes.
This includes node (the right endpoint) as an interior point, adding it twice and mis-weighting it — interior nodes are range(1, n) only, since points number but interior points number .
Someone claims can't be exact for Simpson because it only fits parabolas.
Wrong — the fitted curve is a parabola, but Simpson's integration weights make the cubic error term cancel by symmetry, so the answer is exact for cubics anyway.
A program computes trapezoid as sum(f(a+i*h) for i in range(n+1)) * h.
It weights every node by 1, but endpoints should get . The fix is subtract half of , or equivalently start the sum at then add interior nodes.
Someone integrates over with and reports a positive area.
With , is negative, so the sum naturally comes out negative — the correct signed result; forcing it positive would contradict .
A student uses Simpson with and "just averages the leftover slice."
is odd; the clean 1-4-1 stencil can't tile three slices. You must either pick even or apply a separate rule (e.g. a Simpson or a trapezoid) to the odd trailing slice — not merely average.

Why questions

Why do endpoints get a different weight from interior nodes?
Each endpoint belongs to only one trapezoid/parabola, while each interior node is shared between two adjacent shapes, so interior nodes are counted twice (trapezoid) or take the shared weight 2 (Simpson).
Why does Simpson's rule need points in pairs of slices rather than single slices?
A parabola is determined by three points, and three consecutive nodes span two slices; you can't fit one parabola over a single slice's two points.
Why is Simpson's error while trapezoid is only ?
Taylor series expansion of the error shows the leading term of trapezoid involves times , while Simpson's symmetric stencil cancels the and terms, leaving times .
Why does trapezoid overestimate a convex (upward-bending) curve?
The straight chord joining two points on a convex curve lies above the curve everywhere between them, so the trapezoid area exceeds the true area under the curve.
Why is Simpson exact for but not for ?
Simpson is exact up to degree 3; is degree 4, so its -driven error term is nonzero and the rule leaves a residual.
Why can we combine trapezoid results to build Simpson?
Richardson extrapolation cancels the term by combining and in the ratio that eliminates it; the result is algebraically identical to Simpson, which is why halving and extrapolating works.
Why phrase both rules as "weighted sums "?
It reveals that all Newton-Cotes rules (Newton-Cotes formulas) share one skeleton and differ only in weights, so coding them means changing a weight table, not the loop structure.

Edge cases

What does trapezoid give for ?
The single-trapezoid estimate — one straight chord across the whole interval, usually crude but exact if is linear.
What is the smallest valid for Simpson, and why?
— exactly one pair of slices, three nodes, one parabola; is impossible because you can't fit a parabola through only two points.
What happens if has a sharp corner (not smooth) inside ?
Both error estimates assume smoothness ( or exists); at a corner those derivatives blow up, so the promised orders fail and convergence slows — place a node at the corner to recover accuracy.
What if (zero-width interval)?
, every slice collapses, and the sum is — correct, since for any .
What if dips below the -axis somewhere?
The rules return a signed area: regions below the axis contribute negative height , so the result matches the true signed integral, not the geometric area.
What if you feed Simpson an odd in code?
A correct implementation raises an error (as in the parent's raise ValueError); a careless one silently mis-weights the last node, producing a wrong answer that looks plausible — the most dangerous kind of bug.
What is the limiting behaviour of both rules as ?
Both converge to the exact in exact arithmetic; in floating point, rounding error eventually dominates and further increasing worsens the result.

Recall One-line self-test before moving on

If you can answer "why do the edges count differently?" and "why does even matter for Simpson?" in your own words without peeking, you own this topic. Otherwise re-read the Newton-Cotes formulas and Polynomial interpolation links.

Connections

  • Riemann sums — the limiting sum both rules approximate.
  • Newton-Cotes formulas — the family these two rules open.
  • Polynomial interpolation — why Simpson = integrating the fitted parabola.
  • Taylor series — where the / orders come from.
  • Richardson extrapolation — combining trapezoids to build Simpson / Romberg.
  • scipy.integrate.quad — the adaptive production tool that hides all this.