The term ``numerical quadrature'' refers to the estimation of an area,
or, more generally, any integral. (You might hear the term
``cubature'' referring to estimating a volume in three dimensions, but
most people use the term ``quadrature.'') We might want to integrate
some function
or a set of tabulated data. The domain might be a
finite or infinite interval, it might be a rectangle or irregular
shape, it might be a multi-dimensional volume.
We first discuss the ``degree of exactness'' (sometimes called the ``degree of precision'') of a quadrature rule, and its relation to the ``order of accuracy.'' We consider some simple tests to measure the degree of exactness and the order of accuracy of a rule, and then describe (simple versions of) the midpoint and trapezoidal rules. Then we consider the Newton-Cotes and Gauss-Legendre families of rules, and discuss how to get more accurate approximations by either increasing the order of the rule or subdividing the interval. Finally, we will consider a way of adapting the details of the integration method to meet a desired error estimate. In the majority of the exercises below, we will be more interested in the error (difference between calculated and known value) than in the calculated value itself.
A word of caution. We discuss three similar-sounding concepts:
This lab will take four sessions. If you print this lab, you may prefer to use the pdf version.
Matlab provides the capability of defining functions ``in line'' instead
of writing m-files to do it. This feature is most convenient when the
function to be defined is very simple-a line of code, say-or when
you have a function that requires several arguments but you only
are interested in varying one of them. One way to do this is to
use the inline function. (The full help description is
available from the command line using help inline, from
the help facility on your PC, or from
the Mathworks on-line reference.)
(More recent versions of Matlab include a more general facility called
anonymous functions, defined using the @ symbol. You may use this
facility if you wish, but it does not work on the computers in GSCC.)
Suppose, for example, you want to define a function sq(x)=x^2.
You could do this by writing
sq=inline('x.^2')
(Letters such as x are understood to mean independent variables.)
or, less ambiguously, by writing
sq=inline('x.^2','x')
You could then use sq(x) later, just as if you had defined
it in an m-file. Alternatively, you could use it where a function
name is needed. In the next section you will be writing an integration
function named midpoint that requires a function name as
its first argument. If you wanted to apply it to the integral
q=midpointquad(inline('x.^2','x'),0,1,11)
There is a nice way to use inline to streamline a sequence of calculations computing the integrals of ever higher degree polynomials in order to find the degree of exactness of a quadrature rule. The following statement
q=midpointquad(inline('5*x.^4','x'),0,1,11);1-q
first computes
'5*x.^4' into '4*x.^3' to check
the error in
A second remark is that errors should be reported in scientific notation (like 1.234e-3, not .0012). You can force Matlab to display numbers in this format using the command format short e (or format long e for 15 decimal places). This is particularly important if you want to visually estimate ratios of errors. In this lab, you should use full-exactness values when you compute ratios of errros. For example, you might use code like
err20=midpointquad('runge',-5,5,20)-2*atan(5);
err40=midpointquad('runge',-5,5,40)-2*atan(5);
ratio=err20/err40
to get a ratio of errors without loss of accuracy due to
reading numbers off the computer screen.
In general, numerical quadrature involves breaking an interval
into subintervals, estimating or modelling the function on
each subinterval and integrating it there, then adding up the
partial integrals.
Perhaps the simplest method of numerical integration is the midpoint
method presented by Quarteroni, Sacco, and Saleri on p. 381.
This method is based on interpolation of
the integrand
by the constant
and multiplying
by the width of the interval. The result is a form of Riemann sum that
you probably saw in elementary calculus when you first studied
integration.
Break the interval
into
subintervals
with endpoints
(there is one more endpoint than intervals, of course).
Then the midpoint rule can be written as
function quad = midpointquad( f, a, b, n) % quad = midpointquad( f, a, b, n) % comments % your name and the datewhere f indicates the name of a function, a and b are the lower and upper limits of integration, and n is the number of points, not the number of intervals. The code for your m-file might look like the following:
xpts = linspace( ??? ) ; h = ??? ; % length of subintervals xmidpts = 0.5 * ( xpts(1:n-1) + xpts(2:n) ); fmidpts = ??? quad = h * sum ( fmidpts );
n h Midpoint Result Error
11 1.0 _________________ __________________
101 0.1 _________________ __________________
1001 0.01 _________________ __________________
10001 0.001 _________________ __________________
If a quadrature rule can compute exactly the integral of any polynomial up to some specific degree, we will call this its degree of exactness. Thus a rule that can correctly integrate any cubic, but not quartics, has exactness 3. Quarteroni, Sacco, and Saleri mention it on p. 429.
To determine the degree of exactness of a rule, we might look at
the approximations of the integrals
![]() |
|||
![]() |
|||
![]() |
|||
![]() |
f Midpoint Result Error
1 ___________________ ___________________
2 * x ___________________ ___________________
3 * x^2 ___________________ ___________________
4 * x^3 ___________________ ___________________
The trapezoid rule breaks [a,b] into subintervals, approximates the integral on each subinterval as the product of its width times the average function value, and then adds up all the subinterval results, much like the midpoint rule. The difference is in how the function is approximated. The trapezoid rule can be written as
To apply the trapezoid rule, we need to generate
points
and evaluate the function at each of them. Then, apply either
(2) or (3) as appropriate.
function quad = trapezoidquad( f, a, b, n ) % quad = trapezoidquad( f, a, b, n ) % more comments % your name and the dateYou may use either form of the trapezoid rule.
f Trapezoid Result Error
1 ___________________ ___________________
2 * x ___________________ ___________________
3 * x^2 ___________________ ___________________
4 * x^3 ___________________ ___________________
n h Trapezoid Result Error
11 1.0 _________________ __________________
101 0.1 _________________ __________________
1001 0.01 _________________ __________________
10001 0.001 _________________ __________________
The midpoint and trapezoid rules seem to have the same exactness and about the same accuracy. There is a difference between them, though. Some integrals have perfectly well-defined values even though the integrand has some sort of mild singularity. There are some sophisticated ways to perform these integrals, but there is a simple way that can be adequate for the case that the singularity appears at the endpoint of an interval. Something is lost, however.
Consider the integral
n h Midpoint Result Error
11 0.1 _________________ __________________
101 0.01 _________________ __________________
1001 0.001 _________________ __________________
10001 0.0001 _________________ __________________
Estimate the rate of convergence (power of
Look at the trapezoid rule for a minute. One way of interpreting that
rule is to say that if the function
is roughly linear over the
subinterval
, then the integral of
is the
integral of the linear function that agrees with
(i.e.,
interpolates
) at the endpoints of the interval. What about trying higher order
methods? It turns out that Simpson's rule can be derived by picking
triples of points, interpolating the integrand
by a quadratic
polynomial, and integrating the quadratic. The trapezoid rule and
Simpson's rule are Newton-Cotes rules of index one and index two,
respectively. In general, a Newton-Cotes formula uses the idea that if
you approximate a function by a polynomial interpolant on uniformly-spaced
points in each subinterval, then you can
approximate the integral of that function with the integral of the
polynomial interpolant. This idea does not work very well
for derivatives. The polynomial interpolant in this case being
taken on a uniformly distributed set of points, including the end
points. The number of points used in a Newton-Cotes rule is a
fundamental parameter, and can be used to characterize the rule.
The ``index'' of a Newton-Cotes rule is commonly defined as one
fewer than the number of points it uses,
although this common usage is not universal.
We applied the trapezoid rule to an interval by breaking it into subintervals and repeatedly applying a simple formula for the integral on a single subinterval. Similarly, we will be constructing higher-order rules by repeatedly applying Newton-Cotes rules over subintervals. But Newton-Cotes formulæ are not so simple as the trapezoid rule, so we will first write a helper function to apply the rule on a single subinterval.
Over a single interval, all (closed) Newton-Cotes formulæ can be written as
function quad = nc_single ( f, a, b, n ) % quad = nc_single ( f, a, b, n ) % more comments % your name and the dateThere are no subintervals in this case. The coding might look like something like this:
xvec = linspace ( a, b, n ); wvec = nc_weight ( n ); fvec = ??? quad = (b-a) * sum(wvec .* fvec);
f Error Error Error
n=4 n=5 n=6
4 * x^3 __________ __________ ___________
5 * x^4 __________ __________ ___________
6 * x^5 __________ __________ ___________
7 * x^6 __________ __________ ___________
Degree ___ ___ ___
The objective of numerical quadrature rules is to accurately approximate integrals. We have already seen that polynomial interpolation on uniformly spaced points does not always converge, so it should be no surprise that increasing the order of Newton-Cotes integration might not produce accurate quadratures.
index nc_single Result Error
3 _________________ __________________
7 _________________ __________________
11 _________________ __________________
15 _________________ __________________
The results of the previous exercise should have convinced you that you need a composite Newton-Cotes rule in order to accurately do numerical integration. In the following exercise you will use nc_single as a helper function for a composite Newton-Cotes routine.
function quad = nc_quad( f, a, b, n, numSubintervals) % quad = nc_quad( f, a, b, n, numSubintervals) % comments % your name and the dateThis function will perform these steps: (1) break the interval into numSubintervals subintervals; (2) use nc_single to integrate over each subinterval; and, (3) add them up.
nc_quad('runge', -5, 5, 4, 10) = 2.74533025
Subin- nc_quad
tervals n Error Err ratio
10 2 _____________ __________
20 2 _____________ __________
40 2 _____________ __________
80 2 _____________ __________
160 2 _____________ __________
320 2 _____________
10 3 _____________ __________
20 3 _____________ __________
40 3 _____________ __________
80 3 _____________ __________
160 3 _____________ __________
320 3 _____________
10 4 _____________ __________
20 4 _____________ __________
40 4 _____________ __________
80 4 _____________ __________
160 4 _____________ __________
320 4 _____________
In the previous exercise, the table served to illustrate the behavior of the integration routine. Suppose, on the other hand, that you had an integration routine and you wanted to be sure it had no errors. It is not good enough to just see that you can get ``good'' answers. In addition, it must converge at the correct rate. Tables such as the previous one are one of the most powerful debugging and verification tools a researcher has.
Like Newton-Cotes quadrature, Gauss-Legendre quadrature interpolates the integrand by a polynomial and integrates the polynomial. Instead of uniformly spaced points, Gauss-Legendre uses optimally-spaced points. Furthermore, Gauss-Legendre converges as degree gets large, unlike Newton-Cotes, as we saw above. Of course, in real applications, one does not use higher and higher degrees of quadrature-one uses more and more subintervals, each with some given degree of quadrature.
The disadvantage of Gauss-Legendre quadrature is that there is no simple formula for the node points and weights. See Quarteroni, Sacco, and Saleri, Section 10.2 and their program zplege.m for further information. We will be using a Matlab function to serve as a table of node points and weights.
Normally, Gauss-Legendre quadrature is characterized by the number of integration points. We speak of three-point Gauss, etc.
The following two exercises involve writing m-files analogous to nc_single.m and nc_quad.m.
function quad = gl_single ( f, a, b, n ) % quad = gl_single ( f, a, b, n ) % comments % your name and the dateAs with nc_single there are no subintervals in this case. Your coding might look like something like this:
[xvec, wvec] = gl_weight ( a, b, n ); fvec = ??? quad = sum( wvec .* fvec );
f Error Error
n=2 n=3
3 * x^2 __________ ___________
4 * x^3 __________ ___________
5 * x^4 __________ ___________
6 * x^5 __________ ___________
7 * x^6 __________ ___________
Degree ___ ___
index gl_single Result Error
3 _________________ __________________
7 _________________ __________________
11 _________________ __________________
15 _________________ __________________
You might be surprised at how much better Gauss-Legendre integration is than Newton-Cotes, using a single interval. There is a similar advantage for composite integration, but it is hard to see for small n. When Gauss-Legendre integration is used in a computer program, it is generally in the form of a composite formulation because it is difficult to compute the weights and integration points accurately for high order Gauss-Legendre integration. The efficiency of Gauss-Legendre integration is compounded in multiple dimensions, and essentially all computer programs that use the finite element method use composite Gauss-Legendre integration rules to compute the coefficient matrices.
function quad = gl_quad( f, a, b, n, numSubintervals) % quad = gl_quad( f, a, b, n, numSubintervals) % comments % your name and the dateThis function will perform two steps: (1) break the interval into numSubintervals subintervals; (2) use gl_single to integrate over each subinterval; and, (3) add them up.
gl_quad('runge', -5, 5, 4, 10) = 2.7468113
Subin- gl_quad
tervals index Error Err ratio
10 1 ____________ __________
20 1 ____________ __________
40 1 ____________ __________
80 1 ____________ __________
160 1 ____________ __________
320 1 ____________
10 2 ____________ __________
20 2 ____________ __________
40 2 ____________ __________
80 2 ____________ __________
160 2 ____________ __________
320 2 ____________
45 3 ____________ __________
90 3 ____________
46 3 ____________ __________
92 3 ____________
47 3 ____________ __________
94 3 ____________
48 3 ____________ __________
96 3 ____________
49 3 ____________ __________
98 3 ____________
The proofs you have seen about convergence of Gauss quadrature rely on bounds on higher derivatives of the function. When bounds are not available, higher-order convergence might not be observed.
log(x)
Subin- gl_quad
tervals index Error Err ratio
10 1 ____________ __________
20 1 ____________ __________
40 1 ____________ __________
80 1 ____________
What is the order of accuracy of the method using index=1?
log(x)
Subin- gl_quad
tervals index Error Err ratio
10 2 ____________ __________
20 2 ____________ __________
40 2 ____________ __________
80 2 ____________
What is the order of accuracy of the method using index=2?
log(x)
Subin- gl_quad
tervals index Error Err ratio
10 3 ____________ __________
20 3 ____________ __________
40 3 ____________ __________
80 3 ____________
What is the order of accuracy of the method using index=3?
Our final task will consider ``adaptive quadrature.'' Adaptive quadrature employs non-uniform division of the interval of integration into subintervals of non-equal length. It uses smaller subintervals where the integrand is changing rapidly and larger subintervals where it is flatter. The advantage of this approach is that it minimizes the work necessary to compute a given integral. Adaptive quadrature is discussed by Quarteroni, Sacco, and Saleri on page 402. In this section, you will investigate a recursive algorithm for adaptively computing quadratures.
Numerical integration is used often with integrands that are very complicated and take a long time to compute. Although this section will use simple integrands for illustrative purposes, you should think of each evaluation of the integrand (``function call'') to take a long time. Thus, the objective is to reach a given accuracy with a minimum number of function calls. A good strategy for achieving a specified accuracy efficiently is to attempt to uniformly distribute the error over each subinterval. If no interval is particularly bad, there is no obvious place to improve the estimate and if no interval is particularly good, no work has been wasted.
Recall that, if an integration method has degree of exactness
,
then the local error on an integration interval of length
can be estimated by dividing the interval into two subintervals
of length
each. Denote by
the integral over the
interval of length
and
and
the
two estimates of the integral on the left and right subintervals.
Then
The basic structure of one simple adaptive algorithm depends on using the error estimate (6) over each integration subinterval. If the error is acceptably small over each interval, the process stops, and, if not, continues recursively. In the following exercise, you will write a recursive function to implement this procedure.
function [Q,errEst,x,recursions]= ...
adaptquad(f,x0,x1,tol,recursions)
% [Q,errEst,x,recursions]=
% adaptquad(f,x0,x1,tol,recursions)
% adaptive quadrature
% input parameters
% f = function to integrate
% x0 = left end point
% x1 = right end point
% tol = desired accuracy
% recursions = number of allowable recursions left
%
% output parameters
% Q = estimate of the value of the integral
% errEst = estimate of error in Q
% x = all intermediate integration points
% recursions = minimum number of recursions remaining
% after convergence
% Add a mid-point and re-estimate integral
xmid=(x0+x1)/2;
% Qleft and Qright are integrals over two halves
N=3;
Qboth=gl_single(f,x0,x1,N);
Qleft=gl_single(f,x0,xmid,N);
Qright=gl_single( ??? );
% p=degree of exactness of Gauss-Legendre
p=2*N-1;
errEst= ??? ;
if errEst<tol | recursions<=0 %vertical bar means "or"
% either ran out of recursions or converged
Q= ??? ;
x=[x0 xmid x1];
else
% not converged -- do it again
[Qleft,estLeft,xleft,recursLeft]=adaptquad(f, ...
x0,xmid,tol/2,recursions-1);
[Qright,estRight,xright,recursRight]=adaptquad(f, ...
??? );
% recursive work is all done, return answers
% don't want xmid to appear twice in x
x=[xleft xright(2:length(xright))];
Q= ??? ;
errEst= ??? ;
recursions=min(recursLeft,recursRight);
end
Note: The input and output parameter recursions
is not theoretically necessary, but is used to guard against infinite
recursion. The output vector x is not necessary, either,
but will be used to show the effect of the adaptation.
adaptquad for Runge tol est. error exact error 1.e-3 __________ __________ 1.e-6 __________ __________ 1.e-9 __________ __________You should find that the estimated and exact errors are close in size, and smaller than tol. For the two more accurate cases, the estimated error is slightly larger than the exact error. As you can see, the estimated error is not so good for the case that tol=1.e-3.
[Q,estErr,x,recursions]=adaptquad('func',0,1,tol,50);
The next few exercises will help you look a little more closely at the results of this recursive adaptive algorithm. Some of the points that will be made are listed below.
In the following exercises, you will examine two functions that are
more difficult to integrate. The first is a scaled version of the
Runge function,
, where
The value of the integral on [-1,1] of the scaled Runge function is
The second function is
, and the value of its integral
over the interval [-1,1] is
A third function that is difficult to integrate is
. This
function has an integrable singularity at
that is close to being
nonintegrable.
[Q,estErr,x,recursions]=adaptquad('srunge',-1,1,1.e-10,50);
What are the estimated and true errors? Is recursions larger
than zero?
xave=(x(2:end)+x(1:end-1))/2; dx= x(2:end)-x(1:end-1); semilogy(xave,dx,'*')A semilog plot is appropriate here because of the wide range of interval sizes. Please include this plot with your summary.
xave=(x(2:end)+x(1:end-1))/2; dx= x(2:end)-x(1:end-1); semilogy(xave,dx,'*')where x is replaced by the variable name you used. A semilog plot is appropriate here because of the wide range of interval sizes. Please include this plot with your summary.
In the following exercise you will apply adaptquad to a function that is almost not integrable. You will see the benefit of the recursions variable, whose reduction to 0 indicates that convergence was not achieved.
There is another approach to approximating integrals, one that can be used
even when the integrand is not smooth or piecewise smooth, when the
integral is taken over a region
that is not
easily characterized or when its dimension,
is high. This versatility
comes at a cost: the method is probabalistic and also slowly convergent.
It is called the ``Monte Carlo'' method.
The basic idea behind the Monte Carlo method is that the formula for computing the average value of a function over a region
![]() |
The Monte Carlo method is discussed briefly by
Quarteroni, Sacco, and Saleri on page 416-417, and in many places on
the web. You can find a very clear description, with considerable
detail at
http://farside.ph.utexas.edu/teaching/329/lectures/node109.html.
Eric Weisstein has an article in MathWorld,
http://mathworld.wolfram.com/MonteCarloIntegration.html
that states, for
and
As an example, consider the problem of computing the area of the unit
circle in
. In this case,
is simply the characteristic
function of the circle
![]() |
The following code, using x and y to denote
and
will estimate the area of the circle
CHUNK=10000; % chosen for efficiency
NUM_CHUNKS=100;
VOLUME=4;
totalPoints=0;
insidePoints=0;
for k=1:NUM_CHUNKS
x=(2*rand(CHUNK,1)-1);
y=(2*rand(CHUNK,1)-1);
z=(x.^2+y.^2<=1); % z=1 for "true" and z=0 for "false"
insidePoints=insidePoints+sum(z);
totalPoints=totalPoints+CHUNK;
end
average=insidePoints/totalPoints;
a=VOLUME*average;
disp(strcat('approx area=',num2str(a), ...
' with true error=',num2str(pi-a), ...
' and estimated error=', ...
num2str(VOLUME*sqrt((average-average^2)/totalPoints))));
Remark: Note that the points are divided into chunks of 10,000 points each. Then the function evaluation is done using vectors whose lengths are the chunk size. Working with these long vectors, Matlab will do the calculations efficiently. You don't, however, want to do all 1,000,000 points at once, because such large vectors can take a while just for the memory to be allocated. That is why the problem is first divided into chunks and then repeated a number of times.
Back to MATH2070 page.