1 Introduction

Consider the numerical approximation of the definite integral

$$ I := {{\int}_{0}^{L}} f(x)dx $$
(1)

of a smooth function f by sampling it at equispaced points (or nodes) xk := kh, k = 0,…,n, h = L/n, fk := f(xk), and evaluating the composite trapezoidal rule

$$T_{f}(h) := h\bigg(\frac{f(x_{0})}{2} + \sum\limits_{k=1}^{n-1} f(x_{k}) + \frac{f(x_{n})}{2}\bigg).$$

In this work, we abide by the signal processing (Wikipedia) definition of a sample as a value at a point in time or space. A sample vector is a vector of equispaced samples.

It is well known that the accuracy of Tf(h) depends on the values of the derivatives of odd orders of f at the extremities 0 and L. In the favorable case where all of those coincide at these endpoints, the convergence is faster than every power of h, i.e., spectral. If f, L-periodically extended on both sides of the interval [0,L], is analytic in a horizontal strip containing , the convergence even becomes exponential. A survey [19] about Tf(h) has been published in 2014.

In many numerical analysis courses, students learn that the difference between the (composite) trapezoidal rule and the exact integral is given by the Euler–Maclaurin formula [2, 13].

Theorem 1 (Euler–Maclaurin formula for the trapezoidal rule)

Let fC2m+ 2[0,L] for some m ≥ 0. Then, for every nand \(h:= \frac {L}{n}\), the error of the trapezoidal rule may be written as follows:

$$ T_{f}(h) - I = a_{2}h^{2} + a_{4}h^{4} + {\ldots} + a_{2m}h^{2m} + L\frac{B_{2m+2}}{(2m+2)!}f^{(2m+2)}(\xi)h^{2m+2} $$
(2)

for some ξ ∈ [0,L], where

$$a_{2j} := \frac{B_{2j}}{(2j)!}[f^{(2j-1)}(L) - f^{(2j-1)}(0)].$$

Here B denotes the th Bernoulli number.

There are at least two ways of deriving Tf(h). The customary one is to consider I as the sum of the integrals over the subintervals [xk,xk+ 1] and to replace each of these integrals with the area of the trapezoid with bases f(xk) and f(xk+ 1) and height h. But one may also see Tf(h) (up to a constant depending on the length of the interval) as the exact integral of the trigonometric polynomial of minimal degree which interpolates the function values (f(x0) + f(xn))/2, f(x1), …, f(xn− 1) at the nodes x0,…,xn− 1 [1, 2]. That way, formula (2) is a quantitative description of the influence of the jump at 0 ≡ L (mod L) of the L-periodically extended function f (on both sides of [0,L]) on the accuracy of the exact integral Tf(h) of that trigonometric polynomial as an approximation of I.

In the present work, we shall address the following problem: we assume that a vector (in Section 7 a few vectors) of equispaced samples of a piecewise smooth function is (are) given, together with the location of its jumps. As usual in quadrature by extrapolation, we shall first assume that the number of subintervals is some power of two (or an odd number times such a power). It turns out that a large number of nodes are required in many cases. We therefore also consider the case where several vectors of samples can be taken.

In Section 2, we recall the Euler–Maclaurin formula for functions with jumps, the basis of the extrapolation method presented in this work. The method is described for the case of a single interior jump in Section 3, and an example is given in Section 4. The next section introduces the general method, of which examples are given in Section 6. Up to that point, we use a single vector of samples; Section 7 extends the method to the case where several vectors of samples can be obtained, which can greatly reduce the total number of function evaluations required to achieve a prescribed level of accuracy, and gives corresponding examples. A few remarks conclude the paper.

2 The Euler–Maclaurin formula for functions with jumps

According to the circular interpretation mentioned above, the L-periodized f usually has a jump at c0 := 0 ≡ L (mod L). Nevertheless, when we talk about jumps in the present work, we mean the usual concept, i.e., that f has interior jumps, i.e., jumps at some points cj, 0 < cj < L, j = 1,…,J. We assume that these cj are distinct and ordered according to their index, and that f is continuous on every interval (cj,cj+ 1) between two consecutive jumps, and possesses one-sided limits \(f(c_{j}^{-})\) and \(f(c_{j}^{+})\) at cj, j = 0,1,…,J − 1. Moreover, the value of f at every jump is changed, if necessary, to the average (f(cj−) + f(cj+))/2 of the two corresponding limits there.

As in [2], we consider the classic Riemann sum generalization of Tf(h) with “offset” t, where 0 ≤ t < 1,

$$ R_{f}(h) := h \sum\limits_{k=0}^{N-1} f\big((k+t)h\big) = h \sum\limits_{k=1}^{N} f\big((k-1+t)h\big). $$
(3)

For t = 0, we have Rf(h) = Tf(h); for \(t=\frac {1}{2}\), this is the midpoint rule.

Let us recall the following theorem of [2], which will be the basis of our extrapolation procedure. The result was already given by Lyness in [14] (see the discussion in [2, p. 383]).

Theorem 2

(Euler–Maclaurin formula for Riemann sums of piecewise smooth functions) Let f be piecewise Cm− 1[0,L], and pick a t, 0 ≤ t < 1. Let cj denote the abscissae of the J + 1 jumps of f and, for each of them, \(t_{j} := t - \frac {c_{j}}{h} \mod 1\), j = 0,…,J.

If f(m) is absolutely integrable between every two consecutive cj, then the integration error may be written as follows:

$$ R_{f}(h) - I = a_{1} h + \sum\limits_{\ell=2}^{m} a_{\ell} h^{\ell} - \frac{h^{m}}{m!} {{\int}_{0}^{L}} f^{(m)}(x) \sum\limits_{j=0}^{J} \bar P_{m}(t_{j} - \frac{x}{h}) dx $$
(4)

with

