Numerical integration (source code)

= Numerical integration
{title2=$I(f)=\int_a^bf(x)\,dx$}
{wiki}

<Numerical integration> approximates a <definite integral> using finitely many sampled <function> values. For an <integrand> $f$ on a finite <interval> $[a,b]$, $a<b$, a <quadrature rule> has the form
$$
I(f)=\int_a^bf(x)\,dx,\qquad Q(f)=\sum_{j=1}^m w_jf(x_j)\approx I(f).
$$
The <quadrature nodes> $x_j$ tell us where to evaluate $f$; the <quadrature weights> $w_j$ turn those values into an approximation to the accumulated signed area. The exact <definite integral> depends on the <integrand> throughout the <interval>, whereas the <quadrature rule> sees only these samples. Integrating a chosen <interpolant> exactly still generally approximates the original <integrand>'s <integral>.<Fundamentals of Numerical Computation by Tobin A. Driscoll and others/5.6 Numerical integration>[Fundamentals of Numerical Computation by Tobin A. Driscoll and others; Definition 5.6.1 and §5.6.1, equations 5.6.2–5.6.5: weighted samples and integration of an interpolant]{ref}

The <fundamental theorem of calculus> evaluates $I(f)$ as $F(b)-F(a)$ when an <antiderivative> $F$ is available. <Numerical integration> is useful when no convenient <antiderivative> is known, or each evaluation of the <integrand> requires an expensive computation. The task then becomes choosing informative <quadrature nodes> and controlling the <quadrature error> with as few evaluations as possible. Fixed data also permit a <quadrature rule>, but refinement cannot supply new information between observations.<Fundamentals of Numerical Computation by Tobin A. Driscoll and others/5.6 Numerical integration>[Fundamentals of Numerical Computation by Tobin A. Driscoll and others; Opening discussion, Example 5.6.1 and the paragraph following Definition 5.6.1: antiderivatives, numerical evaluation and fixed data]{ref}

For an exact comparison, take the <polynomial> $f(x)=x^2$ on $[0,1]$. Its <antiderivative> $x^3/3$ gives $I(f)=1/3$. On a <partition of an interval> into $n\geq1$ equal panels, the <composite midpoint quadrature rule> uses $n$ midpoint samples, each with <quadrature weight> $1/n$; the <composite trapezoidal rule> uses $n+1$ endpoint samples, with outer <quadrature weights> $1/(2n)$ and interior <quadrature weights> $1/n$. The existing derivations under these two <quadrature rules> give
$$
M_n(f)=\frac13-\frac{1}{12n^2},\qquad T_n(f)=\frac13+\frac{1}{6n^2}.
$$
In particular, $M_1=1/4$ and $T_1=1/2$ already bracket $1/3$; two panels give $M_2=5/16$ and $T_2=3/8$. The <convex function> $x^2$ explains the opposite signs. A midpoint <rectangle> can cross the curve within its panel, so its total signed area, rather than pointwise ordering, determines the underestimate.

\Image[/quadrature-sample-weights-and-panels.png]
{title=One and two panels for <numerical integration> of $x^2$: midpoint and endpoint samples with their <quadrature weights>}

Define the signed <approximation error> as $E_n=Q_n(f)-I(f)$. In this example the <absolute errors> are $1/(12n^2)$ and $1/(6n^2)$: doubling $n$ reduces each by exactly four. A plot with both axes on a <logarithmic scale> therefore has slope $-2$, expressing order two with respect to panel refinement. This is an exact calculation for this <polynomial>, including $n=1$, rather than an observed rate extrapolated from a few computed answers.

\Image[/quadrature-quadratic-errors-and-slope.png]
{title=Exact <absolute errors> for midpoint and trapezoidal <numerical integration> of $x^2$: slope $-2$ and a factor-four reduction}

For a general <integrand>, the <composite midpoint quadrature rule> and <composite trapezoidal rule> give second-order <quadrature error> bounds when $f$ has two continuous <derivatives> on $[a,b]$ and a known bound on $|f''|$. These hypotheses describe behavior between samples. A formal error formula containing an unknown <derivative> is not a numerical accuracy certificate or a justified sample count. <Finite samples do not certify quadrature accuracy> makes the missing information explicit. Errors in the evaluations and <roundoff error> must also be considered; <noise sensitivity of a quadrature rule> treats the former.

More elaborate choices exploit other information: <Gaussian quadrature> obtains high <polynomial> exactness from carefully chosen <quadrature nodes>, and <Simpson's rule> uses a quadratic <interpolant>. Adaptive <numerical integration> compares approximations and concentrates samples where they disagree, often saving evaluations. Such a difference is an error estimate, not a certified bound without further assumptions: both approximations can miss the same feature.<Fundamentals of Numerical Computation by Tobin A. Driscoll and others/5.7 Adaptive integration>[Fundamentals of Numerical Computation by Tobin A. Driscoll and others; §5.7.1, equation 5.7.4: a difference-based error estimate; §5.7.2: interval bisection; Example 5.7.2 and concluding discussion: requested accuracy is not guaranteed]{ref}