Numerical Analysis Computational Assessment A1:
Adaptive Integration using the Composite Trapezoidal Rule
Jan Brandts
1
Introduction
Let a, b, c, d ∈ R, a ≤ c < d ≤ b and write I = [a, b]. For convenience, we will write the
integration operator over [c, d] as a linear functional
Z d
d
0
d
f (x)dx.
(1)
Ic : C (I) → R : Ic (f ) =
c
Write L1 : C 0 (I) → P 1 (I) for the linear interpolation operator on the points x = c and x = d.
Recall that the trapezoidal rule for numerical approximation of Icd is then defined by
Tcd = Icd ◦ L1
(2)
where ◦ denotes the composition of both mappings. An explicit form for Tcd (f ) is
Tcd (f ) =
f (c) + f (d)
(d − c),
2
(3)
which is the surface of the trapezoid formed by (c, 0), (d, 0), (d, f (d)) and (c, (f (c)), where the
surface below the horizontal axis counts negatively.
1.1
Problem formulation
Let J be a partition of I into n subintervals Ij = [xj−1 , xj ] of length hj with j ∈ {1, . . . , n}.
Write L1J : C 0 (I) → S01 (J ) for continuous piecewise linear interpolation with respect to J .
Define
TJ = Iab ◦ L1J .
(4)
The operator TJ is called the composite trapezoidal rule with respect to J . Using the notational convention h0 = hn+1 = 0 it can be easily verified that
TJ (f ) =
n
X
j=1
x
j
Txj−1
(f ) =
1
[(h0 + h1 )f (x0 ) + · · · + (hn + hn+1 )f (xn )] ,
2
(5)
by using (3) on each of the intervals Ij ∈ J and summing the result. For given f and tolerance
ε > 0, the challenge is to construct a partition J such that
|(Iab − TJ )(f )| < ε,
(6)
where, from the point of view of efficiency, TJ (f ) should be evaluated using as little computational effort as possible. We will measure computational effort by the number of function
evaluations of the function to be integrated.
1
2
Error bounds and error estimators
There are two important tools in the process of constructing partitions on which to apply the
composite trapezoidal rule. Both are similar in their use but different in nature:
• error bounds,
• error estimators.
For error bounds it can be proved that the absolute error is no larger than the value of the
bound. Conversely, error estimators rely on heuristics, with as a result that even though they
might be treated as an error bound, they may actually fail to be one.
Figure 1. Examples of adaptively generated dyadic partitions for TJ .
2.1
Adaptive integration using rigorous error bounds
Assume that f ∈ C 2 (I). Then there exists a ξ ∈ [c, d] such that the error in the trapezoidal
rule approximation Tcd (f ) of Icd (f ) satisfies
(Icd − Tcd )(f ) = −
(d − c)3 00
f (ξ).
12
(7)
Since the position of ξ ∈ I is unknown, it seems natural to derive from this expression the
following upper bound for the absolute value of error,
|(Icd − Tcd )(f )| ≤
(d − c)3 00
kf k∞,[c,d] ,
12
(8)
where the supremum norm is taken over [c, d]. Now, let ε > 0 be given, and suppose that
given any interval [c, d], the number
Bcd (f ) =
(d − c)3 00
kf k∞,[c,d]
12
(9)
is known explicitly. Then Bcd can be used to construct a so-called dyadic partition J of I that
meets the requirement (6) by means of an adaptive recursive algorithm. A dyadic partition
contains only intervals whose lengths equal 2−j times the length of the initial interval.
2
Remark 2.1 The error in TJ (f ) is the sum and not the maximum of the errors on each of
the subintervals. Thus, in order to meet the criterium (6), suitable local tolerances εj , adding
up to ε, must be used on each of the subintervals that are created during the process.
As the size of a subinterval Ij goes to zero, the trapezoidal rule converges faster to the exact
integral over Ij than that the local tolerance εj goes to zero. Thus, at least in theory, the
procedure converges if f ∈ C 2 (I).
Remark 2.2 Due to the fact that some computations cannot be made exactly on the computer, it may happen that convergence does not take place. Therefore it is customary to build
in a safe-guard in the form of a deepest recursion level that is allowed. Matlab already has a
recursion limit built in.
2.2
Adaptive integration using heuristic error estimators
Usually, no error bounds are available in practical situations. This may be due to the fact
that f is only known as a black box that you can ask to evaluate f at some point, or because
it is too complicated to access and/or bound the second derivative of f on a given interval.
In the following, we will develop an error estimator Ecd (f ) having the property that
Ecd (f ) ≈ Icd (f ) − Tcd (f ).
(10)
It will use only one additional function evaluation: at the midpoint e of the current interval
[c, d]. An advantage is that, if necessary, this function evaluation can be re-used in the
continuation of the adaptive integration process.
2.2.1
A simple estimator
A simple and straightforward way to estimate the error in Tcd (f ) is to argue that
d
E c (f ) := Tce (f ) + Ted (f ) − Tcd (f ) ≈ Icd (f ) − Tcd (f ).
(11)
d
The quantity E c (f ) is illustrated by the shaded area in the left picture of Figure 2 below.
●
●
●
ε dc (f)
●
●
c
e
●
d
c
e
d
Figure 2. A simple error estimator (left) and its improved version (right).
3
d
However, even for q ∈ P 2 (I), it is not true that E c (q) is exactly equal to Icd (q) − Tcd (q): the
triangular surface will not be equal to the surface below the quadratic curve. As a result, the
error estimator is not asymptotically exact.
Definition 2.3 Let (ej )∞
j=1 be a sequence of positive numbers with the property that
lim ej = 0.
j→∞
If (sj )∞
j=1 is a sequence with the property that
lim
j→∞
sj
=1
ej
∞
then (sj )∞
j=1 is called an asymptotically exact estimator of (ej )j=1 .
d
We will now slightly modify the estimator E c (f ). The modification is based on the observation
d
that for q ∈ P 2 (I), the quantity E c (q) is always three quarters of the exact error Icd (q)−Tcd (q).
This is depicted in the right sketch in Figure 2 and leads to the motivation to define our actual
estimator Ecd (f ) as follows,
4
Ecd (f ) = (Tce + Ted − Tcd )(f ).
(12)
3
By symmetry of the evaluation points, exact integration of cubic polynomials comes as a
bonus, and therefore it should come as no surprise that Ecd (f ) actually equals
Ecd (f ) = Scd (f ) − Tcd (f ),
(13)
the difference between the results of the Simpson rule Scd (f ) and the trapezoidal rule Tcd (f ).
Since Scd (f ) is, as d − c goes to zero, a much better approximation of Icd (f ) then Tcd (f ), we
expect that
Scd (f ) − Tcd (f ) → Icd (f ) − Tcd (f ).
And indeed, it can be proved that Ecd is an asymptotically exact estimator of the error in the
trapezoidal rule in the sense of Definition 2.3.
2.3
Using the estimator
The estimator Ecd defined in (12) or, equivalently (at least mathematically equivalently) in
(13) can be used in the recursive adaptive integration procedure in case the error bound
Bcd is not available. Of course, there is no guarantee whatsoever that Ecd will give correct
information, and indeed, it may horribly fail. A possible way to improve the reliability of the
adaptive integration procedure is to accept that the error is below a given tolerance, only if
the estimator is below half, or one tenth of this tolerance. Further ways to get confidence is
to apply the method also to a small number of randomly chosen subintervals of I such that
different partitions may result, though with a similar approximation of the total integral.
4
3
Requirements
Here we describe the requirements for this assessment on different levels of complication. The
more levels you are able to complete successfully, the higher your grade will be.
Exercise 1
Write a correct recursive Matlab code that implements the adaptive integration procedure
for a given function f on the interval I = [a, b]. This code should be a single Matlab function
file, called TrapAdapt1.m,
function y = TrapAdapt1(f,a,b,tol)
The input parameter tol specifies the maximum absolute error that is tolerated. The output
y should be the approximation TJ (f ) produced by the adaptive integration procedure.
Exercise 2
Extend your code to the below format and save it under the name TrapAdapt2.m
function [y,x,fx] = TrapAdapt2(f,a,b,tol,pic)
If the Boolean variable pic=1, it should give a plot of the continuous piecewise linear interpolant of f on the points of the partition that was constructed. There should be vertical
lines between (xj , 0) and (xj , f (xj )) to emphasize the partition. The partition J should be
returned as a vector x containing the partition points, as well as a vector fx containing the
function values at those points.
Exercise 3
Extend your code to the below format and save it under the name TrapAdapt3.m
function [y,x,fx,E] = TrapAdapt3(f,a,b,tol,pic)
The additional variable E should be the estimate of the error.
Exercise 4
Do experiments with the functions
f1 : [0, 1] → R : x 7→ e
−20(x− 21 )2
and
f2 : [0, 1] → R : x 7→ sin
1
x + 0.1
which correspond to the left and right picture in Figure 1 respectively. For each tolerance
0.1, 0.01, 0.001, 0.0001 count the number of function evaluations needed to reach this accuracy
using your program. Also make estimates of the number of function evaluations that the
uniform trapezoidal rule would have needed to attain the same accuracy. For this, recall that
|(Iab − TJ )(f )| ≤
b − a 2 00
h kf k∞ .
12
Do not exactly compute kf100 k∞ and kf200 k∞ ! Just use your creativity to find a reasonable estimate. This creativity may be called Matlab or Maple, as long as you explain your estimates.
5
Exercise 5
Repeat the experiment of Exercise 4 with a non-trivial function of your choice of which you
think it well illustrates the difference between adaptive and uniform quadrature.
Format of handing in your work
Please make a LaTeX file in which you include printouts of your MatLab codes. The best
way to display Matlab is to use the so-called \begin{verbatim} ... \end{verbatim} environment. Everything between these commands will be displayed in typewriter-style. Note
that in fact the phrase \begin{verbatim} is in a verbatim environment, to avoid that the
backslash will be interpreted as a LaTeX command :-)
Concerning Exercise 4 and 5, include the pictures if you were able to produce them with your
code. You can save a MatLab picture as png or jpg file using the pull-down menu in the figure
window. Choose file and the option save as and choose .png or .jpg whatever gives the
best quality. A picture you can include in LaTeX as follows (here are three pictures next to
one another)
\begin{center}
\includegraphics[width=5cm]{PICS/mypic-1.pdf}\hspace*{2mm}
\includegraphics[width=5cm]{PICS/mypic-2.pdf}\hspace*{2mm}
\includegraphics[width=5cm]{PICS/mypic-3.pdf}\\[2mm]
{\bf Figure 1. } Left you see mypic-1 and right mypic-3.
\end{center}
Of course, all picture files should be in the directory PICS, and PICS should be in the directory
in which you run LaTeX.
It should be possible to include all pictures on one or two pages. Finally, present the numbers
of function evaluations as tables, either using the \begin{tabular} ... \end{tabular}
environment, or make a poor man’s version in verbatim. Include the function of your choice
of Exercise 5. Comments are welcome, just as a courtesy to the reader (as are your names).
Deadline
Please send the pdf-file described above together with your (at most three) Matlab function
files to [email protected] before or on Friday 21 October, 23.59.59 sharp.
PLEASE DO NOT SEND ANY ZIPPED OR TARRED FILES AND KEEP TO THE
ABOVE FORMAT OF INPUT/OUTPUT AND FUNCTION NAMES
Good luck!
Jan Brandts
6
© Copyright 2026 Paperzz