$$ a_{1} := \sum\limits_{j=0}^{J} \gamma_{j} P_{1}(t_{j})\left[f(c_{j}-) - f(c_{j}+)\right],\qquad \gamma_{j} := \begin{cases}0,&t_{j}=0,\\ 1,&t_{j}\ne 0, \end{cases} $$
(5)

and

$$ a_{\ell} := \sum\limits_{j=0}^{J} \frac{P_{\ell}(t_{j})}{\ell!}\left[f^{(\ell-1)}(c_{j}-) - f^{(\ell-1)}(c_{j}+)\right], $$
(6)

where P is the Bernoulli polynomial of degree and \(\bar P_{\ell }\) its continuous 1-periodic extension.

In the sequel, we shall refer to formula (4) by the acronym EMF.

The change of variable y = L(xa)/(ba) shows that the polynomial part of Rf(h) − I is the same when f is defined on an arbitrary interval [a,b] instead of [0,L]; the latter only makes the formulas and the proofs simpler to read.

In the exceptional case where the values of the derivatives are known on both sides of the jumps, substituting them into (4) easily permits the improvement of the Riemann values (3) (see [2]). One can also correct the endpoint weights in Tf(h) to increase its order [8].

The number tj is the relative distance of cj to the node following it. In the rather exceptional case where the values of h are such that tj = 1/2 (or constant) for every h, one can directly apply Richardson–Romberg (see Example 1 of [2], where the only interior jump is at the midpoint of the interval of integration). But this is atypical and, usually, the tj depend on h, so that the factors a of the powers of h in the EMF are not constants as they depend on values of the Bernoulli polynomials at the tj.

3 Extrapolation via a system of equations

Extrapolation eliminates the first powers of h on the right-hand side of the EMF without knowledge of the a. Assume that a quantity I is approximated by a formula B(h), and that the latter may be written as follows:

$$ B(h) = I + a_{1} h + a_{2} h^{2} + {\ldots} + a_{p}h^{p} + S_{p}(h) = C_{p}(h) + S_{p}(h) $$
(7)

with \(S_{p}(h) = \mathcal {O}(h^{p+1})\). Here B(h) = Rf(h) as given in (3). One method for successively eliminating the first powers of h proceeds by linear combination of values of B(h) for different arguments h; another solves a system of equations, and this is what we shall use here.

Suppose B(h0), B(h1),…,B(hp) have been evaluated for a decreasing sequence of hk monotonely approaching 0. The system of equations with unknowns \(\widetilde {I}\) and \(\widetilde a_{\ell }\)

$$ \widetilde{I} + h_{k}\widetilde a_{1} + {h_{k}^{2}}\widetilde a_{2} + \ldots + {h_{k}^{p}}\widetilde a_{p} = B(h_{k}),\qquad k=0,\ldots,p,$$

determines the polynomial Qp(h) of degree at most p

$$Q_{p}(h) = \widetilde{I} + \widetilde a_{1} h + \widetilde a_{2} h^{2} + \ldots + \widetilde a_{p}h^{p}$$

interpolating the values B(hk), k = 0,…,p.

Recall that the Lebesgue function λp(h) is defined as follows:

$$ \lambda_{p}(h) = \sum\limits_{k=0}^{p} | \ell_{k}(h) |, $$
(8)

in which the k are the Lagrange fundamental polynomials

$$\ell_{k} (h) = \frac{{\prod}_{j=0, j \not= k}^{p} (h - h_{j})}{{\prod}_{j=0, j \not=k }^{p} (h_{k} - h_{j})}. $$

The Lebesgue function is the norm of the interpolation operator and indicates the conditioning of the interpolant (see for example [20]).

Theorem 3

Let the function B(h) of (7) be interpolated in the interval [0,h0] by the polynomial Qp(h) described above, and let the Lebesgue function λp(h) be \(\mathcal {O}\big (g(h)\big )\) near 0, for some function g(h). Then

$$ \widetilde{I} - I = \mathcal{O}\big(g(h)h^{p+1}\big). $$
(9)

Proof

Obviously, \(\widetilde {I} - I = Q_{p}(0) - C_{p}(0)\). In view of the linearity and uniqueness of polynomial interpolation, the polynomial part Cp(h) of B(h) is interpolated exactly by Qp(h), so that Qp(h) − Cp(h) is the interpolant of Sp(h). But, according to a well-known result about the perturbation of the polynomial interpolant [3, 7],

$$|Q_{p}(h) - C_{p}(h)| \le \lambda_{p}(h)|S_{p}(h)|.$$

Letting h → 0 in this expression yields (9). □

With a good set of interpolation nodes {hk}, such as Chebyshev or Legendre points, one has \(\lambda _{p}(h) = \mathcal {O}(\log p)\) [6], so that the error becomes \(\mathcal {O}(h^{p+1}\log p)\). Apart from that of the Chebyshev points of the first kind, we do not know bounds on the Lebesgue function for sets of nodes which accumulate toward a point outside of the interpolation interval, such as 0 here, but our computations with the geometrically decreasing sequence of the first example below demonstrate that its growth at 0 is indeed very slow in comparison with that of 1/h.

It remains to compute \(\widetilde {I} = Q_{p}(0)\). Since it is the value at a single point of the interpolating polynomial Qp, one usually uses Neville’s algorithm [17], which is stable in the context of numerical quadrature, where the h–values hk cluster toward the interpolation point 0 and the coefficients a do not depend on hk. However, in our case, they do (through the tj). General Neville–Aitken type interpolation algorithms going into that direction have been given in the literature. According to the MathSciNet review of [11], Schneider [16] introduced the first one in a special case, before Mühlbach [15] solved the general problem. Brezinski [4] and Håvie [11] later gave different proofs. These same authors [5, 10] also independently derived a general extrapolation algorithm, which Brezinski called the E-algorithm. But none of these articles contains much in terms of numerical experiments demonstrating the stability of the methods, if any; moreover, none seems to have been used to treat functions with interior singularities.

