Ho Chi Minh City University of Technology (HCMUT), VNU-HCM
CHAPTER 3
NUMERICAL INTEGRATION
AND DIFFERENTIATION
Department of Machine Design - Faculty of Mechanical Engineering
Ho Chi Minh City University of Technology (HCMUT), VNU-HCM
OUTLINE
3.1 Numerical Integration
3.2 Numerical Differentiation
Department of Machine Design - Faculty of Mechanical Engineering
Ho Chi Minh City University of Technology (HCMUT), VNU-HCM
3.1 Numerical Integration
Figure 3.1: Examples of how integration is used to evaluate areas in engineering
applications.
(a) Evaluating the area of a field bounded by a meandering stream and tow roads.
(b) Evaluating the cross-sectional area of a river.
(c) Determining the net force due to a nonuniform wind blowing against the side of
a skyscraper.
3
Department of Machine Design - Faculty of Mechanical Engineering
Ho Chi Minh City University of Technology (HCMUT), VNU-HCM
3.1 Numerical Integration
Newton-Cotes Integration Formulas
• This is the most common numerical integration schemes.
• Base 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
where:
f n ( x) = a0 + a1 x + a2 x 2 + ... + an x n
Department of Machine Design - Faculty of Mechanical Engineering
Ho Chi Minh City University of Technology (HCMUT), VNU-HCM
3.1 Numerical Integration
3.1.1 The Trapezoidal Rule
Using first order Taylor series to approximate 𝑓 𝑥 ,
b b
I = ò f ( x)dx » ò f1 ( x)dx
a a
where:
f (b) - f (a )
f1 ( x) = f (a ) + ( x - a)
b-a
Then:
bé f (b) - f (a ) ù f (b) + f (a )
I » ò ê f (a ) + ( x - a ) ú = (b - a )
a
ë b-a û 2
Department of Machine Design - Faculty of Mechanical Engineering
Ho Chi Minh City University of Technology (HCMUT), VNU-HCM
3.1 Numerical Integration
3.1.1 The Trapezoidal Rule
The trapezoidal rule is
equivalent to approximating
the area of the trapezoidal
under the straight line
connecting 𝑓(𝑎) and 𝑓 𝑏 .
An estimated for the local
truncation error of a single
application of the
trapezoidal rule is:
Figure 3.2: Graphical depiction of -1
f "( x)(b - a )3
the trapezoidal rule. 12
Department of Machine Design - Faculty of Mechanical Engineering
Ho Chi Minh City University of Technology (HCMUT), VNU-HCM
3.1 Numerical Integration
3.1.1 The Trapezoidal Rule
Example 3.1: Use the trapezoidal rule to numerically integrate 0.2 + 25x
From a=0 to b=2
Solution:
2 2
The true solution is ò0
f ( x)dx = (0.2 + 12.5 x 2 )
0
= 50.4
f (a ) = f (0) = 0.2
f (b) = f (2) = 50.2
f (b) + f (a ) 0, 2 + 50
I = (b - a ) = (2 - 0) = 50.4
2 2
Because 𝑓(𝑥) is a linear function, using the trapezoidal rule gets the
exact solution.
7
Department of Machine Design - Faculty of Mechanical Engineering
Ho Chi Minh City University of Technology (HCMUT), VNU-HCM
3.1 Numerical Integration
3.1.1 The Trapezoidal Rule
Example 3.2: Use the trapezoidal rule to numerically integrate
f ( x) = 0.2 + 25 x + 3 x 2 from a=0 to b=2
Solution: f (0) = 0.2; f (2) = 62.2
f (b) + f (a )
I = (b - a ) = 62.4
2
The exact solution is
2 2
ò f ( x)dx = (0.2 x + 12.5 x + x ) = 58.4
2 3
0 0
The relative error is:
58.4 - 62.4
et = 100% = 6.85%
58.4
8
Department of Machine Design - Faculty of Mechanical Engineering
Ho Chi Minh City University of Technology (HCMUT), VNU-HCM
3.1 Numerical Integration
3.1.1 The Trapezoidal Rule
Multiple-application trapezoidal rule:
Using smaller integration interval can reduce the approximation
error. Divide (a,b) into n segments of equal width.
f ( x0 ) + 2å i =1 f ( xi ) + f ( xn )
n -1
I » (b - a )
2n
Error:
(b - a )3
Ea = - 2
f "( x)
12n
9
Department of Machine Design - Faculty of Mechanical Engineering
Ho Chi Minh City University of Technology (HCMUT), VNU-HCM
3.1 Numerical Integration
3.1.1 The Trapezoidal Rule
Multiple-application trapezoidal rule:
Example 3.3: Use the 2-segments trapezoidal rule to numerically
integrate 𝑓 𝑥 = 0.2 + 25𝑥 + 3𝑥 ! from a=0 to b=2
Solution:
(b - a )
n = 2; h = =1
n
f (0) = 0.2; f (1) = 28.2; f (2) = 62.2
f (0) + 2 f (1) + f (2)
I = (b - a ) = 59.4
2n
10
Department of Machine Design - Faculty of Mechanical Engineering
Ho Chi Minh City University of Technology (HCMUT), VNU-HCM
3.1 Numerical Integration
3.1.1 The Trapezoidal Rule
Multiple-application trapezoidal rule:
Example 3.3:
Error: f "( x) = 6
2
f "( x) =
ò 0
6dx
=6
2-0
(2 - 0)3
Ea = - 2
6 = -1
12.2
The exact solution is 59.4 − 1 = 58.4
The relative error is 1.71%
11
Department of Machine Design - Faculty of Mechanical Engineering
Ho Chi Minh City University of Technology (HCMUT), VNU-HCM
3.1 Numerical Integration
3.1.2 Simpson’s Rule
Aside from applying the trapezoidal rule with finer segmentation,
another way to improve the estimation accuracy is to use higher
order polynomials.
Multiple-application Simpson’s rule:
b
hé m m -1
ù
ò f ( x)dx » ê f 0 + f 2 m + 4å f 2 k -1 + 2å f 2 k ú
a
3ë a b k =1 k =1 û
odd even
(b - a )5 (4)
Error: Ea = - 4
f
180n
Where 𝑓 ̅(") is the average fourth derivative for the interval.
12
Department of Machine Design - Faculty of Mechanical Engineering
Ho Chi Minh City University of Technology (HCMUT), VNU-HCM
3.1 Numerical Integration
3.1.2 Simpson’s Rule num of interval
Example 3.4: With n=4, estimate the integral of
𝑓 𝑥 = 0.2 + 25𝑥 − 200𝑥 $ + 675𝑥 % − 900𝑥 " + 400𝑥 &
From a=0 to b=0.8. The exact integral is 1.640533
Solution: h=0.2
0.2 + 4(1.288 + 3.464) + 2(2.456) + 0.232
I @ 0.8 = 1.623467
f(0) 0.2 12
f(0.2) 1.288 𝐸' = 1.640533 − 1.623467 = 0.017067
f(0.4) 2.456
𝜀' = 1.04%
f(0.6) 3.464
f(0.8) 0.232
-0.85
The estimated error is Ea = 4
(-2400) = 0.017067
180(4) 13
Department of Machine Design - Faculty of Mechanical Engineering
Ho Chi Minh City University of Technology (HCMUT), VNU-HCM
3.1 Numerical Integration
3.1.2 Simpson’s Rule
0.6 1
Example 3.5: Estimate f ( x) = ò dx, n = 6
0 1+ x
Solution:
x y 0.1
I= {[ f (0) + f (0.6) + 4[ f (0.1) + f (0.3) + f (0.5)]
0 𝑓(0) = 1 3
+ 2[ f (0.2) + f (0.4)]} = 0.470
0,1 𝑓(1) = 0.909
0,2 𝑓(2) = 0.833 0, 65
Ea = - 8.47 = -0.000003
0,3 𝑓(3) = 0.769 180 ´ 6 4
0,4 𝑓(4) = 0.714
0,5 𝑓(5) = 0.667 => True integral ≈ 0.46997
0,6 𝑓(6) = 0.625
14
Department of Machine Design - Faculty of Mechanical Engineering
Ho Chi Minh City University of Technology (HCMUT), VNU-HCM
Problem 1. Evaluate the following integral:
p /2
ò (6 + 3cos x)dx
0
a) Multiple-application trapezoidal rule, with n=4.
b) Multiple-application Simpson’s rule, with n=4.
c) Determine the percent relative error.
15
Department of Machine Design - Faculty of Mechanical Engineering
Ho Chi Minh City University of Technology (HCMUT), VNU-HCM
Problem 1.
Solution:
a) Multiple-application trapezoidal rule.
𝜋
2 −0
ℎ= = 𝜋/8
4
𝑥( = 0; 𝑥) = 𝜋/8; 𝑥$ = 𝜋/4; 𝑥% = 3𝜋/8; 𝑥" = 𝜋/2
p f (0) + f (p / 2) + 2[ f (p / 8) + f (p / 4) + f (3p / 8)]
I=
8 2
p p p 3p
= [9 + 6 + 6[6 + cos + cos + cos ]
16 8 4 8
» 12.386
16
Department of Machine Design - Faculty of Mechanical Engineering
Ho Chi Minh City University of Technology (HCMUT), VNU-HCM
Problem 1.
Solution:
b) Multiple-application Simpson’s rule.
𝜋
2 −0
ℎ= = 𝜋/8
4
𝑥" = 0; 𝑥# = 𝜋/8; 𝑥! = 𝜋/4; 𝑥$ = 3𝜋/8; 𝑥% = 𝜋/2
p f (0) + 4[ f (p / 8) + f (3p / 8)] + 2 f (p / 4) + f (p / 2)
I=
8 3
p
= [9 + 12(4 + cos(p / 8) + cos(3p / 8)) + 2(6 + 3cos(p / 4)) + 6]
24
» 12.4252 17
Department of Machine Design - Faculty of Mechanical Engineering
Ho Chi Minh City University of Technology (HCMUT), VNU-HCM
Problem 1.
Solution:
c) Determine the percent relative error.
p /2 p /2 p /2
ò (6 + 3cos x)dx = 6 ò dx + 3 ò cos xdx
0 0 0
= 3p + 3 » 12.42478
Multiple-application trapezoidal rule:
12.386 - 12.42478
eT = 100% = -0.312%
12.42478
Multiple-application Simpson’s rule
12.4252 - 12.42478
eT = 100% = 0.003%
12.42478
18
Department of Machine Design - Faculty of Mechanical Engineering
Ho Chi Minh City University of Technology (HCMUT), VNU-HCM
3.2 Numerical Differentiation
3.2.1 First Order Derivatives
The first forward finite divided difference is
f ( xi + 1) - f ( xi )
f '( xi ) »
h
The first backward finite divided difference is
f ( xi ) - f ( xi - 1)
f '( xi ) »
h
The first centered finite divided difference is
f ( xi + 1) - f ( xi - 1)
f '( xi ) »
2h
19
Department of Machine Design - Faculty of Mechanical Engineering
Ho Chi Minh City University of Technology (HCMUT), VNU-HCM
3.2 Numerical Differentiation
Figure 3.3: Graphical depiction of forward, backward and centered
finite-divided-difference approximations of the first derivative.
20
Department of Machine Design - Faculty of Mechanical Engineering
Ho Chi Minh City University of Technology (HCMUT), VNU-HCM
3.2 Numerical Differentiation
Example 3.6: Estimate the first derivative of f(x) at x=0,5 and h=0,5
f ( x) = -0.1x 4 - 0.15 x 3 - 0.5 x 2 - 0.25 x + 1.2
Solution:
ì xi = 0.5 ì f ( xi ) = 0.925
ï ï
í xi -1 = xi - h = 0.5 => í f ( xi -1 ) = 1.2
ïx = x + h = 1 ï f ( x ) = 0.2
î i +1 i î i +1
The forward divided difference:
f ( xi + 1) - f ( xi )
f '( xi ) » = -1.45
h
The backward divided difference:
f ( xi ) - f ( xi - 1)
f '( xi ) » = -0.55
h 21
Department of Machine Design - Faculty of Mechanical Engineering
Ho Chi Minh City University of Technology (HCMUT), VNU-HCM
3.2 Numerical Differentiation
Example 3.6
Solution:
The centered divided difference:
f ( xi + 1) - f ( xi - 1)
f '( xi ) » = -1.0
2h
Exact differentiate: -0,9125
=> Using centered finite divided difference and small step size
achieves lower approximation error
22
Department of Machine Design - Faculty of Mechanical Engineering
Ho Chi Minh City University of Technology (HCMUT), VNU-HCM
3.2 Numerical Differentiation
3.2.2 High Order Derivatives
The second forward finite divided difference is
f ( xi + 2 ) - 2 f ( xi +1 ) + f ( xi )
f "( xi ) »
h2
The second backward finite divided difference is
f ( xi ) - 2 f ( xi -1 ) + f ( xi - 2 )
f "( xi ) »
h2
The second centered finite divided difference is
f ( xi +1 ) - 2 f ( xi ) + f ( xi -1 )
f "( xi ) »
h2 23
Department of Machine Design - Faculty of Mechanical Engineering
Ho Chi Minh City University of Technology (HCMUT), VNU-HCM
3.2 Numerical Differentiation
3.2.2 High Order Derivatives
Example 3.7: Evaluate the second derivatives of
f ( x) = -0.1x 4 - 0.15 x 3 - 0.5 x 2 - 0.25 x + 1.2( xi = 0.5; h = 0.25)
Solution:
ì xi = 0.5 ì f ( xi ) = 0.925 Using the forward f.d.d: f "( xi ) = -1.155
ï x = 0.25 ï f ( x ) = 1.1035
ïï i -1 ïï i -1
Using the backward f.d.d: f "( xi ) = -1.312
í xi - 2 = 0 => í f ( xi - 2 ) = 1.2
ï x = 0.75 ï f ( x ) = 0.6363 Using the centered f.d.d: f "( xi ) = -1.7632
ï i +1 ï i +1
ïî xi + 2 = 1 ïî f ( xi + 2 ) = 0.2 Exact differentiate: -1.75
24
Department of Machine Design - Faculty of Mechanical Engineering
Ho Chi Minh City University of Technology (HCMUT), VNU-HCM
Problem 2. Use the following data to find the velocity and
acceleration at t = 10 seconds. Use centered finite difference.
Time, t, s 0 2 4 6 8 10 12 14 16
Position, x, m 0 0.7 1.8 3.4 5.1 6.3 7.3 8.0 8.4
25
Department of Machine Design - Faculty of Mechanical Engineering
Ho Chi Minh City University of Technology (HCMUT), VNU-HCM
Solution:
• Velocity: f ( xi +1 ) - f ( xi -1 )
f '( xi ) =
2h
f ( x6 ) - f ( x4 )
f '( x5 ) =
2h
f (12) - f (8) 7.3 - 5.1
f '(10) = = = 0.55(m / s )
2´ 2 2´ 2
• Acceleration:
f ( xi +1 ) - 2 f ( xi ) + f ( xi -1 )
f "( xi ) =
h2
f ( x6 ) - 2 f ( x5 ) + f ( x4 )
f "( x5 ) =
h2
f (12) - 2 f (10) + f (8) 7.3 - 2 ´ 6.3 + 5.1
f "(10) = 2
= 2
= -0.05( m / s 2
)
2 2 26
Department of Machine Design - Faculty of Mechanical Engineering
Ho Chi Minh City University of Technology (HCMUT), VNU-HCM
Problem3. Determine y’(1) and y”(1) of 𝑦 𝑥 = 𝑐𝑜𝑠 " !
𝑥 ,
with h=0,1.
Result:
𝑦 * 1 = −0,17824017
𝑦"(1) = 0,3573462
27
Department of Machine Design - Faculty of Mechanical Engineering