Skip to article frontmatterSkip to article content

Adaptive quadrature

from pylab import *

The error in some numerical quadrature depends on the number of nodes used which the user has to select. It is desirable to have an automatic algorithm where the user specifies the error ε that is acceptable and the algorithm should select the number of nodes nn so that

I(f)In(f)ϵ|I(f) - I_n(f)| \le \epsilon

The error clearly depends on the smoothness of the integrand ff also. If ff has a singularity, e.g., f(x)=xf(x) = \sqrt{x} has singular derivatives at x=0x=0, then the error will be large. To have an acceptable error we must use a large nn (small hh). But this will increase the number of nodes. Actually, most of the error will come from the region near the singularity and we can use a small hh only in those regions, and not everywhere. This requires the ability to use non-uniformly spaced nodes in the numerical integration formula, and all the composite rules allow us to do this.

1Adaptive Simpson quadrature

Let nn be even and we use n+1n+1 points. The composite Simpson quadrature is

In(f)=j=1n/2(x2jx2j26)(f2j2+4f2j1+f2j),x2j1=12(x2j2+x2j)I_n(f) = \sum_{j=1}^{n/2} \left( \frac{x_{2j} - x_{2j-2}}{6} \right)( f_{2j-2} + 4 f_{2j-1} + f_{2j}), \qquad x_{2j-1} = \half(x_{2j-2} + x_{2j})

with error formula

I(f)In(f)=12880j=1n/2(x2jx2j2)5f(4)(ξj),ξj[x2j2,x2j]I(f) - I_n(f) = -\frac{1}{2880} \sum_{j=1}^{n/2}(x_{2j} - x_{2j-2})^5 f^{(4)}(\xi_j), \qquad \xi_j \in [x_{2j-2}, x_{2j}]

Note that we have a decomposition of the total error into error contributions from each of the sub-intervals. If f(4)f^{(4)} is large in some sub-interval, we should choose the length of that sub-interval x2jx2j2x_{2j} - x_{2j-2} to be small, in order to reduce the error. The best strategy is to equi-distribute the error among all the sub-intervals. Define

Mj=maxx[x2j2,x2j]f(4)(x)M_j = \max_{x \in [x_{2j-2},x_{2j}]} |f^{(4)}(x)|

then

I(f)In(f)12880j=1n/2(x2jx2j2)5Mj|I(f) - I_n(f)| \le \frac{1}{2880} \sum_{j=1}^{n/2}(x_{2j} - x_{2j-2})^5 M_j

We want

I(f)In(f)ϵ|I(f) - I_n(f)| \le \epsilon

This can be achieved if

12880(x2jx2j2)5Mj2ϵn,j=1,2,,n/2\frac{1}{2880} (x_{2j} - x_{2j-2})^5 M_j \le \frac{2 \epsilon}{n}, \qquad j=1,2,\ldots,n/2

If this condition is not satisfied, then we iteratively refine the sub-intervals until desired error level is reached.

1.1Algorithm I

We will assume that we can estimate MjM_j and hence we can estimate the error. The algorithm will iteratively increase the number of sub-intervals until the desired error level is reached.

  1. Start with three nodes, i.e., one Simpson rule, and compute I2(f)I_2(f) and estimate the error. If I(f)I2(f)ϵ|I(f) - I_2(f)| \le \epsilon, then we stop and the answer is I2(f)I_2(f).

  2. Otherwise, insert a point mid-way between the three points and use two Simpson rules. Estimate the error from each rule. If both errors are ϵ/2\le \epsilon/2, then stop.

  3. If not, suppose first error >ϵ/2> \epsilon/2 and second error ϵ/2\le \epsilon/2. Refine the first Simpson rule by inserting two points.

  4. Now the error from the first two rules must be ϵ/4\le \epsilon/4. If this is true, then we stop.

1.2Algorithm II

We may not be able to estimate the derivatives, and the only information available is the function values. We can approximate the derivatives by first constructing a local approximation and differentiating it. But we have seen other ways to estimate the error in integral, e.g., using Richardson extrapolation.

Set α=a\alpha=a and β=b\beta=b and define

Iα,β=αβf(x)dxI_{\alpha,\beta} = \int_\alpha^\beta f(x) \ud x

The Simpson rule is

Iα,β(1)=h3[f(α)+f(α+β2)+f(β)],h=12(βα)I_{\alpha,\beta}^{(1)} = \frac{h}{3}\left[f(\alpha) + f\left(\frac{\alpha+\beta}{2}\right) + f(\beta) \right], \qquad h = \half (\beta - \alpha)

To estimate the error, we compute a refined estimate as

Iα,β(2)=Iα,γ(1)+Iγ,β(1),γ=12(α+β)I_{\alpha,\beta}^{(2)} = I_{\alpha,\gamma}^{(1)} + I_{\gamma,\beta}^{(1)}, \qquad \gamma = \half(\alpha + \beta)

and use Iα,β(2)Iα,β(1)|I_{\alpha,\beta}^{(2)} - I_{\alpha,\beta}^{(1)}| as the error estimate.

  1. If Iα,β(2)Iα,β(1)ϵ|I_{\alpha,\beta}^{(2)} - I_{\alpha,\beta}^{(1)}| \le \epsilon then keep interval [α,β][\alpha,\beta] and use Iα,β(2)I_{\alpha,\beta}^{(2)} as estimate of Iα,βI_{\alpha,\beta}.

  2. Otherwise, divide [α,β][\alpha,\beta] into [α,γ][\alpha,\gamma] and [γ,β][\gamma,\beta]. Now we want to satisfy

    Iα,γ(2)Iα,γ(1)ϵ2,Iγ,β(2)Iγ,β(1)ϵ2|I_{\alpha,\gamma}^{(2)} - I_{\alpha,\gamma}^{(1)}| \le \frac{\epsilon}{2}, \qquad |I_{\gamma,\beta}^{(2)} - I_{\gamma,\beta}^{(1)}| \le \frac{\epsilon}{2}
  3. If both conditions are satisfied, then the integral is Iα,γ(2)+Iγ,β(2)I_{\alpha,\gamma}^{(2)} + I_{\gamma,\beta}^{(2)}.

  4. Otherwise, suppose

    Iα,γ(2)Iα,γ(1)>ϵ2,Iγ,β(2)Iγ,β(1)ϵ2|I_{\alpha,\gamma}^{(2)} - I_{\alpha,\gamma}^{(1)}| > \frac{\epsilon}{2}, \qquad |I_{\gamma,\beta}^{(2)} - I_{\gamma,\beta}^{(1)}| \le \frac{\epsilon}{2}

    Then we keep Iγ,β(2)I_{\gamma,\beta}^{(2)} and divide [α,γ][\alpha,\gamma] into [α,δ][\alpha,\delta] and [δ,γ][\delta,\gamma] with δ=12(α,γ)\delta=\half(\alpha,\gamma). Now try to satisfy

    Iα,δ(2)Iα,δ(1)ϵ4,Iδ,γ(2)Iδ,γ(1)ϵ4|I_{\alpha,\delta}^{(2)} - I_{\alpha,\delta}^{(1)}| \le \frac{\epsilon}{4}, \qquad |I_{\delta,\gamma}^{(2)} - I_{\delta,\gamma}^{(1)}| \le \frac{\epsilon}{4}

    If both conditions are satisfied, then integral is Iα,δ(2)+Iδ,γ(2)+Iγ,β(2)I_{\alpha,\delta}^{(2)} + I_{\delta,\gamma}^{(2)} + I_{\gamma,\beta}^{(2)}.

  5. Continue this process until the errors in all sub-intervals is less than the tolerance.