Here we use the (simpler) matrix version (corresponding to the Vandermonde solution to the interpolation problem) and solve the resulting system of linear equations by means of Gaussian elimination.

4 A first example with one jump

As a test and an example illustrating the method, we have started with a function which numerically has no jump at the extremities of the interval and only one in the interior,

$$ f(x) := 2 e^{-\eta(2x-1)^{2}}g(x), $$
(10)

with

$$ g(x) := \begin{cases} \cos(\beta x),&0\le x\le c_{1},\\ e^{x-c_{1}},&c_{1}< x <1. \end{cases} $$
(11)

f is plotted for \(c_{1} = 1/\sqrt {3}\), β = 2, and η = 35 in Fig. 1. Here and in the subsequent figures, a small circle at a jump cj marks the value (f(cj−) + f(cj+))/2 used in the trapezoidal sum when cj is one of the nodes.

Fig. 1
figure 1

Function f from (10) with \(c_{1} = 1/\sqrt {3}\), β = 2 and η = 35 on [0,1]

We limit our computations to the case t = 0, i.e., Rf(h) = Tf(h), the composite trapezoidal rule. As the sequence of hk-values, we have taken the geometric sequence hk = 2k, as in classic Richardson extrapolation [12, p. 100].

As a means of comparison, we have first applied Richardson extrapolation to the problem of computing (1) with L = 1, \(c_{1} = 1/\sqrt {3}\) (this choice guarantees that the jump at c1 does not coincide with an integration node for any h) and β = 2. η is introduced so that all derivatives of f numerically vanish at the extremities 0 and 1; hence, from a practical point of view, only one jump is present, namely at c1: we chose η = 35.

We have used two calls of the MATLAB quad–routine, one on each subinterval [0,c1] and [c1,1], to obtain an accurate approximate value of (1) with which to compare our results: I ≈ 0.180560634293184. One could also use Chebfun with “splitting on.” (We note, however, that these algorithms are no competitors to our method, as we assume that f is given by one vector of equispaced samples only.) Our results are summarized in Table 1. The first column contains the sequence of hk-values, the second the error of the trapezoidal value for hk without extrapolation, and the third the error in the Richardson-extrapolated value (the value at zero of the polynomial of the lowest degree interpolating the trapezoidal values up to hk, computed here with the polyfit routine of MATLAB). The last column shows the error \(\widetilde {I} - I\) with the extrapolation method presented below. Every row contains the results when the given samples are those corresponding to the value hk in the first column, and the extrapolation makes use of all the trapezoidal values up to this hk.

Table 1 Example with one interior jump and no jump at the extremities

The trapezoidal values decrease, but not monotonically: this is to be expected, as the relative location of the jump in the interval between the two nodes enclosing it varies with h (see also the values of t1(h) in the next tables).

The Richardson values do not improve on the direct trapezoidal ones. This is hardly surprising, as the main part of the Euler–Maclaurin formula is no polynomial in h, because of the tj.

Let us now write down the extrapolation equations of the suggested method. Since there is no jump at the extremities, f(− 1)(c0−) = f(− 1)(c0+) for all , so that the sums in the a start with j = 1 (t0 = 0 is present only when there is a jump at the extremities). We denote by

$$d_{1}^{(\ell)} := f^{(\ell-1)}(c_{1}-) - f^{(\ell-1)}(c_{1}+),\qquad \ell=1,2,\ldots,$$

the differences of derivatives of f at the jump c1, which are not known. Then, according to (4), the trapezoidal value equals

$$ \begin{array}{@{}rcl@{}} \qquad T_{f}(h) &=& I + \gamma_{1} P_{1}(t_{1}(h)) d_{1}^{(1)}h + \frac{P_{2}(t_{1}(h))}{2!}d_{1}^{(2)}h^{2} + \frac{P_{3}(t_{1}(h))}{3!}d_{1}^{(3)}h^{3} + \ldots\\ && {\kern6pt}+ \frac{P_{q}(t_{1}(h))}{q!}d_{1}^{(q)}h^{q} + \mathcal{O}(h^{q}), \end{array} $$

where the dependence of t1 on h is now explicit. We approximate the right-hand side by the pseudo-polynomial with p + 1 ≤ q terms

$$Q_{p}(h) = \widetilde{I} + \gamma_{1} P_{1}(t_{1}(h)) \widetilde{d}_{1}^{(1)}h + P_{2}(t_{1}(h))\widetilde{d}_{1}^{(2)} \frac{h^{2}}{2!} + {\ldots} + P_{p}(t_{1}(h))\widetilde{d}_{1}^{(p)}\frac{h^{p}}{p!}$$

for values \(\widetilde {I}\), \(\widetilde {d}_{1}^{(1)},\ldots ,\widetilde {d}_{1}^{(p)}\) approximating I, \(d_{1}^{(1)},\ldots ,d_{1}^{(p)}\). The interpolation conditions \(Q_{p}\big (\frac {h_{0}}{2^{i}}\big ) = T_{f}\big (\frac {h_{0}}{2^{i}}\big )\), i = 0,…,p, lead to the system of p + 1 equations

$$ \mathbf{A}\mathbf{H} \left[ \begin{array}{c} \widetilde{I}\\ \widetilde{d}_{1}^{(1)}\\ \widetilde{d}_{1}^{(2)}\\ \vdots\\ \widetilde{d}_{1}^{(p)} \end{array} \right] = \left[\begin{array}{c} T_{f}(h_{0})\\ T_{f}\big(\frac {h_{0}} 2\big)\\ T_{f}\big(\frac {h_{0}} 4\big)\\ \vdots\\ T_{f}\big(\frac{h_{0}}{2^{p}}\big) \end{array}\right] $$
(12)

in the p + 1 unknowns \(\widetilde {I},\widetilde {d}_{1}^{(1)},\widetilde {d}_{1}^{(2)}\ldots , \widetilde {d}_{1}^{(p)}\), with the matrices

