Project 29: Numerical Integration and Area Estimator
A self-contained deep-dive from the canonical Math from Foundations to Machine Learning curriculum.
- Difficulty: Intermediate
- Time: 1-2 weeks
- Language: Python with NumPy and Matplotlib
- Prerequisites: Project 26
- Merged from:
HS P08;ML-Math P10
What You Will Build
Build an integration visualizer that estimates definite integrals using left/right Riemann sums, midpoint rectangles, trapezoids, and Simpson’s rule, then compares their convergence as the partition count grows. Shade signed area above and below the axis and keep signed integral separate from geometric area. Allow piecewise interval splitting around discontinuities and add a simple adaptive refinement mode that subdivides regions whose coarse and fine estimates disagree. Test against functions with known antiderivatives, then use the engine on distance-from-velocity and probability-density examples. Every result includes method, interval, evaluation count, estimated value, reference error when available, and a convergence table.
Real World Outcome
For a selected function and bounds, the app draws the curve and chosen rectangles/trapezoids, animates refinement, and plots error versus evaluation count for each method. It can demonstrate cancellation across the x-axis, reject an improper singular interval unless explicitly handled, and export a reproducible comparison report.
Core Question
How does accumulated local contribution become area or total change, and how can refinement provide evidence that a numerical integral is reliable?
Concepts You Must Understand First
- Definite integral: the limit of signed sums represents accumulation. See Stewart, Calculus, integration chapters.
- Quadrature rules: midpoint, trapezoid, and Simpson methods approximate local shape differently.
- Signed versus geometric area: negative contributions cancel in an integral but not in total geometric area.
- Convergence and error: comparing n and 2n is evidence, not a proof, but exposes instability.
- Adaptive subdivision: spend evaluations where curvature or irregularity makes local error larger. See Press et al., Numerical Recipes.
Build Milestones
- Implement equal-width Riemann, midpoint, and trapezoid sums with shared interval validation.
- Add Simpson’s rule, enforcing its even-subinterval requirement.
- Visualize partitions and maintain distinct signed-integral and geometric-area modes.
- Generate convergence/error plots on polynomial, trigonometric, and exponential references.
- Implement recursive or iterative adaptive refinement with evaluation and depth limits.
Hints in Layers
- Verify constants and linear functions first; trapezoids should integrate linear functions exactly up to rounding.
- Cache function evaluations so adaptive refinement does not repeatedly sample the same points.
- Split at known discontinuities and domain boundaries rather than allowing one panel to cross them blindly.
Common Pitfalls and Debugging
- Symptom: area becomes unexpectedly small. Cause: positive and negative regions canceled. Fix: label signed integral and optionally integrate
|f|for geometric area. - Symptom: Simpson’s output is wrong only for some n. Cause: an odd subinterval count. Fix: reject or normalize n explicitly.
- Symptom: refinement never stabilizes near a singularity. Cause: the method assumes a bounded regular integrand. Fix: detect non-finite samples, split, transform, or report unsupported improper integration.
Definition of Done
- Four quadrature methods match known test integrals within expected tolerances.
- Visual partitions correspond exactly to the computed samples and weights.
- Signed and geometric area are clearly distinguished.
- Convergence plots compare error against evaluation cost.
- Adaptive mode has tolerance, evaluation, and recursion limits.
- Discontinuous and non-finite functions fail with actionable diagnostics.
Navigation
Previous: Project 28 · Complete learning path · Next: Project 30