Module 5 Numerical Differentiation and Integration
Module 5 Numerical Differentiation and Integration
MA 311
MODULE 5: NUMERICAL
DIFFERENTIATION AND
INTEGRATION
Musango Lungu, D Eng
where Rn n 1! h
• This is a remainder term included to account for all terms from n + 1 to
infinity.
• x is a value of x that lies somewhere between x and xi 1
NUMERICAL DIFFERENTIATION
Forward difference
f xi 1 f xi (5)
f xi O xi 1 xi
xi 1 xi
This can be re-written as :
f i
f xi O h (6)
h
Backward difference
• The Taylor series can be expanded backward to calculate a previous value
on basis of a present value:
f xi 2
f xi 1 f xi f xi h h ... (7)
2!
• This is re-written as:
f i
f xi O h
h (8)
NUMERICAL DIFFERENTIATION
Graphical depiction of (a) forward, (b) backward, and (c) centered finite-divided
difference approximations of the first derivative.
Central difference
The backward Taylor series expansion is subtracted from the forward Taylor
series expansion :
f xi 2 (9)
f xi 1 f xi f xi h h ...
2!
NUMERICAL DIFFERENTIATION
• to yield:
2 f xi
f xi 1 f xi 1 2 f xi h h 3 ...
3! (10)
• which can be solved for
f xi 1 f xi 1 f xi 2
f xi h ...
2h 6
• or
f xi 1 f xi 1
f xi O h 2
2h (11)
The truncation error is of the order of h2 in contrast to the forward
and backward approximations that were of the order of h.
NUMERICAL DIFFERENTIATION
• Taylor series analysis shows that the centered difference is a more
accurate representation of the derivative .
Example 1
Use forward and backward difference approximations of O(h) and a
centered difference approximation of O(h2) to estimate the first derivative
of f x 0.1x 4 0.15 x 3 0.5 x 2 0.25 x 1.25 at x 0.5 using a step
size of h = 0.5.
Repeat the computation using h=0.25. Note that the derivative can be
calculated directly as
f x 0.4 x 3 0.45 x 2 1.0 x 0.25
and can be used to compute the true value as f 0.5 0.9125
NUMERICAL DIFFERENTIATION
Solution
• For h = 0.5, the function can be employed to determine
xi 1 0 f xi 1 1.25
xi 0.5 f xi 0.975
xi 1 1.0 f xi 1 0.25
• These values can be used to compute the forward divided difference
0.25 0.975
f 0.5 1.45 e t 58.9%
0.5
• The backward divided difference
0.975 1.25
f 0.5 0.55 e t 39.7%
0.5
NUMERICAL DIFFERENTIATION
• The centered divided difference
0.25 1.25
f 0.5 1.0 e t 9.6%
0.5
• For h = 0.25
xi 1 0.25 f xi 1 1.153515625
xi 0.5 f xi 0.975
xi 1 0.75 f xi 1 0.686328125
• which can be used to compute the forward divided difference
0.686328125 0.975
f 0.5 1.155 e t 26.5%
0.25
NUMERICAL DIFFERENTIATION
• The backward divided difference
0.975 1.153515625
f 0.5 0.714 e t 21.7%
0.25
• The centered divided difference
0.686328125 1.153515625
f 0.5 0.934 e t 2.4%
0.5
• The following can be deduced from this example:
(i) the centered difference approximation is more accurate than forward
or backward differences for both step sizes.
(ii) halving the step size approximately halves the error of the backward
and forward differences and quarters the error of the centered difference.
NUMERICAL DIFFERENTIATION
Finite Difference Approximations of Higher Derivatives
• Taylor series expansion can be used to derive numerical estimates of
higher derivatives.
• We write a forward Taylor series expansion for f xi 2 in terms of f xi :
f xi (12)
f xi 2 f xi f xi 2h 2
2!
2h ...
• Equation (9) can be multiplied by 2 and subtracted from Eq. (12) to give:
f xi 2 2 f xi 1 f xi f xi h 2 ...
• which can be solved for
f xi 2 2 f xi 1 f xi (13)
f xi 2
O h
h
NUMERICAL DIFFERENTIATION
• This relationship is called the second forward finite divided difference.
• Similar manipulations can be employed to derive a backward version:
f xi 2 f xi 1 f xi 2
f xi O h
h 2
(14)
• and a centered version
f xi 1 2 f xi f xi 1
f xi O h
h 2
(15)
• Alternatively the centered difference can be expressed as:
(16)
f xi 1 f xi f xi f xi 1
f xi h h
h
NUMERICAL DIFFERENTIATION
• Thus the second divided difference approximation is a difference of two
first divided differences.
High-Accuracy Differentiation Formulas
• High-accuracy divided-difference formulas can be generated by including
additional terms from the Taylor series expansion.
• Re-call the forward Taylor series expansion can be written up to the second
term as:
f xi 2
f xi 1 f xi f xi h h ...
2!
2h
• Inclusion of the second-derivative term has improved the accuracy to
O(h2).
• Similar improved versions can be developed for the backward and
centered formulas as well as for the approximations of the higher
derivatives.
• This is left as an exercise to the reader.
NUMERICAL DIFFERENTIATION
Centered finite
divided difference
formulae
NUMERICAL DIFFERENTIATION
Example 2
Repeat Example 1 for a step size h = 0.25 and higher accuracy formulae.
Solution
Data required for this problem is:
xi 2 0 f xi 2 1.25
xi 1 0.25 f xi 1 1.153515625
xi 0.5 f xi 0.975
xi 1 0.75 f xi 1 0.686328125
xi 2 1.0 f xi 1 0.25
NUMERICAL DIFFERENTIATION
• The forward difference of accuracy O(h2) is computed as
f xi 2 4 f xi 1 3 f xi
f xi
2h
0.25 4(0.686328125) 3(0.975)
f 0.5 0.859375 e t 5.82%
2(0.25)
which stands for the integral of the function f(x) with respect to the
independent variable x, evaluated between the limits x = a to x = b
• Integrals are employed by engineers to evaluate the total amount or
quantity of a given physical variable.
NUMERICAL INTEGRATION
• The integral may be evaluated over a line, an area, or a volume.
• Calculation of the volume of a non-isothermal chemical reactor usually
needs the use of numerical integration.
Example 5
Consider the first order reaction A→B in liquid phase, taking place in an
adiabatic plug flow reactor. Pure A enters the reactor, and it is desired to
have the conversion X, at the outlet. The volume of this reactor is given by
: where V is the volume of the reactor, v0, is the
v0 X1 dX inlet volumetric flow rate of A, k, is the rate
V constant at the temperature T1, Ea, is the
k1 0 Ea 1 1
1 X exp activation energy of the reaction, R is the ideal
R T1 T gas constant, T is the temperature of the
reactor where the conversion is X , and T1 , is
a reference temperature.
NUMERICAL INTEGRATION
• The figure below describes the concept of integration.
The approximation of an integral by the area under (a) a single straight line and
(b) a single parabola.
NUMERICAL INTEGRATION
• The integral can also be approximated using a series of polynomials
applied piece-wise to the function or data over segments of constant
length.
The approximation of an integral by the
area under three straight-line segments.
• Integration yields:
f a f b
I b a (23)
2
which is called the trapezoidal rule.
NUMERICAL INTEGRATION
• Therefore, the integral estimate can be represented as:
I width average height (24)
• or
I b a average height
(25)
• where, for the trapezoidal rule, the
•
average height is the average of the
function values at the end points, or
[ f(a) + f(b)]∕2.
• All the Newton-Cotes closed
formulas can be expressed in the
general format of Eq. 25 .
• They differ only with respect to the
formulation of the average height.
NUMERICAL INTEGRATION
Error of the Trapezoidal Rule
• When the integral under a straight-line segment is employed to
approximate the integral under a curve, a substantial might be introduced.
• An estimate for the local truncation error of a single application of
the trapezoidal rule is :
1
f x b a
3
Et
12 (26)
where ξ lies somewhere in the interval from a to b.
• Equation indicates that if the function being integrated is linear, the
result from the trapezoidal rule will be exact.
• Otherwise, for functions with second- and higher-order derivatives
(that is, with curvature), some error can occur.
NUMERICAL INTEGRATION
Example 6
Use the trapezoidal rule to numerically integrate:
f x 0.2 25 x 200 x 2 675 x3 900 x 4 400 x5
Solution
• Exact analytical value of the integral is 1.640533
• The function values f(0) = 0.2 and f(0.8) = 0.232
• Substituting in Eqn. (24) :
0.2 0.232
I 0.8 0.1728
2
• Relative error,
NUMERICAL INTEGRATION
• In actual situations, we would have no foreknowledge of the true
value.
• Therefore, an approximate error estimate is required.
• To obtain this estimate, the function’s second derivative over the interval
can be computed by differentiating the original function twice to give:
f x 400 4050 x 10800 x 2 8000 x3
• The average value of the second derivative can be computed:
0.8
f x
400 4050 x 10800 x 2
8000 x3 dx
0 0.8 0
• which can be substituted into Eq. (26) to yield:
1
2.56
3
Ea 60 0.8
12
NUMERICAL INTEGRATION
• Which is of the same order of magnitude and sign as the true error.
Multiple-Application Trapezoidal Rule
• One way to improve the accuracy of the trapezoidal rule is to divide
the integration interval from a to b into a number of segments and
apply the method to each segment.
• The areas of individual segments can then be added to yield the integral
for the entire interval.
• The resulting equations are called multiple-application, or composite,
integration formulas.
NUMERICAL INTEGRATION
There are n + 1 equally spaced base points
(x0 , x1 , x2 , . . . , xn ).
f x i
f i 1 (32)
n
b a
3
Ea f (33)
12n 2
• Thus, if the number of segments is doubled, the truncation error will
be quartered.
• Note that Eq. (33) gives an approximate error because of the approximate
nature of Eq. (32).
NUMERICAL INTEGRATION
Example 7
Use the two-segment trapezoidal rule to estimate the integral of
f x 0.2 25 x 200 x 2 675 x3 900 x 4 400 x5 from a = 0 to b = 0.8.
Employ Eq. (33) to estimate the error. Recall that the correct value for the
integral is 1.640533.
Solution
For n =2 (h = 0.4)
f 0 0.2, f 0.4 2.456, f 0.6 0.232
0.2 2 2.456 0.232
I 0.8 1.0688
4
Et 1.640533 1.0688 0.57173, Et 34.9%
0.83
Ea 60 0.64
12 2
2
NUMERICAL INTEGRATION
SIMPSON’S RULES
• Another way to obtain a more accurate estimate of an integral is to use
higher-order polynomials to connect the points.
• For example, if there is an extra point midway between f(a) and
f(b), the three points can be connected with a parabola (Fig. a).
• If there are two points equally spaced between f(a) and f(b), the four
points can be connected with a third-order polynomial (Fig. b).
• The formulas that result from taking the integrals under these
polynomials are called Simpson’s rules.
NUMERICAL INTEGRATION
(a) Graphical depiction of Simpson’s 1/3 rule: It consists of taking the area under
a parabola connecting three points. (b) Graphical depiction of Simpson’s 3/8 rule:
It consists of taking the area under a cubic equation connecting four points.
NUMERICAL INTEGRATION
Simpson’s 1/3 Rule
• Simpson’s 1∕3 rule results when a second-order interpolating
polynomial is substituted into Eqn. (21) .
b b
I f x dx f 2 x dx
a a
Ea 2400 0.2730667
2880
• where −2400 is the average fourth derivative for the interval.
NUMERICAL INTEGRATION
• Simpson’s rule can be improved by dividing the integration interval into a
number of segments of equal width :
b a (36)
h
n
• The total integral can be represented as:
x2 x4 xn
I f x dx f x dx ... f x dx (37)
x0 x2 xn 2
• to yield
3h
I f x0 3 f x1 3 f x2 f x3
8
where h= (b−a)∕3
• This equation is called Simpson’s 3∕8 rule because h is multiplied by 3∕8.
• It is the third Newton-Cotes closed integration formula.
NUMERICAL INTEGRATION
• The 3∕8 rule can also be expressed in the form of Eq. (25):
f x0 3 f x1 3 f x2 f x3
I b a
1442443144444444444444244444444444444
(40)
8 3
width
average height
• Thus, the two interior points are given weights of three-eighths, whereas the
end points are weighted with one-eighth.
• Simpson’s 3∕8 rule has an error of:
b a
5
f x
4
Et
6480 (41)
Ea 2400 0.1213636
6480
Because the next two segments from x = 0.12 to 0.32 are of equal length,
their integral can be computed with Simpson’s 1∕3 rule:
1.743393 4 1.305241 1.309729
I 0.2 0.2758029
6
NUMERICAL INTEGRATION
• The next three segments are also equal and, as such, may be evaluated with
the 3∕8 rule to give I = 0.2726863.
• Similarly, the 1∕3 rule can be applied to the two segments from x = 0.44 to
0.64 to yield I = 0.6684701.
• Finally, the last two segments, which are of unequal length, can be
evaluated with the trapezoidal rule to give values of 0.1663479 and
0.1297500, respectively.
• The area of these individual segments can be summed to yield a total
integral of 1.603641.
• This represents an error of εt=2.2%, which is superior to the result using the
trapezoidal rule in Example 11.
NUMERICAL INTEGRATION
OPEN INTEGRATION FORMULAS
• Open integration formulas have limits that extend beyond the range of the
data.
• Table below summarizes the Newton-Cotes open integration formulas.
The formulas are presented in format of Eq. (25). The step size is given by h
= (b − a)/n.
NUMERICAL INTEGRATION
MULTIPLE INTEGRALS
• Multiple integrals are widely used in engineering.
• For example, a general equation to compute the average of a two
dimensional function can be written as
f x, y dx dy
d b
c a (43)
f
d c b a
• The numerator is called a double integral.
NUMERICAL INTEGRATION
Example 13
Suppose that the temperature of a rectangular heated plate is described
by the following function:
T x, y 2 xy 2 x x 2 y 72
2 2
NUMERICAL INTEGRATION
If the plate is 8-m long (x dimension) and 6-m wide (y dimension), compute
the average temperature.
Solution
• Use two-segment applications of the trapezoidal rule in each dimension.
• The temperatures at the necessary x and y values are depicted in
the Figure below
Numerical evaluation of
a double integral using
the two-segment
trapezoidal rule.
NUMERICAL INTEGRATION
• A simple average of these values is 47.33.
• The function can also be evaluated analytically to yield a result of
58.66667.
• To make the same evaluation numerically, the trapezoidal rule is first
implemented along the x dimension for each y value.
• These values are then integrated along the y dimension to give the
final result of 2544.
• Dividing this by the area yields the average temperature as 2544∕(6 ×
8) = 53.
• Applying a single-segment Simpson’s 1∕3 rule in the same fashion an
integral value of 2816 and an average of 58.66667, i.e. exact answer.
END OF MODULE 4