$$\mathbf{A} := \left[\begin{array}{ccccc} 1&\gamma_{1}(h_{0})P_{1}(t_{1}(h_{0}))&P_{2}(t_{1}(h_{0}))&\ldots&P_{p}(t_{1}(h_{0}))\\ 1&\frac{1}{2}\gamma_{1}(\frac{h_{0}}{2})P_{1}(t_{1}(\frac{h_{0}}{2})) &\frac{1}{2^{2}}P_{2}(t_{1}(\frac{h_{0}}{2}))&\ldots&\frac{1}{2^{p}}P_{p}(t_{1}(\frac{h_{0}}{2}))\\ 1&\frac{1}{4}\gamma_{1}(\frac{h_{0}}{4})P_{1}(t_{1}(\frac{h_{0}}{4})) &\frac{1}{4^{2}}P_{2}(t_{1}(\frac{h_{0}}{4})) &\ldots&\frac{1}{4^{p}}P_{p}(t_{1}(\frac{h_{0}}{4})) \\ \vdots&&\vdots\\ 1&\frac{1}{2^{p}}\gamma_{1}(\frac{h_{0}}{2^{p}})P_{1}(t_{1}(\frac{h_{0}}{2^{p}})) &\frac{1}{(2^{p})^{2}}P_{2}(t_{1}(\frac{h_{0}}{2^{p}})) &\ldots&\frac{1}{(2^{p})^{p}}P_{p}(t_{1}(\frac{h_{0}}{2^{p}})) \end{array} \right] $$

(compare with [9, p. 220]) and

$$\mathbf{H} := \text{diag}\Big(1,h_{0},\frac{{h_{0}^{2}}}{2!},\frac{{h_{0}^{3}}}{3!},\ldots,\frac{{h_{0}^{p}}}{p!}\Big).$$

We merely need the first component of the solution \(\textbf {x} := \left [\widetilde {I},\widetilde {d}_{1}^{(1)},\widetilde {d}_{1}^{(2)},\ldots , \widetilde {d}_{1}^{(p)}\right ]^{T}\). Since the first element of the diagonal of H is 1, it suffices to find the first component of the solution of Ay = b (y := Hx), where b is the right-hand side of (12).

If the jump is a quadrature node for every hk, then γ1 = 0 for all of these and the second column of A vanishes. Then the unknown \(\widetilde {d}_{1}^{(1)}\) must be removed, together with the second column and the last row of A, H becomes \(\text {diag}(1,{h_{0}^{2}}/2!,{h_{0}^{3}}/3!,\ldots ,{h_{0}^{p}}/p!)\) and the last component of the right-hand side of (12) must be discarded as well.

These equations could probably be solved analytically to obtain Neville-like formulas. As mentioned at the end of Section 3, we solve the system with Gaussian elimination, in which pivoting almost always guarantees a stable method. This is more expensive, but every value of the Neville tableau is computed independently, so that any numerical error arising in one of the entries does not affect the following ones. Our results are in the last column of Table 1.

The values display a pattern that is common to several of our examples. When starting with h0 = ba, as we do, quite a large number of subdivisions of the interval are needed to reach the point (here hk = 1/64) where the extrapolated values become better than those obtained with the plain trapezoidal method. But once this stage is attained, our extrapolation scheme rapidly outperforms the latter and we always obtain an accuracy below our tolerance (usually N ∗ 10− 16, where N + 1 is the size of the sample vector).

The huge value for h = 1/8 is consistent with the large condition number of A: see Section 6.2 for a discussion.

Our function \(\cos \limits (2x)\) may seem too nice and simple. We also computed with a more challenging function, namely \(\cos \limits (100x)\), as well as Runge’s function centered at c1/2, both of which are much more difficult to interpolate with equispaced points: the results are very similar, and the method continues to perform well.

5 Generalization to several jumps

We now address the general case, where f may have several interior jumps at c1,c2, …,cJ, J ≥ 1. Since with the trapezoidal rule the extremities 0 and L are quadrature points, the jump at the endpoints has γ0 = 0 and formula (4) becomes

$$ T_{f}(h) = I + \sum\limits_{j=1}^{J} \gamma_{j} P_{1}(t_{j})d_{j}^{(1)}h + \sum\limits_{\ell=2}^{q} \bigg(\sum\limits_{j=0}^{J} \frac{P_{\ell}(t_{j})}{\ell!}d_{j}^{(\ell)}\bigg)h^{\ell} + \mathcal{O}(h^{q}) $$
(13)

with

$$ d_{j}^{(\ell)} := f^{(\ell-1)}(c_{j}-) - f^{(\ell-1)}(c_{j}+). $$
(14)

Here, the tj are as defined in Theorem 2. The interpolating pseudo-polynomial Qp(h) with p + 1 ≤ q terms again is the same expression, in which I is replaced with its approximation \(\widetilde {I}\) and the terms of the –sum stop with hp. The interpolation conditions \(Q_{p}\big (\frac {h_{0}}{2^{i}}\big ) = T_{f}\big (\frac {h_{0}}{2^{i}}\big )\), i = 0,…,(J + 1)p − 1, lead to the system of (J + 1)p equations

$$ \mathbf{A}\mathbf{H}\mathbf{x} = \left[T_{f}(h_{0}), T_{f}\left( \frac {h_{0}} 2\right), T_{f}\left( \frac {h_{0}} 4\right), \ldots, T_{f}\left( \frac{h_{0}}{2^{(J+1)p-1}}\right)\right]^{T} $$
(15)

