Lect12_NumericalIntegration
Lect12_NumericalIntegration
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?
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
n = 1 Æ straight line
n = 2 Æ parabola
n=1 n=2
a b x a b x
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
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 ⎦
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
⎡ ( 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
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 ⎦
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
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).
a x0 x1 b x
(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
b − a = c0 + c1
-(b-a)/2 (b-a)/2 x
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
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
f(x)
a x0 x1 b x
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!
(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
( ) (
I = f −1/ 3 + f 1/ 3 ) 3rd order accurate
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
1 n
∫ f ( x)dx = ∑ ci f ( xi )
−1 i =0
f(x)
-1 x0 x1 1 x
∫ 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.
−1 1
-1 xo = x1 = 1 xd
3 3
⎛ −1 ⎞ ⎛ −1 ⎞
I ≈ f⎜ ⎟+ f ⎜ ⎟
⎝ 3⎠ ⎝ 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
]
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
I=trapz(y)*dx
f=inline(‘x^2+1’)
I=quad(f,0,2)
Integration limits
12