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

MethodSettingEstimate QEvaluationsAbsolute error |Q − I_ref|Relative error |Q − I_ref| / |I_ref|
Composite trapezoidaln = 81.97423160292.577e-21.288e-2
n = 161.993570344176.430e-33.215e-3
n = 321.998393361331.607e-38.033e-4
n = 641.999598389654.016e-42.008e-4
Composite Simpsonn = 82.0002691792.692e-41.346e-4
n = 162.000016591171.659e-58.296e-6
n = 322.000001033331.033e-65.167e-7
n = 642.000000065656.453e-83.227e-8
Gauss-Legendrem = 21.93581957526.418e-23.209e-2
m = 32.00138891431.389e-36.945e-4
m = 41.99998422841.577e-57.886e-6
m = 52.0000001151.103e-75.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

Composite trapezoidalComposite SimpsonGauss-Legendre
Absolute error against the supplied reference integral, on a logarithmic vertical axis, plotted against a linear count of the function evaluations each level cost. A level costing exactly zero evaluations is representable here; an exactly zero error cannot be drawn on the logarithmic axis and is read from the table.

Integrand and sample points

f(x)interval bounds a, bComposite trapezoidal · n = 64 (markers thinned)Composite Simpson · n = 64 (markers thinned)Gauss-Legendre · m = 5
f(x) = sin(x) on [0, 3.142], with the sample points of the final successful configuration of every enabled method. A method with no successful level contributes no nodes.

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

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.