in the (J + 1)p unknowns \(\mathbf {x} = \left [\widetilde {I},\widetilde {d}_{1}^{(1)},\ldots ,\widetilde {d}_{J}^{(1)}, \widetilde {d}_{0}^{(2)},\ldots ,\widetilde {d}_{J}^{(2)},\widetilde {d}_{0}^{(3)},\ldots ,\widetilde {d}_{J}^{(3)}, \ldots ,\right .\) \(\left .\widetilde {d}_{0}^{(p)},\right .\) \(\left .\ldots ,\widetilde {d}_{J}^{(p)}\right ]^{T}\) with

$$\mathbf{H} := \text{diag}\Big(1,h_{0},\ldots,h_{0},\frac{{h_{0}^{2}}}{2!},\ldots,\frac{{h_{0}^{2}}}{2!}, \frac{{h_{0}^{3}}}{3!},\ldots,\frac{{h_{0}^{3}}}{3!},\ldots, \frac{{h_{0}^{p}}}{p!},\ldots,\frac{{h_{0}^{p}}}{p!}\Big),$$

in which h0 appears J times and the other powers of h0 show up J + 1 times. Again, we only want the first component of the solution x, i.e., the first component of the solution of Ay = b, where b is the right-hand side of (15) and the matrix A is given by

$$ \mathbf{A} := \left( \begin{array}{ccccc} \ \cr \mathbf{A}_{1}\cr \ \end{array}\right. \left| \begin{array}{c} \ \cr \mathbf{A}_{2}\cr \ \end{array}\right. \left|\begin{array}{c} \ \cr \ldots\cr \ \cr \end{array}\right. \left| \begin{array}{c} \ \cr \mathbf{A}_{p-1}\cr \ \end{array}\right. \left| \begin{array}{c} \ \cr \mathbf{A}_{p}\cr \ \cr \end{array}\right) $$
(16)

with r = (J + 1)p − 1 rows,

$$\mathbf{A}_{1} := \left[ \begin{array}{lllll} 1&\gamma_{1}(h_{0})P_{1}(t_{1}(h_{0}))&\gamma_{2}(h_{0})P_{1}(t_{2}(h_{0}))&\ldots &\gamma_{J}(h_{0})P_{1}(t_{J}(h_{0}))\cr 1&\frac{1}{2}\gamma_{1}(\frac{h_{0}}{2})P_{1}(t_{1}(\frac{h_{0}}{2}))& \frac{1}{2}\gamma_{2}(\frac{h_{0}}{2})P_{1}(t_{2}(\frac{h_{0}}{2}))&\ldots& \frac{1}{2}\gamma_{J}(\frac{h_{0}}{2})P_{1}(t_{J}(\frac{h_{0}}{2}))\\ 1&\frac{1}{4}\gamma_{1}(\frac{h_{0}}{4})P_{1}(t_{1}(\frac{h_{0}}{4}))& \frac{1}{4}\gamma_{2}(\frac{h_{0}}{4})P_{1}(t_{2}(\frac{h_{0}}{4}))&\ldots& \frac{1}{4}\gamma_{J}(\frac{h_{0}}{4})P_{1}(t_{J}(\frac{h_{0}}{4}))\\ 1&\frac{1}{8}\gamma_{1}(\frac{h_{0}}{8})P_{1}(t_{1}(\frac{h_{0}}{8}))& \frac{1}{8}\gamma_{2}(\frac{h_{0}}{8})P_{1}(t_{2}(\frac{h_{0}}{8}))&\ldots& \frac{1}{8}\gamma_{J}(\frac{h_{0}}{8})P_{1}(t_{J}(\frac{h_{0}}{8}))\\ \vdots\\ 1&\frac{1}{2^{r}}\gamma_{1}(\frac{h_{0}}{2^{r}})P_{1}(t_{1}(\frac{h_{0}}{2^{r}}))& \frac{1}{2^{r}}\gamma_{2}(\frac{h_{0}}{2^{r}})P_{1}(t_{2}(\frac{h_{0}}{2^{r}}))&\ldots& \frac{1}{2^{r}}\gamma_{J}(\frac{h_{0}}{2^{r}})P_{1}(t_{J}(\frac{h_{0}}{2^{r}})) \end{array} \right] $$

and

$$ \mathbf{A}_{m} := \left[\begin{array}{ccccc} P_{m}^{*}(t_{0}(h_{0}))&P_{m}(t_{1}(h_{0}))&\ldots&P_{m}(t_{J}(h_{0}))\\ \frac{1}{2^{m}} P_{m}^{*}(t_{0}(\frac{h_{0}}{2}))& \frac{1}{2^{m}} P_{m}(t_{1}(\frac{h_{0}}{2}))&\ldots& \frac{1}{2^{m}} P_{m}(t_{J}(\frac{h_{0}}{2}))\\ \frac{1}{4^{m}} P_{m}^{*}(t_{0}(\frac{h_{0}}{4}))& \frac{1}{4^{m}} P_{m}(t_{1}(\frac{h_{0}}{4}))&\ldots& \frac{1}{4^{m}} P_{m}(t_{J}(\frac{h_{0}}{4}))\\ \frac{1}{8^{m}} P_{m}^{*}(t_{0}(\frac{h_{0}}{8}))& \frac{1}{8^{m}} P_{m}(t_{1}(\frac{h_{0}}{8}))&\ldots& \frac{1}{8^{m}} P_{m}(t_{J}(\frac{h_{0}}{8}))\\ \vdots\\ \frac{1}{(2^{r})^{m}}P_{m}^{*}(t_{0}(\frac{h_{0}}{2^{r}}))& \frac{1}{(2^{r})^{m}}P_{m}(t_{1}(\frac{h_{0}}{2^{r}}))&\ldots& \frac{1}{(2^{r})^{m}}P_{m}(t_{J}(\frac{h_{0}}{2^{r}})) \end{array} \right],\ \ 2\le m\le p. $$
(17)

