Numerical Integration
Ch. 21
Lecture Objectives
• To solve various types of engineering
problems using numerical integration
• To be able to determine which type of
integration technique to use for specific
applications – cost benefit
Numerical Integration
• Very common operation in engineering, Examples?
• Functions that are difficult or impossible to analytically
integrate can often be numerically integrated
• Discrete data integration (I.e, experimental, maybe unevenly
spaced data)
• We will consider two numerical integration techniques:
– Newton Cotes
– Gauss Quadrature
1
Newton Cotes Integration Formula –
• Most common numerical technique
• Replace a complicated function or tabulated data
with with an approximate function that we can
easily integrate
x =b x =b
I= ∫ f ( x)dx ≈ ∫ f
x=a x=a
n ( x)dx
Nth order polynomial
f n ( x) = a0 + a1 x + a2 x 2 + ⋅ ⋅ ⋅ + an −1 x n −1 + an x n
n = 1 Æ straight line
n = 2 Æ parabola
Newton Cotes Integration Formula
f(x) f(x)
n=1 n=2
a b x a b x
Apply piecewise to cover the range a < x < b
OPEN & CLOSED forms of Newton-Cotes
•Open form – integration limits extend beyond the
range of data (like extrapolation); not usually used for
definite integration
•Closed form – data points are located at the beginning
and end of integration limits are known Æ Focus
Newton Cotes Integration Formula – Trapezoidal Rule
• Use a first order polynomial (n = 1, a straight line)
to approximate our function f(x)
x =b x =b
I= ∫ f ( x)dx ≈ ∫ f ( x)dx
x=a x=a
1
f (b ) − f (a )
f1 ( x) = f (a ) + (x − a )
b−a
⎡ f (b ) − f (a ) x 2 bf (a ) − af (b ) ⎤
b
I =⎢ + x⎥
f(a)
⎣ b−a 2 b−a ⎦a
f(x) f(b)
I =⎢
(+
)
⎡ f (b ) − f (a ) b 2 − a 2 bf (a ) + af (b ) ⎤
(b − a )⎥
b
⎣ b−a 2 b−a ⎦a
f (a ) + f (b )
n=1
I = (b − a )
2
a b x
2
Newton Cotes Integration Formula – Trapezoidal Rule
• This is in the form width x average height
f (a ) + f (b )
I = (b − a )
2
width Average height
• Error for a single application (Truncation Error)
f ' ' (ξ )(b − a )
1
Et = −
3
f(a)
f(x) f(b) 12
- Exact for a linear function
n=1 - functions with 2nd and higher
order derivatives will have some
a b x error
ξ
Trapezoidal Rule – Multiple Applications
• Divide the interval a Æ b into n segments with n+1
equally spaced base points
f(x)
h = segment width
b−a
h=
n
x0 x1 x2 x3 x4 x5 x6
a b
x1 x2 xn
I= ∫ f ( x)dx + ∫ f ( x)dx + ⋅ ⋅ ⋅ + ∫ f ( x)dx
x0 x1 xn −1
f ( xo ) + f ( x1 ) f ( x1 ) + f ( x2 ) f ( xn −1 ) + f ( xn )
I =h +h + ⋅⋅⋅ + h
2 2 2
h⎡ n −1
⎤
I = ⎢ f (xo ) + 2∑ f (xi ) + f (xn )⎥
2⎣ i =1 ⎦
Trapezoidal Rule – Multiple Applications
• Put in the form width x average height
⎡ n −1
⎤
⎢ f (xo ) + 2∑ f (xi ) + f (xn )⎥
I = (b − a )⎢ i =1
⎥
⎢ 2n ⎥
⎢⎣ ⎥⎦
width Average height
• Total Trapezoidal error – sum of individual errors
(b − a )3 n
Et = −
12n 3
∑ f ' ' (ξ )
i =1
• Approximate Et by estimating mean 2nd derivative
over the entire interval
f '' ≈
1⎛ n ⎞
⎜ ∑ f ' ' (ξ )⎟ Ea = −
(b − a )3 f ''
n ⎝ i =1 ⎠ 12n 2
3
Trapezoidal Rule – Notes
1. For nicely behaved functions a single application of
the trapezoid rule will give sufficient accuracy for
many engineering purpose
2. For high accuracy (large n), computational effort is
higher
3. Round Off Error with large n will limit the accuracy
of the trapezoid rule
Simpson’s 1/3 Rule -
• Use a higher order polynomial to approximate our
function f(x) – 2nd order Lagrange polynomial – a
unique polynomial that passes through a data points
⎡ ( x − x1 )(x − x2 ) ⎤
⎢ f (x0 ) ⎥
⎢ ( x0 − x1 )( x0 − x 2 ) ⎥
x = x2
⎢ (x − x0 )( x − x2 ) ⎥
I = ∫ ⎢+ f ( x1 ) ⎥dx
f(x)
x = x0 ⎢
( x1 − x 0 )( x1 − x 2 ) ⎥
⎢ ( x − x0 )( x − x1 ) ⎥
⎢ + f ( x )
2 ⎥
⎣ ( x2 − x0 )( x2 − x1 )
n=2
⎦
x0 x1 x2 x
a b
Simpson’s 1/3 Rule -
• After integrating & simplifying:
I=
h
[ f (x0 ) + 4 f (x1 ) + f (x2 )]
3
h = segment width
b−a
h=
2
⎡ f ( x0 ) + 4 f (x1 ) + f (x2 )⎤
f(x)
I = (b − a )⎢ ⎥
⎣ 6 ⎦
h width Average height
x0 x1 x2 x
a b
4
Simpson’s 1/3 Rule –
• Error
Et = −
(b − a )5 f (4 ) (ξ )
2880
• Exact for 3rd order polynomials
• Error goes like (b-a)5 compared to 3rd power of
trapezoidal rule
Simpson’s 1/3 Rule – Multiple Applications
• Requires an Even number of segments
⎡ n −1 n
⎤
⎢ f (xo ) + 4 ∑ f ( xi ) + 2 ∑ f (xi ) + f ( xn )⎥
I = (b − a )⎢ i =1,3, 5 i = 2, 4 ,6
⎥ n segments
⎢ 3n ⎥ n+1 points
⎢ ⎥
⎣ ⎦
width Average height
• Error, third order accurate even though we only use
3 points 5
Ea = −
(b − a ) f (4 )
180n 4
Simpson’s 3/8 Rule -
• An odd number of segments with an even number of
points formula (use a 3rd order polynomial to
approximate f(x))
• Can be used with Simpson’s 1/3 rule to evaluate
even or odd number of segment problems.
x =b x =b
I= ∫
x=a
f ( x)dx ≈ ∫ f ( x)dx
x=a
3
b−a
h=
I = h[ f (x0 ) + 3 f (x1 ) + 3 f (x2 ) + f (x3 )]
3
3
8
⎡ f ( x0 ) + 3 f (x1 ) + 3 f (x2 ) + f (x1 )⎤
I = (b − a )⎢ ⎥
⎣ 8 ⎦
width Average height
5
Simpson’s 3/8 Rule –
• Error
Et = −
(b − a )5 f (4 ) (ξ )
6480
• Exact for 3rd order polynomials, slightly more
accurate than 1/3 rule
• Simpson’s 1/3 rule is preferred since the same
accuracy is achieved with few points.
Gauss Quadrature
Newton-Cotes – (ie., trapezoidal rule & Simpson’s) the integral
was determined by calculating the area under the curve
connecting points a and b (where we evaluate the function at the
end points).
Gauss Quadrature – Consider 2 points along a straight line in
between a and b where positive and negative errors balance to
reduce total error and give a an improved estimate of the integral.
Uses unequal non-uniform spacing – best for functions not
tabular data.
f(x)
a x0 x1 b x
Gauss Quadrature – Method of Undetermined
Coefficients
f (a ) + f (b )
Trapezoidal Rule:
I = (b − a )
2
I ≈ c0 f (a) + c1 f (b)
Should give exact results if f(x) = constant or straight line
(a) y = 1 (b) y = x
f(x) f(x)
-(b-a)/2
(b-a)/2
-(b-a)/2 (b-a)/2 x x
6
Gauss Quadrature – Method of Undetermined
Coefficients
(a) y = 1
b−a b−a
Evaluate Exact 2 2 b−a
integral I= ∫ f (x )dx = ∫ 1dx = x
b−a b−a
−
2
b−a
2
=b−a
− −
2 2
Evaluate
approximation I = c0 f (a ) + c1 f (b) = c0 + c1
f(x) Set equal to each other:
b − a = c0 + c1
-(b-a)/2 (b-a)/2 x
Gauss Quadrature – Method of Undetermined
Coefficients
(b) y = x b−a b−a b−a
2 2
x2
f ( x )dx =
2
I= ∫
b−a
∫ xdx =
b−a 2 b−a
=0
− − −
2 2 2
I ≈ c0 f (a ) + c1 f (b)
I ≈ c0 (− (b − a) / 2) + c1 ((b − a ) / 2 )
f(x)
b−a b−a
-(b-a)/2
− c0 + c1 =0
(b-a)/2 2 2
x
Gauss Quadrature – Method of Undetermined
Coefficients
2 Equations & 2 unknowns solve for co and c1:
b−a b−a
− c0 + c1 =0 c0 = c1
2 2
b − a = c0 + c1 b−a
c0 = c1 =
2
Substitute co and c1 back into the original equation:
I ≈ c0 f (a) + c1 f (b)
b−a b−a
I≈ f (a) + f (b)
2 2
Equivalent to the
f (a ) + f (b)
I = (b − a ) Trapezoidal Rule!
2
7
Two Point Gauss Legendre Formula
Extend the method of undetermined coefficients:
I ≈ c0 f ( x0 ) + c1 f ( x1 )
co & c1 – unknown constants
f(xo) & f(x1) – unknown locations between a & b
We now have 4 unknowns Æ need 4 equations!
f(x)
a x0 x1 b x
Two Point Gauss Legendre Formula
Need to assume functions again:
I ≈ c0 f ( x0 ) + c1 f ( x1 )
(a) f(x) = 1
(b) f(x) = x
(c) f(x) = x2 Parabolic & cubic functions will give
(d) f(x) = x3 us a total of 4 equations
We will get a 2pt linear integration formula formula that will be
exact for cubics!
To simplify the math & provide a general formula Æ select
limits of integration to be –1 & 1
Normalized coordinates
Two Point Gauss Legendre Formula
Evaluate the integrals for our 4 equations:
(a) f(x) = 1 c0 f ( x0 ) + c1 f ( x1 ) = ∫ 1dx = 2
−1
1
(b) f(x) = x c0 f ( x0 ) + c1 f ( x1 ) = ∫ xdx = 0
−1
1
2
(c) f(x) = x2 c0 f ( x0 ) + c1 f ( x1 ) = ∫ x 2 dx =
−1
3
1
(d) f(x) = x3 c0 f ( x0 ) + c1 f ( x1 ) = ∫ x 3 dx = 0
−1
8
Two Point Gauss Legendre Formula
Rewrite the equations:
c0 + c1 = 2
c0 x0 + c1 x1 = 0
2
c0 x02 + c1 x12 =
3
c0 x03 + c1 x03 = 0
Solve for co, c1, xo and x1:
c0 = c1 = 1
x0 = −1 / 3
x1 = 1 / 3
Two Point Gauss Legendre Formula
2 Point Gauss Legendre Formula (for integration limits –1 to 1:
( ) (
I = f −1/ 3 + f 1/ 3 ) 3rd order accurate
Need to change variables to translate to other integration limits
Two Point Gauss Legendre Formula
Changing the limits of integration:
• Introduce a new variable xd that represents x in our
generalized formula (where we use –1 to 1)
• Assume xd is linearly related to x
x = a0 + a1 xd
x = a → xd = −1 a = a0 + a1 (− 1)
x = b → xd = +1 b = a0 + a1 (1)
f(x)
b+a b−a
a0 = a1 =
2 2
x = a0 + a1 xd
a x0 x1 b x
9
Two Point Gauss Legendre Formula
Substitute ao and a1 back into our original linear formula
x = a0 + a1 xd
⎛b+a⎞ ⎛b−a⎞
x=⎜ ⎟+⎜ ⎟ xd
⎝ 2 ⎠ ⎝ 2 ⎠
Differentiate with respect to xd:
⎛b−a⎞
dx = ⎜ ⎟dxd
⎝ 2 ⎠
Substitute these values of x and dx in the original integral to
effectively change the limits of integration without changing
the value of the integral.
b−a
[ ]
b
I = ∫ f ( x)dx ≈ c0 f ( xotrans ) + c1 f ( x1trans )
a
2
Gauss-Legendre Quadrature – uses roots of
Legendre Polynomials to locate the point at which
the integrand is evaluated
1 n
∫ f ( x)dx = ∑ ci f ( xi )
−1 i =0
f(x)
-1 x0 x1 1 x
Gauss-Legendre Quadrature – uses roots of
Legendre Polynomials to locate the point at which
the integrand is evaluated
unknown weighting coefficients
1 n
∫ f ( x)dx = ∑ ci f ( xi )
−1 i =0
Gauss Points: specific
f(x) value where the
integrand
is evaluated
-1 x0 x1 1 x
10
Gauss-Legendre Quadrature – uses roots of
Legendre Polynomials to locate the point at which
the integrand is evaluated
unknown weighting coefficients
1 n
∫ f ( x)dx = ∑ ci f ( xi )
−1 i =0
Gauss Points: specific
value where the
The values of wi and xi are chosen so that integrand
is evaluated
the formula will be exact up to & including
a polynomial of degree (2m-1), where m is
the number of points.
Ex: 2 Point Æ Exact 3rd order polynomial
Gauss-Legendre Quadrature – General form 2 Point
Application – from our derivation, we found xo and x1for
the integration limits 1 to -1
1
I = ∫ f ( xd )dxd ≈ c0 f ( x0 ) + c1 f ( x1 )
−1
f(xd)
−1 1
-1 xo = x1 = 1 xd
3 3
⎛ −1 ⎞ ⎛ −1 ⎞
I ≈ f⎜ ⎟+ f ⎜ ⎟
⎝ 3⎠ ⎝ 3⎠
Gauss-Legendre Quadrature – 2 Pt Application –
Transformation procedure
b+a b−a
x= + xd f(x)
2 2
b + a b − a ⎛ −1 ⎞
xotrans = + ⎜ ⎟
2 2 ⎝ 3⎠
b+a b−a⎛ 1 ⎞
x1trans = + ⎜ ⎟
2 2 ⎝ 3⎠ xo =
−1
x1 =
1
-1
3 3
1 xd
b−a
dx = dxd x
2 a xotrans x1trans b
b−a
[ ]
b
I = ∫ f ( x)dx ≈ c0 f ( xotrans ) + c1 f ( x1trans )
a
2
11
Gauss-Legendre Quadrature – Simple 2 point Example
Integrate the following function from x=0.2 to 0.8:
f ( x) = 4 x 4 + 2 x 2 − 1
.8 .8
I = ∫ f ( x)dx = ∫ 4 x 4 + 2 x 2 − 1dx
.2 .2
1
I = ∫ f ( xd )dxd ≈ f (−1 / 3 ) + f (1 / 3 )
−1
Step 1: Transform limits and Gauss points (xo & x1) from general form
b + a b − a ⎛ −1 ⎞ ⎛ −1 ⎞
xotrans = + ⎜ ⎟ = 0.5 + 0.3⎜ ⎟ = 0.3267949
2 2 ⎝ 3⎠ ⎝ 3⎠
b+a b−a⎛ 1 ⎞ ⎛ 1 ⎞
x1trans =+ ⎜ ⎟ = 0.5 + 0.3⎜ ⎟ = 0.6732050
2 2 ⎝ 3⎠ ⎝ 3⎠
Step 2: perform summation
b−a
[ ]
b
I = ∫ f ( x)dx ≈ c0 f ( xotrans ) + c1 f ( x1trans )
a
2
I ≈ 0.3[(1) f (0.3267949) + (1) f (0.6732050)]
[
I ≈ 0.3 4(0.3267949 ) + 2(0.3267949 ) − 1 + 4(0.673205) + 20.6732052 − 1
4 2 4
]
Error Estimate n-point Gauss-Legendre
Formula
2 2 n +1 [n!]
4
Et = f (2 n ) (ξ ) −1 < ξ < 1
(2n + 1)[(2n )!]3
Where n is the number of points in the formula (remember,
a n-point formula integrates a polynomial of 2n-1 exactly!)
f (2 n ) (ξ ) is the (2n)th derivative after the change of variable
If the magnitude of the higher order derivatives decrease or
only increase slowly with increasing n, Gauss formulas are
Significantly more accurate than Newton-Cotes formulas.
Matlab Multipoint Example
Built in Matlab integration methods
I=trapz(y)*dx
f=inline(‘x^2+1’)
I=quad(f,0,2)
Integration limits
12