Numerical Integration (SV)
Numerical Integration (SV)
Numerical Integration
1
Learning outcomes
• Implement single application of Newton-Cotes formula
(Trapezoidal rule, Simpson’s 1/3 rule, Simpson’s 3/8 rule)
• Implement composite Newton-Cotes formulas.
• Use the trapezoidal rule to integrate unequally spaced data
2
Integration
• The inverse process to differentiation in calculus is
integration.
• Mathematically, integration is represented by
"
𝐼 = # 𝑓 𝑥 𝑑𝑥
!
• 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.
• The function 𝑓(𝑥) is referred to as the integrand.
3
Integration
4
Main uses
• Two Main Uses
• Summing things up
• Averaging quantities
• a) Discrete Data Integration
n
åy i
y= i =1
n
5
Integrate analytically
• One way to do the integration is to analytically obtain a
closed form solution
t
z (t ) = ò v(t )dt
0
t
gm æ gcd ö
z (t ) = ò tanh çç × t ÷÷dt
0
cd è m ø
m é æ gcd öù
z (t ) = ln êcosh çç t ÷÷ú
cd êë è m øúû
6
Newton-Cotes Formulas
• The Newton-Cotes formulas are the most common
numerical integration schemes.
• Generally, they are based on replacing a complicated
function or tabulated data with a polynomial that is easy to
integrate:
" "
𝐼 = # 𝑓 𝑥 𝑑𝑥 ≅ # 𝑓𝑛 𝑥 𝑑𝑥
! !
where fn(x) is an nth order interpolating polynomial.
7
Newton-Cotes Examples
• The integrating function
can be polynomials for
any order—for example,
(a) straight lines or (b)
parabolas.
• The integral can be
approximated in one step
or in a series of steps to
improve accuracy.
8
Numerical integration
• If integration is the same as summing, can we do it
numerically?
• Take this simple function: f(x) = 2
f(x)
2
a b x
9
Numerical integration
• What if the function is not flat?
• We can approximate using Trapezoid
f1 f2
Area = x(f1+f2)/2
10
Trapezoidal rule
• The trapezoidal rule is the first
of the Newton-Cotes closed
integration formulas; it uses a
straight-line approximation for
the function:
"
𝐼 = # 𝑓𝑛 𝑥 𝑑𝑥
!
"
𝑓 𝑏 −𝑓 𝑎
𝐼=# 𝑓 𝑎 + (𝑥 − 𝑎) 𝑑𝑥
! 𝑏−𝑎
𝑓 𝑎 + 𝑓(𝑏)
𝐼 = (𝑏 − 𝑎)
2
11
Trapezoidal rule error
• The large curvature of f(x) between a=0 and b=0.8 results
in a massive error!
12
Example
Integrate numerically
f ( x) = 0.2 + 25 x - 200x 2 + 675x3 - 900x 4 + 400x5
from a = 0 to b = 0.8.
13
Example
14
Trapezoidal rule error
• Can we estimate the truncation error if there is no analytical
solution?
12
• where a < x < b,
• f ” is the 2nd derivative of the function being integrated.
15
Trapezoidal rule error
• In actual situations, we would have no foreknowledge of the
true value.
• Therefore, an approximate error estimate is required.
1 $$ %
𝐸# = − 𝑓 𝜉 𝑏 − 𝑎
12
16
Trapezoidal rule error
1 $$ %
𝐸# = − 𝑓 𝜉 𝑏 − 𝑎
12
17
Estimating Truncation Error
• Functions that have quickly changing gradients will have
large Trapezoidal Rule errors
18
Numerical integration
• Can we estimate the integral more accurately by using
multiple segments?
19
Composite Trapezoidal Rule
• With n input data points there are n-1 segments
• The segment width h = (b – a) / (n – 1)
• The total integral can be calculated by integrating each
segment and then adding them together
Assume Data Points
I = ò f ( x ) dx
xn
are evenly spaced at
x1
distance h apart
= ò f ( x ) dx + ò f ( x ) dx + + ò f ( x ) dx
x2 x3 xn
x1 x2 xn-1
f ( x2 ) + f ( x1 ) f ( x3 ) + f ( x2 ) f ( xn -1 ) + f ( xn )
= ( x2 - x1 ) + ( x3 - x2 ) + + ( xn - xn -1 )
2 2 2
f ( x2 ) + f ( x1 ) f ( x3 ) + f ( x2 ) f ( xn -1 ) + f ( xn )
=h +h ++ h
2 2 2
hé n -1
ù
= ê f ( x1 ) + 2å f ( xi ) + f ( xn )ú
2ë i =2 û
Composite
Trapezoidal Rule 20
Example
• Use a two-segment Trapezoidal Rule to integrate
f ( x) = 0.2 + 25 x - 200x 2 + 675x3 - 900x 4 + 400x5
from a=0 to b=0.8
21
Example
• Use a four-segment Trapezoidal Rule to integrate
f ( x) = 0.2 + 25 x - 200x 2 + 675x3 - 900x 4 + 400x5
from a=0 to b=0.8
22
Composite Trapezoidal Rule Example
• Use a eight-segment Trapezoidal Rule to integrate
f ( x) = 0.2 + 25 x - 200x 2 + 675x3 - 900x 4 + 400x5
from a=0 to b=0.8
23
Comparison Table
Step
Method Points Segments
Size
Result Et et
Analytical - - - 1.640533 - -
Trapezoidal Rule
Composite
Trapezoidal Rule
24
Dealing with Unequal Segments
• In many engineering situations, the independent data is not
evenly spaced
25
Composite Trapezoidal Rule with Unequal
Segments
• The previous Composite Trapezoidal equation relied on evenly
spaced data points at distance h apart
hé n -1
ù
I = ê f (x1 ) + 2å f (xi ) + f (xn )ú
2ë i =1 û
• The Composite Trapezoidal Rule still works on unequal
segments, just not as “neat”
Unequal Segment
I = ò f ( x ) dx
xn
Composite
x1
Trapezoidal Rule
= ò f ( x ) dx + ò f ( x ) dx + + ò f ( x ) dx
x2 x3 xn
x1 x2 xn-1
f ( x2 ) + f ( x1 ) f ( x3 ) + f ( x2 ) f ( xn -1 ) + f ( xn )
= ( x2 - x1 ) + ( x3 - x2 ) + + ( xn - xn -1 )
2 2 2
f ( x2 ) + f ( x1 ) f ( x3 ) + f ( x2 ) f ( xn -1 ) + f ( xn )
=h +h ++ h Can’t do this
2 2 2
hé n -1
ù anymore!
= ê f ( x1 ) + 2å f ( xi ) + f ( xn )ú
2ë i =2 û
26
Simpson’s Rules
• One drawback of the Trapezoidal Rule is that the truncation
error is related to the second derivative of the function.
27
Trapezoidal rule
• Trapezoidal Rule (Composite, n=4)
28
Simpson’s rules
29
Simpson’s 1/3 Rule
• Simpson’s 1/3 rule uses a degree 2 polynomial (x2) that
passes through three points
2 segments
• The integration is performed on this degree 2 polynomial,
which produces the following equation
I = ò f n (x ) dx
x3
x1
x1 x2 x3
30
Simpson’s 1/3 Rule
• The integration is performed on this degree 2 polynomial,
which produces the following equation
I = ò f n ( x ) dx
x3
h=
(b - a )
x1
n -1
h
I = [ f (x1 ) + 4 f ( x2 ) + f ( x3 )]
3 Simpson’s 1/3 Rule
Width
* Mean Height
31
Simpson’s 1/3 Rule Example
• Consider the polynomial
f ( x) = 0.2 + 25 x - 200x 2 + 675x3 - 900x 4 + 400x5
• Integrate it from a=0 to b=0.8 using Simpson’s 1/3 Rule
32
Comparing Simpson’s 1/3 Rule with Trapezoidal Rule
Composite
Trapezoidal
εt = 34.85%
33
Comparison Table
• In general, the Simpson’s 1/3 rule is more accurate than the
Trapezoidal rule with the same n and h
Step
Method Points Segments
Size
Result Et et
Analytical - - - 1.640533 - -
Trapezoidal Rule
Composite
Trapezoidal Rule
Simpson’s 1/3
Rule
34
Simpson’s 1/3 Rule Error
• An estimate for the truncation error of a single application of
Simpson’s 1/3 rule is
1
f (x )(b - a)
(4 ) 5
Et = -
2880
where a < x < b
x3 dx
x2 dx
x dx
constant dx
0
35
Comparing Truncation Errors
• The truncation error estimate of the single application of
Trapezoidal Rule is:
1
Et = - f ¢¢(x )(b - a )
3
12
• An estimate for the truncation error of a single application of
Simpson’s 1/3 Rule is:
1
f (4 ) (x )(b - a)
5
Et = -
2880
36
Composite Simpson’s 1/3 Rule
• Like the Trapezoidal rule, we can use multiple segments to
perform a Simpson’s 1/3 integration
• This improves integration accuracy
37
Composite Simpson’s 1/3 Rule
h
I= [ f (x1 ) + 4 f (x2 ) + f (x3 )]
3
38
Composite Simpson’s 1/3 Rule
• The integral is broken up two segments at a time
I = ò f (x ) dx = ò f (x ) dx + ò f (x ) dx + + ò f (x ) dx
xn x3 x5 xn
x1 x1 x3 x n -2
39
Composite Simpson’s 1/3 Rule
é n -1 n-2
ù
f (x1 ) + 4 å f (xi ) + 2 å f (x j ) + f (xn )ú
hê
I=
3ê i = 2 , 4 , 6 ,... j =3, 5, 7 ,...
ú
êë i , even j , odd úû
x1 x2 x3 x4 x5 x6 x7 x8 x9 x10 x11
40
Composite Simpson’s 1/3 Rule Example
• Lets revisit this polynomial
f ( x) = 0.2 + 25 x - 200x 2 + 675x3 - 900x 4 + 400x5
• We compared a single application of Simpson’s 1/3 Rule to
a two-segment Composite Trapezoidal Rule
41
Composite Simpson’s 1/3 Rule Example
• Lets revisit the polynomial from a few slides ago
f ( x) = 0.2 + 25 x - 200x 2 + 675x3 - 900x 4 + 400x5
• Integrating from a=0 to b=0.8 using TWO applications of
the Simpson’s 1/3 Rule (5 points, 4 segments)
n=5
f(x4)
h = 0 .2 f(x3)
x1 = 0.0
f(x2)
x 2 = 0 .2
f(x5)
x3 = 0.4 f(x1)
x 4 = 0 .6
Simpsons 1/3 Simpsons 1/3
x5 = 0.8
42
Composite Simpson’s 1/3 Rule Example
43
Composite Simpson’s 1/3 Rule Example
Single
Simpson’s 1/3 Rule
Composite
Simpson’s 1/3 Rule
(Two Passes)
44
Comparison Table
Step
Method Points Segments
Size
Result Et et
Analytical - - - 1.640533 - -
Trapezoidal Rule
Composite
Trapezoidal Rule
Simpson’s 1/3
Rule
Composite
Simpson’s 1/3
Rule
45
Simpson’s 3/8 Rule
• Simpson’s 3/8 rule corresponds to using degree 3 polynomials
(x3) that pass through four points
I = ò f n ( x ) dx
x4
Simpson’s 3/8
x1
Rule
3h
I = [ f ( x1 ) + 3 f ( x2 ) + 3 f ( x3 ) + f ( x4 )]
8
h=
(b - a)
where n -1
46
Simpson’s 3/8 Rule Error
• The Simpson’s 3/8 Rule truncation error can be estimate
using
1
Et = - f ( 4 ) (x )(b - a ) 5
6480
47
Simpson’s 3/8 Rule Example
• Reusing the same polynomial as before
f ( x) = 0.2 + 25 x - 200x + 675x - 900x + 400x
2 3 4 5
48
Simpson’s 3/8 Rule Example
49
Comparison Table
• As expected, the Simpson’s 3/8 rule result is more accurate
than Simpson’s 1/3 but not better than the four-segment
(n=5) Composite Simpson’s 1/3 result
Step
Method Points Segments
Size
Result Et et
Analytical - - - 1.640533 - -
Trapezoidal Rule
Composite
Trapezoidal Rule
Composite
Simpson’s 1/3 Rule
Simpson’s 3/8
50
Combining Simpson’s 1/3 and 3/8 Rule
51
Combining Simpson’s 1/3 and 3/8 Rule
• Since n = 6, h = (0.8 - 0) / 5 = 0.16, which gives the
following function values
f (0) = 0.2 f (0.16) = 1.296919
f (0.32) = 1.743393 f (0.48) = 3.186015
f (0.64) = 3.181929 f (0.8) = 0.232
52
Combining Simpson’s 1/3 and 3/8 Rule
53
Comparison Table
Step
Method Points Segments
Size
Result Et et
Analytical - - - 1.640533 - -
Trapezoidal Rule
Composite
Trapezoidal Rule
54
Romberg Integration
• Technique designed to attain efficient numerical integrals of
functions.
• Based on successive application of the trapezoidal rule.
• An error correction technique similar to Richardson’s
Extrapolation for numerical differentiation.
• The formula for Romberg Integration is
4 1
𝐼 ≅ 𝐼 ℎ& − 𝐼(ℎ')
3 3
where ℎ = (𝑏 − 𝑎)/(𝑛 − 1)
55
Example
Evaluate the integral of
f ( x) = 0.2 + 25 x - 200x 2 + 675x3 - 900x 4 + 400x5
from a = 0 to b = 0.8.
56
Multiple Integrals
• Multiple integrals can be determined numerically
by first integrating in one dimension, then a
second, and so on for all dimensions of the
problem.
57
Example
• Suppose that the temperature of a rectangular heated plate
is described by the following function:
𝑇 𝑥, 𝑦 = 2𝑥𝑦 + 2𝑥 − 𝑥 & − 2𝑦 & + 72
• If the plate is 8 m long (x-dimension) and 6 m wide (y-
dimension), compute the average temperature of the plate.
58
Example
59