Adaptive Quadrature Techniques Explained
Adaptive Quadrature Techniques Explained
1
Observations:
2
The basis for adaptive quadrature is the following
fundamental additive property of the definite integral.
1
Actually, the ensuing equation usually is true even if c 6∈ [a, b] (unless
f (x) is misbehaved), but such values of c are not used by adaptive quadrature.
3
We expect the quadrature strategy to adapt to the
integrand automatically, partitioning the interval into
subintervals with fine spacing where the integrand is
varying rapidly and coarse spacing where the integrand
is varying slowly.
4
Basic Quadrature Rules
5
The trapezoidal rule T approximates the integral by
the area of a trapezoid with base h and sides equal to
the values of f (x) at the two end points.
f (a) + f (b)
T =h .
2
6
An equivalent interpretation of order (and accuracy) of
a quadrature rule is as follows:
7
If the underlying function is linear (so that the
interpolant and the underlying function are identical),
it should not be surprising that the trapezoidal rule
gives the exact result.
8
The accuracy of the midpoint and trapezoidal rules
can be compared by examining their behavior on the
simple definite integral
Z 1
2 1
x dx = .
0 3
02 + 1 2 1
T =1 = .
2 2
9
The errors have opposite signs and, perhaps
surprisingly, the midpoint rule is twice as accurate
as the trapezoidal rule.
10
We settle for creating a new rule S that better
approximates I:
2 1
S = M + T.
3 3
Indeed, S is usually a more accurate approximation
than either M or T alone.
This yields
h
S = (f (a) + 4f (c) + f (b)).
6
11
So far, we have fit one interpolant to the entire interval
[a, b] and integrated it to obtain a quadrature rule.
12
Applying Simpson’s rule to each subinterval, we obtain
a quadrature rule over [a, b]:
h
S2 = (f (a) + 4f (d) + 2f (c) + 4f (e) + f (b)).
12
Both rules are of order four, but the step size for S2 is
half the step size for S, so the error in S2 is roughly
24 = 16 times smaller than that of S.
(S2 − S)
Q = S2 + .
15
13
As an exercise on the mock final asks you to express Q
as a weighted combination of the five function values
f (a) through f (e) and to establish that its order is six.
14
An Adaptive Quadrature Strategy
15
Rb
The basic problem is to approximate a
f (x) dx to
within some specified tolerance > 0.
where
h a+b
Sh(a, b) = f (a) + 4f + f (b) .
6 2
16
Recalling Weddle’s rule (translated to the present
notation),
where
Sh/2(a, b) − Sh(a, b)
Eh/2 = .
15
17
If not, we repeat this process on the subintervals
[a, (a + b)/2] and [(a + b)/2, b] individually.
18
Two important notes:
Example: Evaluate
Z 3
100 10
2
sin dx.
1 x x
See quadDemo.m
19
Specifying Integrands
we can use
f = @(x) 1./sqrt(1+x.^4)
q = integral(f,0,1)
20
For more complicated functions, we can create a file
(for the above example, say called integrand.m)
function f = integrand(x)
f = 1./sqrt(1+x.^4);
end
Then the statement
q = integral(@integrand,0,1)
uses a function handle to compute the integral.
The integral routine can sometimes handle
integrable singularities at endpoints automatically.
For example, if we want to compute
Z π
sin x
dx,
0 x
we can type
q = integral(@(x) sin(x)./x, 0, 1)
21
However, integral is not likely to work for integrands
that have singularities stronger than about x−1/2 as
x → 0+, even if mathematically the integral is well
defined.
q = integral(@(x) x.^(-0.9), 0, 1)
the output is
Warning: Infinite or Not-a-Number value encountered.
> In funfun/private/integralCalc>iterateScalarValued at 349
In funfun/private/integralCalc>vadapt at 132
In funfun/private/integralCalc at 75
In integral at 88
ans =
Inf
22
Definite integrals that depend on parameters are also
encountered frequently.
function f = my beta(t,z,w)
f = t.^(z-1).*(1-t).^(w-1)
23
The functions used with quadrature routines must have
the variable of integration as the first argument.
z = 8/3;
w = 10/3;
atol = 1e-10;
rtol = 1e-6;
and then use
Q = integral(@t f(t,z,w),0,1,’AbsTol’, atol, ’RelTol’, rtol);
or
Q = integral(@(t) my beta,0,1,’AbsTol’, atol, ’RelTol’, rtol);
24
In other words, the function you input to the function
function should be able to accept a vector argument;
i.e., it should be written in such a way as to be able to
process a bunch of inputs simultaneously .
For example,
sin x
f (x) =
1 + x2
should be input as
sin(x)./(1 + x.^2)
sin(x)/(1 + x^2)
25
So
vectorize(’sin(x)/(1+x^2)’)
produces
sin(x)./(1+x.^2)
26
Integrating Discrete Data
Rb
When approximating a f (x) dx, it is now not possible
to sample f (x) wherever we want.
27
The most obvious approach is to do piecewise linear
interpolation through the data and integrate the
interpolant exactly.
n−1
X yk + yk+1
T = hk ,
2
k=1
where hk = xk+1 − xk .
T = sum(diff(x).*(y(1:end-1)+y(2:end))/2)
28
For example, consider the data set
x = 1:6
y = [6 8 11 7 5 2]
T = 35.
29
Recall that both the spline and pchip interpolants
are based on piecewise cubic Hermite interpolation:
30
As an exercise, show that
xk+1
2 dk+1 − dk
Z
yk+1 + yk
P (x) dx = hk − hk .
xk 2 12
Hence Z b
P (x) dx = T − D,
a
where T is the trapezoidal rule, and
n−1
X dk+1 − dk
D= h2k ,
12
k=1
is a higher-order correction.
dn − d1
D = h2 .
12
31
For the data shown earlier, the area obtained by trapz
(piecewise linear interpolation) is 35.00.
32
High-Powered Tricks and Theory
33
Suppose the nodes xi have been chosen; e.g., they
could be n equally spaced points in [a, b], including the
endpoints.
34
(Then by the linearity property of integration, the rule
will be exact for all polynomials of up to degree 2.)
f (x) = 1 :
Z b
w1 · 1 + w2 · 1 + w3 · 1 = 1 dx = b − a
a
f (x) = x :
b
b2 − a2
Z
w1 · a + w2 · w2 + w3 · b = x dx =
a 2
f (x) = x2 :
2 b 3 3
−
Z
2 a+b b a
w1 · a + w2 · + w3 · b2 = x2 dx =
2 a 3
35
Solution:
See undetCoeffs.m
36
• Gaussian quadrature
37
Recall that when we fixed the xi, we chose the wi to
integrate exactly the functions f (x) = 1, x, x2, . . ..
38
Z 1
f (x) = 1 : w1 · 1 + w2 · 1 = 1 dx = 2
−1
Z 1
f (x) = x : w1 · x1 + w2 · x2 = x dx = 0
−1
Z 1
2 2
f (x) = x : w1 · x21 + w2 · x22 = x dx = 2
−1 3
Z 1
f (x) = x3 : w1 · x31 + w2 · x32 = x3 dx = 0
−1
One solution is
1 1
x1 = − √ , x2 = √ , w1 = 1, w2 = 1.
3 3
39
It turns out that the abscissae for these maximum-
order (2n) quadrature rules are the roots of the nth
Legendre polynomial.
Hence, these methods are also sometimes called Gauss–
Legendre quadrature rules.
Knowledge of the location of the xi makes the
computation of the wi much easier!
→ A linear system of size n can be solved for the wi,
as opposed solving to a nonlinear system of size 2n for
the xi and wi simultaneously.
Note: The Legendre polynomials are defined on the
interval [−1, 1].
So in general, the definite integral defined on [a, b] is
transformed to [−1, 1] to make the evaluations more
convenient.
This means we change the variable of integration by
means of
2x − b − a 1
t= ⇐⇒ x = [(b − a)t + (b + a)].
b−a 2
40
Thus,
b 1
b−a
Z Z
1
f (x) dx = f [(b − a)t + (b + a)] dt.
a −1 2 2
−x2
R1
Example: Approximate 0
e dx by the 2-point
Gaussian quadrature rule.
1
a = 0, b = 1 =⇒ x = (t + 1).
2
Thus
1
1 − 0 1 −{ 1 (t+1)}2
Z Z
2
e−x dx = e 2 dt
0 2 −1
n o2 n o2
1 − 12 − √13 +1 − 1 √1 +1
≈ e +e 2 3
2
= 0.746595.
41
Note: The exact answer is 0.746824.
See gaussQuad.m
42
• Infinite intervals of integration
43
or
t x
x= ⇐⇒ t = ,
1−t x+1
x = 0 =⇒ t = 0, x = ∞ =⇒ t = 1.
44
Z ∞
f (x) = 1 : w1 · 1 + w2 · 1 = e−x(1) dx = 1
0
Z ∞
f (x) = x : w1 · x1 + w2 · x2 = e−xx dx = 1
0
Z ∞
f (x) = x2 : w1 · x21 + w2 · x22 = e−xx2 dx = 2
0
Z ∞
f (x) = x3 : w1 · x31 + w2 · x32 = e−xx3 dx = 6
0
Solution:
√ √
√ √ 2+ 2 2− 2
x1 = 2− 2, x2 = 2+ 2, w1 = , w2 = .
4 4
45
Implementation note: When solving
Z ∞
f˜(x) dx,
0
See infiniteInterval.m
46
• Multiple integrals (cubature)
47
The syntax for integral2 is
q = integral2(fun,xmin,xmax,ymin,ymax)
48
2. Use a higher-dimensional quadrature rule.
This generalizes what we have been doing to higher
dimensions; e.g., a 2D quadrature rule looks like
Z bZ d n
X
f (x, y) dy dx ≈ wif (xi, yi).
a c i=1
49
For example, to gain an extra decimal of accuracy,
you will need 100 times more points!
This makes Monte Carlo uncompetitive for low-
dimensional quadrature.
However, the beauty is that the convergence rate is
independent of the number of dimensions!
Also, the software is easy to write and parallelize.
Monte Carlo methods can be made more efficient
by biasing the sampling, e.g., concentrating where
the integrand is large or rapidly varying.
→ This makes it behave like adaptive quadrature.
Example: Estimate
Z 1Z 1
1
dy dx.
0 0 1 − xy
See multipleIntegral.m
50
Shared-Memory Adaptive Quadrature
51
The Shared-Memory Paradigm
Each processor (or node) also has its own local memory
(cache) and runs its own program.
High−speed Interconnect
Shared Memory
52
We assume each node has access to the integral
function and the integrand f.
53
For concreteness, suppose we are trying to compute2
Z 1
1 log(x)
Q= cos dx
0 x x
using p = 3 processors.
1 2 3
High−speed Interconnect
2
This is the first problem from the SIAM 100-Digit Challenge.
54
Static Scheduling
a+ih
b−a
Z
Qp(i) = f (x) dx, h := ,
a+(i−1)h p
to processor i, i = 1, 2, . . . , p.
The plan is for processor i to work on its part of Q
then write its result to Qp(i).
When all processors have finished, processor 1 sums
the entries of Qp and reports the final answer Q.
This schedule is called “static” because the assignment
of the tasks is fixed before the computation begins.
Because the time taken to solve the problem is
governed by the time it takes the slowest processor to
finish its task, this strategy works well if the workload
can be divided evenly among the processors.
55
Unfortunately, this is often not the case in quadrature
problems.
56
Dynamic Scheduling
57
This process continues until all rp intervals have been
assigned.
58
Example of Parallel Quadrature
59
We now re-solve the two-dimensional quadrature
problem that we solved with the Monte Carlo method:
Z 1Z 1
1
dy dx,
0 0 1 − xy
See
parallelQuad.m
60