However, there is a caveat here. Recall that all odd degree Bernoulli polynomials vanish at 0, and that t0(z) = 0 (t0 as defined in Theorem 2) for all z = h0/2k, \(k\in \mathbb {N}\), in the present case. Therefore, the first column of the m th block Am of A vanishes when m is odd and a “jump” is present at the extremities. (The unknown \(\widetilde {d}_{0}\) is absent as well.) We thus implement the following modification of the process: when the increase of p by one unit leads to an odd number, that first column is erased, and simultaneously one row of the matrix is removed, which means that one less trapezoidal approximation is taken into account in the process (making it, in fact, cheaper). We express this by adding a star to the first column of Am, to indicate that it is absent if p is odd; then r < (J + 1)p − 1 if p exceeds two. H changes as well, as some of its elements are eliminated, but that does not affect the method, as only the first component of x is needed.

A general algorithm, which includes the case where several sample vectors are available, is given in Section 7.

One of the reviewers expressed doubts about our method’s efficiency and suggested to replace f over every interval between two jumps by an approximation constructed from the samples there and to sum the exact integrals of these approximations (which amounts to using an open quadrature rule over every subinterval). We have not compared our method to such a scheme. Another comment suggested to use adaptivity, and implement a special procedure near the discontinuities. This is a natural idea, but would ignore our assumption that only equispaced samples are available in our setting.

6 Examples

6.1 A first example with the jump at the boundary

Here we want to integrate the function g in (11) between 0 and 1. The exact value is

$$I = \frac{1}{\beta}\sin\big(\beta c_{1}\big) + e^{1 - c_{1}} - 1.$$

With \(c_{1} = 1/\sqrt {3}\) and β = 2, I ≈ 0.9833366718258914; the corresponding g is given in Fig. 2. The jump at the extremities — or “boundary jump” — which appears in the classic Euler–Maclaurin formula of Theorem 1 is now present and the corresponding terms must be taken into account in Formula (4).

Fig. 2
figure 2

Function g from (11) with \(c_{1} = 1/\sqrt {3}\) and β = 2, as in Fig. 1

The extrapolation method described in the previous section yields Table 2, in the caption of which it is called “first version,” to distinguish it from the second version introduced after the table. Its first column again displays the values hk, the second gives 100 ∗ t1(hk), the relative location of the jump with respect to the next node, in percent. The third column shows the errors of the trapezoidal value, the fourth the degree p of the extrapolation using hk, and the last column lists the errors of the extrapolated value computed as the first component of the solution of Ay = b. A missing value in that last column points to the fact that no extrapolated value is computed after the first new trapezoidal value, when two are needed to interpolate a hp-term, i.e., p is even.

Table 2 Example with one interior jump (and the boundary jump), first version

The abovementioned elimination of one column and simultaneous non-computation of a trapezoidal value have the consequence that the values used in the next extrapolation step do not improve in accuracy as much as for even p. We have therefore repeated the same computations, but by dividing the stepsize h by four when extrapolation is not performed (with corresponding modification of the matrix A, i.e., a multiplication by 1/4 instead of 1/2 of the fraction in front of γ1 in the second column, and correspondingly in the next columns). The results with this “second version” of the method are given in Table 3. Fewer trapezoidal values are now used for extrapolation for about the same accuracy (11 instead of 14 in this example), which decreases the size of the system of equations.

Table 3 Same as Table 2, but second version

6.2 Another function with one jump

To confirm the above results, we have tried another function with one jump, namely

$$ f(x) := \begin{cases} \cos(4 x),&-1 \le x < c_{1},\\ \big(\cos(4 x)+\sin(2.5x)\big)/2,&x=c_{1},\\ \sin(2.5x),&c_{1}< x \le 3. \end{cases} $$
(18)

The exact value is \({\int \limits }_{-1}^{3}f(x)dx = \big (\sin \limits (4c_{1})+\sin \limits (4)\big )/4 + \big (\cos \limits (2.5c_{1})-\cos \limits (2.5)\big )/2.5\). We set c1 = 1/30; the corresponding function is plotted in Fig. 3 and the results with the first version, i.e., the division of h by two at every computation of a new Tf(h), are displayed in Table 4. (From here on, we refrain from giving the value of p, which is just the number of the extrapolation step, i.e., of the row in the last column.)

Fig. 3
figure 3

Function f from (18) with c1 = 1/30

Table 4 Second example with one interior jump, first version

As in Table 1, we encounter a phenomenon that arose in several of our examples: the extrapolated value can be extremely large before hk becomes small enough (or enough trapezoidal values are computed). But this is not terribly important, as we need small enough hk to get improvement over the trapezoidal values.

The results also confirm that bad intermediate extrapolated values do not affect the quality of the subsequent approximations \(\widetilde {I}\): the only numbers used in performing the extrapolation are the trapezoidal values. This illustrates our comments about the Neville tableau in Section 4.

We stopped with the last hk given in the table, since the difference between two consecutive extrapolated values was smaller than our tolerance of N ∗ 10− 16. We then went up to p = 15: the accuracy remained below 5 ⋅ 10− 13, despite condition numbers of A larger than 1020.

These huge condition numbers impact the results only for relatively large hk. The condition number is, of course, only an upper bound on the amplification of round-off errors in solving linear equations. Whether actual amplification occurs depends on how the right hand side b and perturbations to it are aligned with the singular vectors of the matrix. In many practical applications, numerical solutions behave much better than predicted by condition numbers.

In our case, because of the jumps, the first components of b behave eratically, and it is not surprising that sometimes the solution of the system suffers gravely from the ill-conditioning. When the abscissae hk of the last Tf(hk) cluster close to the evaluation point 0, these trapezoidal values become much more regular, which diminishes the likelihood of a bad b. For a generalized Richardson method close to ours, Gaussian elimination has been shown in [18, pp. 73–76] to be stable. All the above likely explains the excellent behavior of our scheme for p large enough.

We then solved the same problem, but, as with the former example, by dividing the stepsize h by four when extrapolation is not performed. The results with that second version appear in Table 5.

Table 5 Same as Table 4, but second version

