132
6 Computing Integrals and Testing Code
Calculating an integral is traditionally done by
b
a
f (x) dx = F (b) − F (a),
(6.1)
where
f (x) =
dF
dx
.
The major problem with this procedure is that we need to find an anti-derivative
F (x) corresponding to a given f (x). For some relatively simple integrands f (x),
finding F (x) is a doable task. Often, however, it is really challenging, and sometimes
even impossible!
The method (6.1) provides an exact or analytical value of the integral. If we relax
the requirement of computing an exact value for the integral, and instead look for
approximate values, produced by numerical methods, integration becomes a very
straightforward task for almost any given f (x)! In particular, we do not need an
anti-derivative F (x) at all, since it is just the known integrand f (x) that enters the
calculations.
The (apparent) downside of a numerical method is that it can only find an
approximate answer. Leaving the exact for the approximate is a mental barrier in
the beginning, but remember that most real applications of integration will involve
an f (x) function that contains physical parameters, which are measured with some
error. That is, f (x) is very seldom exact, and it does not make sense trying to
compute the integral with a smaller error than the one already present in f (x).
Another advantage of numerical methods is that we can easily integrate a
function f (x) that is only known as samples, i.e., discrete values at some x points,
and not as a continuous function of x expressed through a formula. This is highly
relevant when f is measured in a physical experiment.
6.1 Basic Ideas of Numerical Integration
We consider the integral
b
a
f (x)dx .
(6.2)
Most numerical methods for computing this integral split up the original integral
into a sum of several integrals, each covering a smaller part of the original
integration interval [a, b]. This re-writing of the integral is based on a selection
of integration points x i , i = 0, 1, . . . , n that are distributed on the interval [a, b].
Integration points may, or may not, be evenly distributed. An even distribution
simplifies expressions and is often sufficient, so we will mostly restrict ourselves
to that choice. The integration points are then computed as
x i = a + ih, i = 0, 1, . . . , n,
(6.3)
6 Computing Integrals and Testing Code
Calculating an integral is traditionally done by
b
a
f (x) dx = F (b) − F (a),
(6.1)
where
f (x) =
dF
dx
.
The major problem with this procedure is that we need to find an anti-derivative
F (x) corresponding to a given f (x). For some relatively simple integrands f (x),
finding F (x) is a doable task. Often, however, it is really challenging, and sometimes
even impossible!
The method (6.1) provides an exact or analytical value of the integral. If we relax
the requirement of computing an exact value for the integral, and instead look for
approximate values, produced by numerical methods, integration becomes a very
straightforward task for almost any given f (x)! In particular, we do not need an
anti-derivative F (x) at all, since it is just the known integrand f (x) that enters the
calculations.
The (apparent) downside of a numerical method is that it can only find an
approximate answer. Leaving the exact for the approximate is a mental barrier in
the beginning, but remember that most real applications of integration will involve
an f (x) function that contains physical parameters, which are measured with some
error. That is, f (x) is very seldom exact, and it does not make sense trying to
compute the integral with a smaller error than the one already present in f (x).
Another advantage of numerical methods is that we can easily integrate a
function f (x) that is only known as samples, i.e., discrete values at some x points,
and not as a continuous function of x expressed through a formula. This is highly
relevant when f is measured in a physical experiment.
6.1 Basic Ideas of Numerical Integration
We consider the integral
b
a
f (x)dx .
(6.2)
Most numerical methods for computing this integral split up the original integral
into a sum of several integrals, each covering a smaller part of the original
integration interval [a, b]. This re-writing of the integral is based on a selection
of integration points x i , i = 0, 1, . . . , n that are distributed on the interval [a, b].
Integration points may, or may not, be evenly distributed. An even distribution
simplifies expressions and is often sufficient, so we will mostly restrict ourselves
to that choice. The integration points are then computed as
x i = a + ih, i = 0, 1, . . . , n,
(6.3)
