Professional Documents
Culture Documents
APM2613
Semesters 1 and 2
BARCODE
university
Define tomorrow. of south africa
1 Lesson 6: Numerical Differentiation and Integration
1.1 Objectives
The objectives of this chapter/lesson is to introduce numerical methods for approximating derivatives and
integrals. in these notes we will
• Highlight the link between methods in this topic and those for interpolation;
• Highlight the nature of the three-point and five-point formulas for approximating the derivative of a
function f (x) at a specific point x0 ;
• Highlight some of the methods for approximating definite integrals for a function represented by tab-
ulated points;
1. To use the forward, backward and central difference methods to approximate the derivative, f ′ (x0 );
3. To use the basic interpolating polynomials to formulate the numerical methods to approximate the
integral of a function on a specific interval;
4. To use the following basic numerical methods of integration, called Newton-Cotes formulas:
• Trapezoidal rule
1
• Simpson’s rule or Simpson’s 3
rule
3
• Simpson’s 8
rule
6. To use the composite form of the above Newton-Cotes formulae to find better approximations of a
given interval;
7. To formulate the Gaussian quadrature formula for various degree polynomials, leading to the different
sets of parameters;
8. To use Gaussian quadrature formula with different parameter sets to approximate a given integral.
9. To compare numerical results with analytical values of the given integral if the function is given.
10. To extend the basic methods of numerical integration to functions of two variables.
2
APM2613/LN06/3/2022
1.3 Introduction
In previous discussions of functional approximation - interpolation and least squares approximation - we
have seen how polynomials in particular have been used in the approximation of a function represented by
data points (xi , f (xi )) : i = 0, 1, 2, . . . , n. We continue to exploit the admirable properties of polynomial
with regard to differentiation and integration of functions to develop methods for approximating derivatives
and integrals. As it will be observed, the methods developed make use of the interpolation polynomials to
approximate the function to be differentiated or integrated.
The history of the methods goes back 300 years ago to famous names as Newton, Gauss and Lagrange, who
have featured in numerical methods discussed in other contexts of approximation.
f (x + h) − f (x)
f ′ (x) = lim
h→0 h
We can already see how this quotient relates to the notion of the secant line and the tangent line that have
been used in previous methods of approximation. Following this definition, much has been discussed about
the derivatives of known functions by calculus methods, and rules are available to apply in such cases. How-
ever, if a function is known only through experimental data displayed in a table of coordinates (x, f (x)), such
analytical rules cannot be applied directly, but through values of approximate functions such as interpolation
polynomials previously discussed.
The above quotient already looks familiar from the difference methods associated with interpolation poly-
nomials approximation. Prior to that, a similar quotient was encoountered in the Mean-value theorem when
analysing a function f (x) over an interval [a, b]:
f (b) − f (a)
, a < ξ < b.
b−a
We saw in the difference methods for interpolation that there are different ways of computing the difference
of f (x) between two neighbouring points:
(iii) Central-difference: f (x + h) − f (x − h)
In the current context, the derivative f ′ (x) of a function at a point x can be approximated in terms of its
neighbouring points using these differences as
3
f (x + h) − f (x)
f ′ (x) ≈ , (forward-difference)
h
f (x) − f (x − h)
f ′ (x) ≈ , (backward-difference)
h
f (x + h) − f (x − h)
f ′ (x) ≈ , (central-difference)
2h
for small values of h. As a specific point x0 , f (x0 ) is obtained by replacing x with x0 in the above formulas.
The textbook presents formulas for approximating the derivative f ′ (x0 ) of a tabulated function f (x) at a node
x0 . Note that x0 in this case is used to denote one of the tabulated nodes in a different context than the usual
left end-point x0 in the general node representation x0 , x1 , x2 , . . . , xn . In this context, x0 , is a point of focus
where the value of the derivative is desired. this point can be any of the tabulated points, not necessarily the
endpoint of the interval [x0 , xn ].
The formulas are based on the Lagrange interpolating polynomial. Starting with the usual Lagrange form of
the interpolating polynomial approximation f (x), together with the error term, the equation is differentiated
to get the derivative f ′ (x) in terms of the derivative of the Lagrange coefficients L′k (x) and the tabulated
values f (xk ). This is equation (4.2) on p174.
Recalling that the coefficient Lk (x) of an n-th degree Lagrange polynomial is explicitly written as
the derivatives of Lk (x) will be based on the explicit form. The number of factors depend on the number of
points used and the degree of the approximating polynomial. Taking the first few terms of the equation to
approximate f ′ (x) leads to equation (4.3) which involves three points, x0 , x1 , and x2 at a time. Hence the
name three-point formulas.
NOTE:
• the derivative is based on the assumption that the data nodes are equally spaced by h; so that
• starting with any of the nodes and call it x0 , the other nodes can be written in terms of this node as
x0 +kh; thus for 3 successive nodes x0 , x1 , x2 the denotion x+1 = x0 +h, x1 = x0 +2h, x2 = x0 +2h
is used.
Hence letting some node xj = x0 , the formula of the derivative f ′ (xj ) and at the neighbouring nodes can be
written in terms of x0 and h.
This yields 3 three-point formulas, two of which are prominent and called the Three-point Endpoint-Formula
(equation (4.4)) and the Three-point Midpoint Formula (equation (4.5)).
Note also the emphasis of ’Endpoint’and ’Midpoint’ in these formulas. This is the main distinction between
these formulas:
4
APM2613/LN06/3/2022
By similar arguments, the five-point formulas are derived, one of which is given.
For the second derivative, only the ’Midpoint’ formula is presented for the three-point case.
Another difference formula that has been used in interpolation in the divided-difference formula. this turns
out to be a powerful way to get derivatives numerically. In this context, to get the derivative f ′ (x) at the point
x within an interval where the function is given as data points (xi , f (xi )), for which a divided-difference
interpolating polynomial can be used to approximate the functional value f (x), the interpolating polynomial
can be used in the place of the function to approximate the derivative f ′ (x).
The order (or magnitude) of the error is expressed in terms of the power of h in the error term; i.e. if hk
appears in the error term, the error is of order O(hk ). This implies a dependence of the error on the size of h,
noting that if |h| < 1, then higher powers of h get smaller. So the higher the power k of h in the error term
associated with a formula (this is different from round-off error), the smaller the error resulting from the use
of that formula.
Rb
Recall that the definite integral a
f (x) dx is geometrically interpreted as the area under the curve f (x) be-
tween x = a and x = b.
The methods presented under numerical integration are developed from the Lagrange interpolating polyno-
mial (as it has been with differentiation). In this approach, the general numerical quadrature approximation
Z b n
X
f (x) dx ≈ ai f (xi )
a i=0
Xn Z b
= f (xi ) Li (x) dx, i = 0, 1, 2, . . . , n
i=0 a
from which it is deduced that the coefficients of the quadrature expression have the form
Z b
ai = Li (x) dx.
a
5
So, the main task int he formulation is the evaluation of these integrals.
• etc.
The interpretation of associated with the Trapezoidal Rule is that it approximates the area under the curve of
f (x) inthe interval [a, b] as the area of the trapezium obtained from drawing the secant joining (a, f (a)) and
(b, f (b)) (see Figure 4.3). The normal formula for computing the area of a trapezium is applied, taking two
parallel sides to be the lines x = a and x = b and the corresponding lengths are respectively f (a) and f (b).
Simpson’s Rule
A second degree polynomial is a quadratic polynomial represented graphically by a parabola.Since the poly-
nomial used is an interpolation polynomial, this parabola passes through 3 equally spaced points on the graph
of f (x) (see Figure 4.4), with x0 = a, x1 = a + h, and x2 = a + 2h. So in this case h = b−a
2
.
Observe that some of the area in [a, b] under the curve f (x) is above and other part of the area is below the
parabola.
Although the original intent is to use a second degree Lagrange polynomial in the approximation of f (x), a
direct formulation is bypassed by using a Taylor polynomial about x1 , a mid-point of [x0 , x0 +2h] (see p194).
Take note that the derivatives involved in the Taylor expansion are eventually replaced by numerical ap-
proximations using numerical differentiation discussed in the section immediately before this one. the final
formula for Simpson’s Rule for approximating the definite integral is stated as
Z b Z x2
h
f (x) dx = f (x) dx ≈ [f (x0 ) + 4f (x1 ) + f (x2 )]
a x0 3
6
APM2613/LN06/3/2022
Note that Simpson’s Rule is based on 3 node points partitioning the interval [a, b] into 2 subintervals (or area
panels).
Simpson’s 3/8 Rule is discussed below under the subsection, Newton Cotes Formulas.
Two classes of Newton-Cotes Formulas are presented, closed type (x = a and x = b included as nodes)
and open type (x = a and x = b excluded from the nodes). Depending on the choice of n, the number of
sub-intervals, different formulas have been developed. Of particular interest is the case of n = 3 which leads
to the so called Simpson’s 83 -th Rule:
x3
3h5 (6)
Z
3h
f (x) dx = [f (x0 ) + 3f (x1 ) + 3f (x2 ) + f (x3 )] − f (ξ), x0 < ξ < x3 ,
x0 8 80
7
1.6.1 Composite Trapezoidal Rule
The Composite Trapezoidal Rule is not discussed in the textbook, but it is easy to derive and apply, and so
we discuss it below.
This numerical technique assumes that the values of y = f (x) are known for a sequence of equally spaced
values of x, a ≤ x ≤ b, We let
(a) xk , k = 0, 1, 2, . . . , n, denote the values of x (the nodes), with x0 = a, xk = x0 +kh, and xn = x0 +nh,
h being the stepsize h = b−a n
.
(b) fk = f (xk ), k = 0, 1, 2, . . . , n, denote the corresponding functional values.
The procedure is based on joining consecutive pair of points (xk , f (xk )) and (xk+1 , f (xk+1 )), k = 0, 1, 2, . . . , (n−
1) with straight line segments (secants) whose equation is given by
y − fk fk+1 − fk
= (x − xk ) (k-th interval), k = 0, 1, 2, . . . , (n − 1).
x − xk xk+1 − xk
Note here that the right-endpoint of each intermediate sub-interval coincides with the left-endpoint of the
next sub-interval.
8
APM2613/LN06/3/2022
h3 h2 h2
|E| ≤ n M = nh M = (b − a) M.
12 12 12
This is the truncation error bound for the method.
Example R4
Using Example 1 on p202, to approximate 0 ex dx by the Composite Trapezoidal Rule with h = 1 we have
Z 4 Z 1 Z 2 Z 3 Z 4
x x x x
e dx = e dx + e dx + e dx + ex dx
0 0 1 2 3
h
≈ [f0 + 2f1 + 2f2 + 2f3 + f4 ]
2
1 0
= [e + 2e1 + 2e2 + 2e3 + e4 ]
2
1
= (115.9839) = 57.99195
2
y = αx2 + βx + γ
or any other equivalent form. It is hoped that the area under a parabola is easier to find than the area under
the curve f (x) in a particular interval [xk , xk+1 ] and that the two areas are approximately equal.
There are various ways to express the parabolas and given below is one of the possible ways that does not
allude to Legendre polynomials which are a topic that we have not yet discussed in our functional approxi-
mation methods. The principle in using quadratic functions is, however, the same in all the approaches.
In the Trapezoidal rule, the area between any two adjacent points would be approximated by the equation
h
Ak = (f (xk ) + f (xk+1 ),
2
which can be written as
Ak = P f (xk ) + Qf (xk+1 ),
9
h
where P = Q = 2
and h is the width of the trapezoid.
Here we seek an equation of similar form but involving three points xk−1 , xk and xk+1 :
Simpson’s Rule should give exact answers for any f (x) which is either constant, a straight line, or a parabola.
That is, Simpson’s Rule should give exact answers for
Z h
I1 = 1 dx = x]h−h = 2h,
−h
h h
x2
Z
I2 = x dx = = 0,
−h 2 −h
Z h h
2 x3 2h3
I3 = x dx = = ,
−h 3 −h 3
I1 = P · 1 + Q · 1 + R · 1 = 2h
I2 = P (−h) + Q(0) + R(h) = −P h + Rh = 0
2h3
I3 = P (−h)2 + Q(0) + R(h2 ) = ph2 + rh2 = .
3
Hence the three equations to be solved for P, Q and R:
(1) I1 = P + Q + R = 2h
(2) I2 = −P h + Rh = 0
2h3
(3) I3 = P h2 + Rh2 = .
3
From (2), P = R and hence from (3) we obtain P = R = h3 .
finally, from (1)
h
P + Q + R = Q + 2 = 2h,
3
4
from which we obtain Q = 3 h. thus for Simpson’s method the equation for the area approximation (4) is
10
APM2613/LN06/3/2022
Therefore, summing these over the whole interval [a, b] yields the Composite Simpson’s Rule
Z b Z xn Z x2 Z x4 Z xn
f (x) dx = f (x) dx = f (x) dx + f (x) dx + · · · + f (x) dx
a x0 x0 x2 xn−2
h h
≈ [f (x0 ) + 4f (x1 ) + 2f (x2 )] + [f (x2 ) + 4f (x3 ) + 2f (x4 )] + . . .
3 3
h
+ [f (xn−2 ) + 4f (xn−1 ) + 2f (xn )]
3
h
= [f (x0 ) + 4f (x1 ) + 2f (x2 ) + 4f (x3 ) + · · · + 2f (xn−2 ) + 4f (xn−1 ) + f (xn )] (5)
3
Note the alternating coefficients of 4 and 2 in the expression of the Composite Simpson’s Rule.
Ek = Ak − Ik ,
where Ak is the approximate area by Simpson’s Rule and Ik is the correct area found by integrating the
function f (x) from xk−1 to xk+1 . That is
h
Ak = [f (xk−1 ) + 4f (xk ) + f (xk+1 )]
Z3 xk+1
Ik = f (x) dx = F (xk+1 ) − F (xk−1 )
xk−1
Using exactly the same procedure as in Trapezoidal Rule, we use Taylor series to expand f (xk−1 ) and
f (xk+1 ) about xk and obtain
h2 h3 h4
f (xk−1 ) = f (xk ) − f ′ (xk )h + f ′′ (xk ) − f ′′′ (xk ) + f (iv) (xk ) + . . .
2! 3! 4!
and
h2 h3 h4
f (xk+1 ) = f (xk ) + f ′ (xk )h + f ′′ (xk ) + f ′′′ (xk ) + f (iv) (xk ) + . . .
2! 3! 4!
Substituting these into Ak yields
h3 h5
Ak = 2f (xk ) + 2f ′′ (xk ) + 2f (iv) (xk ) + ...
3! 3 · 4!
Similar expansions of F (xk−1 ) and F (xk+1 ) in Taylor series about xk , and using the fact that f (x) = F ′ (x),
and simplifying, we obtain
Ik = F (xk+1 ) − F (xk−1 )
h3 h5
= 2f (xk ) + 2f ′′ (xk ) + 2f (iv) + ...
3! 3 · 4!
h3 h5
= 2f (xk )h + 2f ′′ (xk ) + 2f (iv) + . . .
3! 5!
11
Finally, we have
Ek = Ak − Ik
h3 h5
′′ (iv)
= Ak = 2f (xk ) + 2f (xk ) + 2f (xk ) + ...
3! 3 · 4!
h3 5
′′ (iv) h
− 2f (xk )h + 2f (xk ) + 2f + ...
3! 5!
5
h5
(iv) h
= 2f (xk ) − + O(h7 ).
3 · 4! 5!
h5 h5
(iv)
Ek ≈ 2f (xk ) − . (6)
3 · 4! 5!
n
An error bound for the overall error from all the now 2
pairs of strips is,
n h5 nh h4 h4
|ES | ≤ M= M = (b − a) M, (7)
2 90 2 90 180
where
M = max |f (iv) (x)| : a ≤ x ≤ b
Hence the error truncation error of Simpson’s Rule is proportional to h4 and thus it decreases much faster as
more intervals are taken; i.e. as h gets smaller.
Note:
• The Composite Trapezoidal Rule can be used for any number of subintervals, but the Composite Simp-
son’s and midpoint Rules require an even number of subintervals since it use two subintervals in its
formula.
b−a
• The width of each subinterval is h = n
for all the formulas.
• Further, that using the composite formulas is not any more difficult that using the basic formulas. The
key thing is to understand the basic formulas and repeatedly apply them on consecutive subintervals,
ensuring that no gaps remain between the subintervals,
Two such quadrature methods are presented: Adaptive Quadrature Method and Gaussian Quadrature Method.
12
APM2613/LN06/3/2022
Note that if a further subdivision of a subinterval, say [ak , bk ], is done inthe process, then [a, b] in the above
Adaptive Quadrature formulas (8) and (9) are replaced with [ak , bk ].
By letting c1 be at the point (x1 , f (x1 )) and (x2 , f (x2 )), a formula of the form
Ai = c1 f (x1 ) + c2 f (x2 )
is developed to determine the values of the parameters x1 , x2 , c1 and c2 . Similar to the derivation of Simpson’s
Rule, the method holds for the monomials y = 1, y = x, y = x2 , and y = x3 . An integration of these
monomials over [−1, 1] (to start with), yields
Z 1 Z 1
I1 = 1 dx = 2; I2 = x dx = 0
−1 −1
Z 1 Z 1
2 2
I3 = x dx = ; I4 = x3 dx = 0.
−1 3 −1
13
(If the actual limits of integration are different, they can be changed to −1 and 1) by a change of variable.)
Equating these true integrals Ii to the Ai s above leads to a set of four equations to be solved for the parame-
ters:
Although the above discussion was based on two (end)points only (from which we derive two-point Gaussian
quadrature formula), the same procedure works for any number of unevenly spaced points along the curve
in the interval −1 to 1 and a suitable curve could be passed through these points; i.e. a parabola if there are
three points, a cubic for four points, etc.
Example
Given below is an example to illustrate how Gaussian quadrature is applied in the case of arbitrary integral
limits.
R3
In the integral 1 cosx x dx, f (x) = cosx x , a = 1, b = 3.To transform the integral limits from 1 to 3, to −1 to
1, the change of variable generally given on p233 as
2x − a − b
t=
b−a
which transforms the variable x to
1
x = [(b − a)t + a + b] = t + 2 → dx = dt
2
and the integral limits 1 and 3 to −1 and 1. Hence the tranformed integral becomes
Z 3 Z 1
cos x cos(t + 2)
dx = dt
1 x −1 t+2
14
APM2613/LN06/3/2022
If Gaussian quadrature method is extended to n points, the general equation for the area to be solved for the
weighting coefficients ci and roots (abscissas) xi is given by
Z 1 n
X
xk dx = ck xki , k = 0, 1, 2, . . . , n. (10)
−1 i=1
The derivation used in the textbook follows what Gauss has shown, that exactness for polynomials of degree
not exceeding 2n − 1 can be attained if and only if the values of x1 , x2 , . . . , xn are the zeros of the Legendre
polynomials Pn (x) given by
2n − 1 n−1
P0 (x) = 1, P1 (x) = x, . . . , Pn (x) = xPn−1 (x) − Pn−2 (x), n = 2, 3, . . . (11)
n n
As you may have figured out, the more points there are involved, the more cumbersome the computations
for solving the system of equations for the parameters. Fortunately, the values of ci and xi to be chosen for
respective number of points are available in standard tables such as Table 4.12 on p232.
e.g. for n = 4
Coefficients, ci Roots, xi
c1 = 0.3478548451 x1 = −0.8611363116
c2 = 0.6521451549 x2 = −0.3399810436
c3 = 0.6521451549 x3 = 0.3399810436
c4 = 0.3478548451 x4 = 0.8611363116
Note that c1 = c4 , c2 = c3 and −x1 = x4 , −x2 = x3 .
15
2 Useful Formulas
1. Trapezoidal rule on [a, b]:
h
Basic: I ≈ (f (a) + f (b)), h=b−a
2
Composite (n intervals):
Z b Z x1
h h
f (x) dx = f (x) dx ≈ (f (x0 )+2f (x1 )+2f (x2 )+· · ·+2f (xn−1 +f (xn )) = (f (a)+f (b)), h=
a x0 2 2
2. Simpson’s Rule:
Basic:
Z b Z x2
h h b−a
f (x) dx = f (x) dx ≈ (f (a)+4f (a+h)+f (b)) = (f (x0 )+4f (x0 +h)+f (x2 ), h=
a x0 3 3 2
Composite rule:
Z b Z xn
h
f (x) dx = f (x) dx ≈ (f (a) + 4f (a + h) + 2f (a2 h) + 4f (a + 3h) + · · · + 4f (b − h) + f (b))
a x0 3
h
= (f (a) + 4f (xa + h) + 2f (xa + 2h) + 4f (xa + 3h) + · · · + 4f (xb − h) +
3
3
Simpson’s 8
rule:
Basic:
Z b Z x3
3h
f (x) dx = f (x) dx ≈ (f (a) + 3f (a + h) + 3f (b − h) + f (b))
a x0 8
3h
= (f (x0 ) + 3f (x1 ) + 3f (x2 ) + f (x4 )),
8
b−a x3 − x0
h= =
3 3
16