When hk is such that the jump is close to a node, the trapezoidal rule usually performs worse than with hk− 1. Nevertheless, the extrapolated values are much more regular than in the first version. We have therefore considered only this second version from here on.

What if, after some steps, the jump coincides with a node? In fact, this does not cause a problem: as the first elements of the corresponding column do not vanish, the matrix A remains (mathematically) nonsingular (except for the case where the jump coincides with a node from the onset, but then Romberg may be applied from the start). To demonstrate this, we have repeated the calculations for the last example, but this time with c1 = 1/32. The results are listed in Table 6: from n = 128 on, when c1 is a node, the trapezoidal values become much more regular (in fact O(h2) according to the Euler–Maclaurin formula) and the extrapolated values rapidly converge within our tolerance N ∗ 10− 16.

Table 6 Second example with one interior jump at c1, with c1 becoming a node

6.3 An example with two jumps

As a specific example with two jumps, we have taken

$$ f(x) := \begin{cases} \cos(4 x),&-1 \le x < c_{1},\\ \big(\cos(4 x)+\sin(2.5x)\big)/2,&x=c_{1},\\ \sin(2.5x),&c_{1}< x < c_{2},\\ (\sin(2.5x) + e^{(x-c_{2})})/2&x = c_{2},\\ e^{(x-c_{2})},&c_{2}<x\le 3, \end{cases} $$
(19)

and we have again integrated from − 1 to 3. The exact value is \(\big (\sin \limits (4c_{1})+\sin \limits (4)\big )/4 + \big (\cos \limits (2.5c_{1})-\cos \limits (2.5c_{2})\big )/2.5 + e^{(3-c_{2})} - 1.\)

The function with c1 = 1/30 and \(c_{2} = \sqrt {3}\) is shown in Fig. 4; the exact value becomes I = 2.945411417434258 and our results are given in Table 7. The number of trapezoidal values necessary for an extrapolation is not always three, the number of jumps, since for odd p only two such values are required: as in Section 5, Pm(t0(h/2k)) = 0 for all k, so that \(d_{0}^{(\ell )}\) does not appear in (13) and therefore needs not be determined.

Fig. 4
figure 4

Function f from (19) with c1 = 1/30 and \(c_{2} = \sqrt {3}\)

Table 7 Example (19) with two interior jumps

The first extrapolated values are extremely large, in fact about the reciprocal of the unit round-off. This results from the extremely bad conditioning of the matrix A and, at first, even led us to believe that there was a programming error in our code. To test this, we changed c2 from \(\sqrt {3}\) to \(1 + \sqrt {3}\). The results are given in Table 8.

Table 8 Same as Table 7, but with \(c_{2} = 1 + \sqrt {3}\) instead of \(\sqrt {3}\)

7 Extension to several sample vectors

So far we have started with the trapezoidal value corresponding to the interval [a,b] and then recursively divided all intervals in two, or four in the second version when p is odd, thereby doubling (resp. quadrupling) the number of function values at each step. Equivalently, being given the vector of samples corresponding to the last subdivision, we compute all trapezoidal values and solve the system of equations Ay = b for the first component of y. The fact that this uses only one vector of samples may be an important advantage of the method.

However, such doubling of the number of function values rapidly results in a very large number of these. For that reason, it has been suggested long ago to alternate powers of two multiples of 2 and 3 in the extrapolation process: the so-called Bulirsch sequence [9] of interval numbers, with nk = 2nk− 2 for k ≥ 4, is

$$ (1,)2,3,4,6,8,12,16,24,32,48,\ldots\ldots $$
(20)

(the sequence is monotonically increasing). This requires two sample vectors, one with 2 ⋅ 2 + 1 and the other with 3 ⋅ 2 + 1 samples.

One may generalize this to several vectors of samples, say q of them, the s th one with (2s − 1)2 + 1 components, s = 1,2,…,q, i.e., the first q odd numbers times the first powers of two. (One could consider taking primes times 2 to have only different values of f in the interior of the interval, but with q ≤ 4 this is the same.) We do not know whether it matters, but we have ordered the numbers of intervals. For q = 3, and ignoring the possible first element 1, this yields the following sequences of sample indices:

$$ \begin{array}{llllllllllllllll} 2~~~&&4~~~&&8&&16~~~&&32~~~&&64~~~&&128~~~&&256~~~&~\ldots\\ 3&&6&&12&&24&&48&&96&&192&&384&~\ldots\\ 5&&10&&20&&40&&80&&160&&320&&640&~\ldots\\ \end{array} $$

Ordering these numbers turns out to be very simple: it suffices to read them one anti-diagonal after the other, to obtain

$$ (1,)2,3,4,5,6,8,10,12,16,20,24,32,40,48,64,\ldots $$
(21)

or

$$ n_{k} = 2n_{k-3},\quad k\ge 6. $$
(22)

It does not seem as simple for q = 4.

These numbers indeed grow slowly and may give trapezoidal approximations too inaccurate to yield a precise extrapolate. Compared with the harmonic sequence, which is the best in the context of extrapolation methods for ODEs [9, p. 221], the sequence grows somewhat faster, but requires only three sample vectors whereas the harmonic sequence requires an unboundedly increasing number of them.

The system of equations to be solved for the \(\widetilde {d}_{j}^{(\ell )}\) is like (15), but now with a modified matrix Am

