Lab · Engineering Mathematics
Numerical Integration Lab
Integrate one definite integral with the composite trapezoidal rule, composite Simpson's rule and Gauss-Legendre quadrature at once, refine each of them, and read accuracy against the number of function evaluations it cost.
Problem
Operators + − * / ^, parentheses, pi and e, and sin, cos, tan, asin, acos, atan, exp, sqrt, abs, ln, log, log10. Write 2*x, not 2x.
Errors below are measured against this value, which is taken exactly as entered.
Methods and refinement
Composite rules double n at each level; Gauss-Legendre steps up to the chosen maximum order. A composite base setting is validated before any sequence is built: a fractional n, or an odd n for Simpson, is reported and nothing is run. One sweep may spend at most 40,000 evaluations in total; each level reserves its worst-case cost before f is touched, so a level that fails late still consumed the work it did and the cap can never be exceeded. A zero-width interval costs nothing at all and reserves nothing. Drawing the sample points of the final successful configuration re-evaluates that one configuration again, outside this cap, for display only.
Presets
Comparison
| Method | Setting | Estimate Q | Evaluations | Absolute error |Q − I_ref| | Relative error |Q − I_ref| / |I_ref| |
|---|---|---|---|---|---|
| Composite trapezoidal | n = 8 | 1.974231602 | 9 | 2.577e-2 | 1.288e-2 |
| n = 16 | 1.993570344 | 17 | 6.430e-3 | 3.215e-3 | |
| n = 32 | 1.998393361 | 33 | 1.607e-3 | 8.033e-4 | |
| n = 64 | 1.999598389 | 65 | 4.016e-4 | 2.008e-4 | |
| Composite Simpson | n = 8 | 2.00026917 | 9 | 2.692e-4 | 1.346e-4 |
| n = 16 | 2.000016591 | 17 | 1.659e-5 | 8.296e-6 | |
| n = 32 | 2.000001033 | 33 | 1.033e-6 | 5.167e-7 | |
| n = 64 | 2.000000065 | 65 | 6.453e-8 | 3.227e-8 | |
| Gauss-Legendre | m = 2 | 1.935819575 | 2 | 6.418e-2 | 3.209e-2 |
| m = 3 | 2.001388914 | 3 | 1.389e-3 | 6.945e-4 | |
| m = 4 | 1.999984228 | 4 | 1.577e-5 | 7.886e-6 | |
| m = 5 | 2.00000011 | 5 | 1.103e-7 | 5.514e-8 |
Absolute error is |Q − I_ref|; the relative error is the same quantity divided by |I_ref|, a dimensionless ratio.
Error against cost
Integrand and sample points
Every node was used in the sums; a dense partition draws an evenly spaced subset of markers for legibility only. Marker thinning never changes a numerical result, and reconstructing these nodes for the plot is not counted as quadrature cost.
What this Lab models
One proper definite integral of a scalar function over a finite interval, attacked by three rules of very different character: a second-order composite rule, a fourth-order composite rule, and a rule that places its points optimally. The Lab exists to make the trade-off visible — accuracy is read against the number of times f had to be evaluated, which is the only cost that matters when f is expensive.
Model scope
- · Finite interval, proper integral, scalar integrand. Infinite bounds and unbounded integrands are out of scope.
- · a > b is integrated in the orientation given and returns the correct negative value; a = b is exactly zero.
- · A non-finite value at any required node is reported as a failure for that level, never skipped.
- · Composite rules refine by doubling n; Gauss-Legendre refines by raising the order, up to 8.
- · Simpson requires an even n at every level and rejects an odd one rather than rounding it.
- · With no reference integral, successive differences are reported as refinement differences — a convergence indicator, not an error.
Related calculations
- Trapezoidal Rule Calculator
- Simpson's Rule Calculator
- Gaussian Quadrature Calculator
- Numerical Derivative Calculator
Reading the presets
- · sin(x) · [0, π] — ∫₀^π sin(x) dx = 2 exactly. A smooth, well-scaled benchmark: Simpson should beat the trapezoidal rule by orders of magnitude at the same cost, and a low-order Gauss rule beats both.
- · exp(x) · [0, 1] — ∫₀¹ eˣ dx = e − 1 ≈ 1.718281828459045. Analytic everywhere, so every nominal convergence order is genuinely attained.
- · 1/(1+x²) · [0, 1] — ∫₀¹ dx/(1+x²) = π/4 ≈ 0.7853981633974483. Smooth but with a nearby complex singularity, which slows Gauss convergence compared with eˣ.
- · sqrt(x) · [0, 1] — ∫₀¹ √x dx = 2/3 ≈ 0.6666666666666666. Finite, but the derivative blows up at x = 0: the integrand is not smooth there, so the O(h²) and O(h⁴) error statements no longer apply and the observed rates fall short.