1
Part 5: Integration and
Differentiation
Chapter 19: Numerical
Integration
ENG-307
2 Objectives
Recognizing that Newton-Cotes integration formulas are based on
the strategy of replacing a complicated function or tabulated data
with a polynomial that is easy to integrate.
Knowing how to implement the following single application
Newton-Cotes formulas:
Trapezoidal rule
Simpson’s 1/3 rule
Simpson’s 3/8 rule
Knowing how to implement the following composite Newton-Cotes
formulas:
Trapezoidal rule
Simpson’s 3/8 rule
Knowing how to use the trapezoidal rule to integrate unequally
spaced data.
Understanding the difference between open and closed integration
formulas.
3 Introduction
Integration:
f x dx
b
I a
is the total value, or summation, of f(x) dx over the range from
a to b:
4 Newton-Cotes Formulas
The Newton-Cotes formulae are some of the most common numerical
integration schemes.
They are based on the strategy of replacing a complicated function or
tabulated data with an approximating function that is easy to integrate.
b b
I f ( x)dx f n ( x)dx
a a
f n ( x) a0 a1 x an 1 x n 1 an x n
Consider for example,
the approximation of an
integral by the area under:
A single straight line
A single parabola
5 Newton-Cotes Formulas
Or… the approximation of an
integral by the area under
three straight lines
Closed and open forms of the
Newton-Cotes formulae are
available:
a. Closed forms: data at the
beginning and end of the
limits of integration are
known.
b. Open forms: Integration
limits extend beyond range
of data.
6 The Trapezoidal Rule
The trapezoidal rule is the
first of the Newton-Cotes
closed integration formulas;
it uses a straight-line
approximation for the
function:
b b
I f ( x)dx f1 ( x)dx
a a
f b f a
x a dx
b
I f (a)
a
ba
f a f b
I b a
2
7 The Trapezoidal Rule
An estimate for the local
truncation error of a single
application of the trapezoidal
rule is:
1
Et f b a
3
12
where is somewhere between a
and b.
This formula indicates that the
error is dependent upon the
curvature of the actual function
as well as the distance between
the points.
Error can thus be reduced by
breaking the curve into parts.
8 The Trapezoidal Rule
The local truncation error (true error) in the Trapezoidal rule
(Derivation in text):
Since, , (unknown exactly), take an average value of
the
The approximate error is:
Where:
Note:
9 The Trapezoidal Rule
Example:
Use trapezoidal rule to estimate the integral of:
f (x) = 0.2 + 25x − 200x2 + 675x3 − 900x4 + 400x5
from a = 0 to b = 0.8. Estimate the error. Recall that the exact value of the integral is
1.640533.
1
0
The Trapezoidal Rule
Assuming n+1 data points are evenly
spaced, there will be n intervals over
which to integrate.
The total integral can be calculated by
integrating each subinterval and then
adding them together:
ba
h a x0 b xn
n
x1 x2 xn
I f ( x)dx f ( x)dx f ( x)dx
x0 x1 xn 1
xn x1 x2 xn
I f n x dx f n x dx f n x dx f x dx
x0 x0 x1 xn 1 n
f x0 f x1 f x1 f x2 f xn 1 f xn
I x1 x0 x2 x1 xn xn 1
2 2 2
h n 1
I f x0 2 f xi f xn
2 i 1
1
1
The Trapezoidal Rule
An error for multiple-application “composite” trapezoidal rule can be obtained
by summing the individual errors for each segment:
(b a)3
Ea 3 f ( i)
12n
The average of the 2nd order summation can be obtained by integration over
the interval, divided by interval, and multiplied by number intervals, or
b
f ( i) nf ( f f ( )d / (b a ) )
a
(b a )3
Ea 2
f
12n
So if the number of segments is doubled, the truncation error will be
quartered!
12 The Trapezoidal Rule
Example:
Use the two-segment (composite) trapezoidal rule to estimate the integral of:
f (x) = 0.2 + 25x − 200x2 + 675x3 − 900x4 + 400x5
from a = 0 to b = 0.8. Estimate the error. Recall that the exact value of the integral is
1.640533.
13 The Trapezoidal Rule – MATLAB
h ba
2 2n
14 The Trapezoidal Rule – MATLAB
Simpson’s Rules
One drawback of the trapezoidal rule is that the error is
related to the second derivative of the function.
More complicated approximation formulas can improve the
accuracy for curves - these include using (a) 2nd and (b) 3rd
order polynomials.
The formulas that result from taking the integrals under
these polynomials are called Simpson’s rules.
16 Simpson’s 1/3 Rule
In this method, the function is approximated by a 2nd
order polynomial (quadratic- parabola) .
Requires : 3 points connected by a parabola (odd)
& 2 segments (even)
f2 (x)
f(x)
x0 x1 x2 x
h h
17 Simpson’s 1/3 Rule
Using 2nd order Lagrange polynomial:
Let , & , → # of segments
After integration and algebraic manipulation:
Simpson’s 1/3 Rule
The most accurate formula for equally spaced 2 segments.
18 Simpson’s 1/3 Rule
The true error is given by (see text for derivation):
As with the trapezoidal rule, since we do not know the value of ,
we will use instead of :
The approximate error is:
Note:
19 Simpson’s 1/3 Rule
• Example
Integrate the function
from 0 to 0.8 using Simpson’s 1/3 rule with 2 segments.
Solution: ( 1.640533)
b a 0.8 0
2 segments → =2 h 0.4
n 2
8
f ( 0) 0. 2 f (0.4) 2.456 f (0.8) 0.232
h
I f (0) 4 f (0.4) f (0.8) 03.4 0.2 4(2.456) 0.232 1.367467
3
1.640533 1.367467
t 16.64%
1.640533
20 Simpson’s 1/3 Rule
Multiple-applications of Simpson’s 1/3 rule (Text 21.2):
a. divide the interval to smaller segments.
b. Apply the method for each segment
AAAA AAAA AAAA
AAAA AAAA AAAA
AA AA AA AA AAAA
21 Simpson’s 1/3 Rule
Multiple-applications of Simpson’s 1/3 rule (Text 21.2):
c. For uniform spacing (equal segments):
AA A AA A
, ,.. , ,.. AAA AA A
AA A AAAA AAAA AA A
, ,.. , ,..
The true error of the multiple-application Simpson’s 1/3 rule is:
The approximate error is:
Where:
22 Simpson’s 1/3 Rule
Note:
a. We need an even number of segments, , for multiple
applications of Simpson’s 1/3 rule because we need 2
segments (i.e. 3 points) for each application of the rule.
it is limited to situations where there are an even number of
segments and odd number of points
b. Simpson’s rule is superior to trapezoidal rule for most
applications.
c. However, it is limited to cases where values are equal spaced
data.
23 Simpson’s 1/3 Rule
← 2 segments.
Multiple
application
, ,.. , ,..
24 Simpson’s 1/3 Rule
• Example
Integrate the function
from 0 to 0.8 using Simpson’s 1/3 rule with 4 segments.
Solution: ( 1.640533)
b a 0 .8 0
4 segments → =4 h 0 .2
n 4
f (0) 0.2 f (0.2) 1.288 f (0.4) 2.456
f (0.6) 3.464 f (0.8) 0.232
h
I f ( 0 ) 4 ( f ( 0 .2 ) f ( 0 . 6 )) 2 f ( 0 . 4 ) f ( 0 . 8 )
3
0.2
0.2 4(1.288 3.464) 2(2.456) 0.232 1.623467
3
1.640533 1.623467
t 1.04%
1.640533
25 Simpson’s 3/8 Rule
In this method, our function is approximated by a 3rd order
(cubic) polynomial.
→ we need 4 points (even) 3 segments (odd).
→ will use points a and b , in addition to two
more points in between them
Approximating by the 3rd order polynomial then integrating:
Using 3rd order Lagrange polynomial:
26 Simpson’s 3/8 Rule
Substitute for and integrate
AA
AA
← Simpson’s
3/8 Rule
The most accurate formula for equally spaced 3 segments.
27 Simpson’s 3/8 Rule
The true error of the multiple-application Simpson’s 3/8 rule is:
As with the trapezoidal and Simpson’s 1/3 rules, since we do not
know the value of , we will use instead of :
The approximate error is:
Note:
28 Simpson’s 3/8 Rule
• Example
Integrate the function
from 0 to 0.8 using the most accurate method for 3 segments.
Solution: ( 1.640533)
3 segments = 3 the most accurate methods is Simpson’s 3/8 rule
b a 0.8 0
h 0.2667
n 3
f (0) 0.2 f (0.2667) 1.432774 f (0.5334) 3.487177 f (0.8) 0.232
3h
I f (0) 3( f (0.2667) f (0.5334)) f (0.8)
8
3 * 0.2667
0.2 3(1.43277 3.487177) 0.232 1.51917
8
1.640533 1.51917
t 7.4%
1.640533
29 Simpson’s 3/8 Rule
Note: 𝐸 , / =− 𝑓̅ 𝑥 𝐸 , / =− 𝑓̅ 𝑥
a) The error for the Simpson’s 3/8 rule is same as that for the
Simpson’s 1/3 rule except that we have a larger denominator in the
Simpson’s 3/8 rule (6,480 compared to 2,880). The Simpson’s
3/8 rule is somewhat more accurate than the Simpson’s 1/3 rule.
b) Even though the Simpson’s 3/8 rule is somewhat more accurate
than the Simpson’s 1/3 rule, it still has the same order of accuracy
& at the same time, it requires 4 points compared to 3 points only
for the Simpson’s 1/3. The Simpson’s 1/3 rule is usually
preferred over the Simpson’s 3/8 rule.
c) When the # of segments is odd, the Simpson’s 3/8 rule can be
mixed with the Simpson’s 1/3 rule. In this case, use Simpson’s 3/8
rule to the last 3 segments and Simpson’s 1/3 rule is applied to all
the previous segments.
30 Simpson’s 3/8 Rule
• Example
Determine the integral of the following tabular data using Simpson’s
rules: 0 0.1 0.2 0.3 0.4 0.5
1 7 4 3 5 9
Solution: We have five segments →
Use the 1/3 rule for the 1st two segments.
Use the 3/8 rule for the last 3 segments.
Rule Rule
31 Simpson’s 3/8 Rule
32 Quick Review
1. The trapezoidal rule: Linear
a. 2 points: 1 segment
b. Multiple-applications:
2. Simpson’s 1/3 Rule: 2nd order
a. 3 points 2 segments:
b. Multiple-applications:
, ,.. , ,..
3. Simpson’s 3/8 Rule: cubic
4 points 3 segments:
33 Integration with Unequal Segments
All the previous discussion is for closed integrals with
equally spaced data.
Sometimes experimental data are not equally spaced.
For non-equally spaced data, we apply Trapezoidal
rule to each segment and then sum the results.
xn x1 x2 xn
I fn x dx fn x dx fn x dx fn x dx
x0 x0 x1 x n1
f x0 f x1 f x1 f x2 f xn1 f xn
I x1 x0 x2 x1 xn xn1
2 2 2
34 Integration with Unequal Segments
Integral of Unevenly-Spaced Data:
a. Trapezoidal rule for each segment or:
b. Could also be evaluated with Simpson’s rule for
higher accuracy.
35 Integration with Unequal Segments
• Example x f(x)
0.0 0.200000
Integrate the following data:
0.12 1.309729
Apply Trapezoidal rule: 0.22 1.305241
0.32 1.743393
0.36 2.074903
0.40 2.456000
h1 h2
I f(x0 ) f(x1 ) f(x1 ) f(x2 ) 0.44 2.842985
2 2 0.54 3.507297
hn
f(xn 1 ) f(xn )
0.64 3.181929
2 0.70 2.363000
0.80 0.232000
1.309729 0.2 1.305241 1.309729 0.232 2.363
0.12 0.10 0.10
2 2 2
36 Integration with Unequal Segments
• Example
Integrate the following data. Use the most accurate methods.
x f(x)
The most accurate formula
0.0 0.200000
TR 1 segment 0.12
0.12 1.309729
1/3 rule 2 segments 0.10 0.22 1.305241
0.32 1.743393
0.36 2.074903
3/8 rule 3 segments 0.04
0.40 2.456000
0.44 2.842985
1/3 rule 2 segments 0.10 0.54 3.507297
0.64 3.181929
TR 1 segments 0.06 0.70 2.363000
TR 1 segments 0.10 0.80 0.232000
Integration Code for Unequal Segments
MATLAB Functions
MATLAB has built-in functions to evaluate
integrals based on the trapezoidal rule
z = trapz(y)
z = trapz(x, y)
produces the integral of y with respect to x. If x
is omitted, the program assumes h=1.
z = cumtrapz(y)
z = cumtrapz(x, y)
produces the cumulative integral of y with
respect to x. If x is omitted, the program
assumes h=1.
39 Integration with Unequal Segments
40 Integration with Unequal Segments
Suppose that we had measurements of velocity at a series of discrete unequally spaced
times during free fall (70-kg jumper with 0.275 kg/m drag coefficient). A nice application
of integration is to compute the distance z(t) of an object based on its velocity v(t).
Analytical