5.4.24 · D4Scientific Computing (Python)

Exercises — Implementing numerical integration from scratch — trapezoidal, Simpson's

2,049 words9 min readBack to topic

Two pictures we will reuse — glance at them now so the words below have something to point at.

Figure — Implementing numerical integration from scratch — trapezoidal, Simpson's
Figure — Implementing numerical integration from scratch — trapezoidal, Simpson's

Level 1 — Recognition

Recall Solution

Simpson's stencil is 1-4-1 per pair. Chain three pairs across six slices.

  • Endpoints () belong to one parabola each → weight .
  • Odd-index nodes () are pair-centres (the peak the parabola bends to) → weight .
  • Even interior nodes () are shared between two adjacent parabolas → weight .

So the pattern is Check: the weights sum to , and recovers for a constant . ✓

Recall Solution

Points , so . Then . Simpson needs even; is even ✓ — allowed.


Level 2 — Application

Recall Solution

. Nodes and heights: Exact value: . Trapezoid overshoots (curve is convex, straight lids sit above it) by .

Recall Solution

Same nodes/heights as L2.1. Weights : This equals the exact exactly, because is degree and Simpson integrates all parabolas exactly.


Level 3 — Analysis

Recall Solution

Prediction: . Halving gives . So error should shrink by a factor of .

Check. With :

  • (), nodes , : Error .
  • (), nodes , : Wait—recompute the bracket carefully: ; interior sum , doubled ; total ; . Error .

Ratio . ✓ The prediction holds.

Recall Solution

Why: The error of fitting a parabola to a cubic comes from the leftover cubic term. On the symmetric interval the cubic error term is an odd function of , and the integral of an odd function over symmetric limits is . The Simpson nodes are symmetric, so the odd leftover integrates to exactly zero — the cubic term contributes nothing to the error. That is the "free" extra degree.

Confirm: true value (odd function). Simpson with , : Exact, as promised. ✓


Level 4 — Synthesis

Recall Solution

First get the two trapezoid values ( then ).

  • (, nodes , ):
  • () from L2.1.

Richardson combine: This matches exactly. Why it works: trapezoid error is . Halving scales the leading term by ; the weights and over are chosen precisely so the terms cancel, leaving a method accurate to — which is Simpson. This is one step of Richardson extrapolation; iterating it gives Romberg integration.

Recall Solution

, nodes , heights Simpson on (nodes , weights ): Bracket ; Trapezoid on (nodes ): Total . Error — much better than pure trapezoid, honestly handling the dangling slice.


Level 5 — Mastery

Recall Solution

Trapezoid exact: any straight line, e.g. on . Trapezoid is exact for degree-1 polynomials by construction (a straight lid is the function). Exact value ; with , . ✓ Simpson also gives but buys you nothing extra — there is no curvature to capture.

Error orders fail: take on . Its derivative blows up at , so the Taylor-series argument behind / (which needs bounded higher derivatives) breaks down. The observed convergence is slower than . Lesson: the advertised error orders assume is smooth enough (bounded 2nd derivative for trapezoid, 4th for Simpson). Kinks, cusps, and singularities void the guarantee — reach for scipy.integrate.quad's adaptive machinery there.

Recall Solution

Correct (weights ): . Exact ✓. Buggy (weights ): . Error from the bug — a wildly wrong answer ( too big). The extra weight on (where ) alone added . This is why endpoint weighting is the #1 silent bug: it doesn't crash, it just quietly returns garbage.

Recall Solution

(, nodes , ): Bracket ; Error — small but nonzero (degree exceeds Simpson's exact degree ). (): computing the weighted sum gives , error . Ratio . ✓ The order is confirmed.


Recall Self-test summary (cloze)

The slice width is ==. Simpson's per-pair stencil is 1-4-1, scaled by ==. Trapezoid error order ::: Simpson error order ::: Highest exact degree — trapezoid / Simpson ::: 1 / 3 Points for subintervals ::: Richardson combo giving Simpson from trapezoids :::

Connections