$$ \mathbf{A}_{m} := \left[\begin{array}{cccc}\frac{1}{{n_{1}^{m}}}P_{m}^{*}(t_{0}(\frac{h_{0}}{n_{1}})) &\frac{1}{{n_{1}^{m}}}P_{m}(t_{1}(\frac{h_{0}}{n_{1}})) &\ldots&\frac{1}{{n_{1}^{m}}}P_{m}(t_{J}(\frac{h_{0}}{n_{1}}))\\ \frac{1}{{n_{2}^{m}}} P_{m}^{*}(t_{0}(\frac{h_{0}}{n_{2}}))& \frac{1}{{n_{2}^{m}}} P_{m}(t_{1}(\frac{h_{0}}{n_{2}}))&\ldots& \frac{1}{{n_{2}^{m}}} P_{m}(t_{J}(\frac{h_{0}}{n_{2}}))\\ \frac{1}{{n_{3}^{m}}} P_{m}^{*}(t_{0}(\frac{h_{0}}{n_{3}}))& \frac{1}{{n_{3}^{m}}} P_{m}(t_{1}(\frac{h_{0}}{n_{3}}))&\ldots& \frac{1}{{n_{3}^{m}}} P_{m}(t_{J}(\frac{h_{0}}{n_{3}}))\\ \frac{1}{{n_{4}^{m}}} P_{m}^{*}(t_{0}(\frac{h_{0}}{n_{4}}))& \frac{1}{{n_{4}^{m}}} P_{m}(t_{1}(\frac{h_{0}}{n_{4}}))&\ldots& \frac{1}{{n_{4}^{m}}} P_{m}(t_{J}(\frac{h_{0}}{n_{4}}))\\ \vdots\\ \frac{1}{{n_{r}^{m}}}P_{m}^{*}(t_{0}(\frac{h_{0}}{n_{r}}))& \frac{1}{{n_{r}^{m}}}P_{m}(t_{1}(\frac{h_{0}}{n_{r}}))&\ldots& \frac{1}{{n_{r}^{m}}}P_{m}(t_{J}(\frac{h_{0}}{n_{r}})) \end{array} \right]. $$
(23)

The general algorithm for determining an extrapolated value \(\widetilde {I}\) may now be described as follows:

  • obtain one (or several) sample vector(s) with N1 + 1, resp. N1 + 1,N2 + 1…Nq + 1 samples; the numbers of intervals Nr should be composite numbers, so that several trapezoidal values can be computed from any one of the vectors;

  • determine the sequence {n + 1} of numbers of samples from which the trapezoidal values will be computed (for instance (20) or (21));

  • compute the corresponding vector b of the trapezoidal values; after every step with no extrapolation, a value of n (if enough values are available) should be skipped to improve accuracy (this is the more accurate “second method” suggested in Section 5);

  • compute the matrix A from (15) with the submatrices Am from (23);

  • solve the system of equations Ay = b for the first component y1; the latter is the sought extrapolated value \(\widetilde {I}\).

Before experimenting with several sample vectors, we have computed with only the second vector of the above table, i.e., nk = 3 ⋅ 2k, k = 0,1,2,3,4,…, which can be accomplished by a minor change in our program. For the same example as in Section 6.3, Table 9 then replaces Table 7.

Table 9 Example (19), same as Table 7 but now with 3 ⋅ 2 intervals

This change does not look like a good idea: to obtain, say, an accuracy of 10− 11, about 25’000 points are necessary, compared to about 16’000 with the sequence 2.

7.1 An example with two sample vectors

To test the method with several sample vectors, we started with two of them, i.e., with the Bulirsch sequence (20). Our results, still with the same example as in Section 6.3, are given in Table 10.

Table 10 Example (19), same as Table 7, but with two sample vectors

The availability of a second vector of samples is very effective in terms of function evaluations: despite trapezoidal values all in error by more than 10− 3, the extrapolation scheme reaches an accuracy better than 10− 8 with 512 + 1 + 384 - 1 = 896 values of f, while it does not even reach 10− 6 with a single vector of 2049 values (Table 7)!

7.2 An example with three sample vectors

In example (19), J = 2 and thus three trapezoidal values are needed for the first extrapolation. In order to avoid computing the same function values twice and to use the formula giving T(h/2) from T(h) [17, p. 377], and in view of (22), we started the method with h = (ba)/3, so that the next two interval lengths are h = (ba)/4 and h = (ba)/5. The results are displayed in Table 11.

Table 11 Example (19), same as Table 7, but with three sample vectors

Here, the extrapolated value is with hk = h0/10 already better than the plain trapezoidal value, an accuracy of 2.53 ⋅ 10− 8 is attained with 320 + 256 + 96 – 1 = 671 function evaluations, and a higher precision than 10− 10 with 640 + 512 + 340 – 1 = 1491 evaluations, while working with two sample vectors requires 1536 + 1024 = 2060 evaluations for an accuracy of 1.76 ⋅ 10− 9.

With the data of Tables 710, and 11, we finally plot in Fig. 5 the number of function evaluations needed to reach a certain accuracy, starting from 10− 2, in green and dotted after extrapolation with one vector of samples, in blue and dashed with two, and in red and solid with three (the curves are intended to be read from right to left); the plain trapezoidal values are added in black and dash-dotted for comparison. Note the logarithmic scale!

Fig. 5
figure 5

Comparison of the number of function evaluations needed for a given accuracy with one, two, and three sample vectors

8 Conclusions

The direct application of the composite trapezoidal rule to a piecewise smooth function f converges slowly and in a non-monotone fashion to the integral of f. The extrapolation method presented in this work accelerates the convergence, and makes it regular as soon as the interval length h is small enough. It converged in all examples we tried, despite obtaining sometimes very bad values at the initial stage. The number of correct digits increased by a factor of two to three compared with the trapezoidal values. One may decrease h without any stability concern, and thus use a stopping criterion based on the agreement of consecutive values of \(\widetilde {I}\).

One question is whether better results could be obtained by starting (and extrapolating) from smaller values h0, to avoid the often erratic first extrapolates. We tried this for the very first example (Section 4): the results were not too encouraging, as large values due to the ill-conditioning of the matrices again arose for small divisors of h0 and longer sample vectors were needed to reach our tolerance.

Note, finally, that the method requires knowledge of the locations of the jumps. Methods exist for determining them, but most work in a transformation space; we envision one using just the vector(s) of equispaced samples.