0% found this document useful (0 votes)
88 views59 pages

Numerical Integration (SV)

This document discusses numerical integration techniques. It introduces the trapezoidal rule and composite trapezoidal rule for numerically evaluating integrals. It provides examples of using these rules to integrate various functions and compares the results to analytical solutions or more precise numerical methods using more segments.

Uploaded by

KOOK MING-JO
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
88 views59 pages

Numerical Integration (SV)

This document discusses numerical integration techniques. It introduces the trapezoidal rule and composite trapezoidal rule for numerically evaluating integrals. It provides examples of using these rules to integrate various functions and compares the results to analytical solutions or more precise numerical methods using more segments.

Uploaded by

KOOK MING-JO
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 59

BSC207 / BSC208

Numerical Methods in Engineering

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

• b) Continuous Data Integration


b
ò f ( x ) dx
y = a b-a

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 øúû

• What if we encounter a function we cannot integrate


analytically?

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

What is the Integral


between x=a and x=b?

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?

• The truncation error estimate of the Trapezoidal Rule is:


1
Et = - f ¢¢(x )(b - a )
3

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

𝑓 $$ 𝑥 = −400 + 4050𝑥 − 10800𝑥 & + 8000𝑥 %

We need average value of 𝑓′′(𝜉)

16
Trapezoidal rule error
1 $$ %
𝐸# = − 𝑓 𝜉 𝑏 − 𝑎
12

𝑓 $$ 𝑥 = −400 + 4050𝑥 − 10800𝑥 & + 8000𝑥 %

17
Estimating Truncation Error
• Functions that have quickly changing gradients will have
large Trapezoidal Rule errors

f ( x) = 0.2 + 25 x - 200 x 2 + 675x 3 - 900 x 4 + 400 x 5


f " ( x) = -400 + 4050 x - 10800x 2 + 8000 x 3
Integral is more
f ( x) = 0.2 + 25 x - 200 x + 675x - 900 x
2 3 4
erroneous with high
curvature models
f " ( x) = -400 + 4050 x - 10800x 2

f ( x) = 0.2 + 25 x Linear f(x) has


zero truncation
f " ( x) = 0 error!

18
Numerical integration
• Can we estimate the integral more accurately by using
multiple segments?

• The error 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.

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

Integrate multiple segments reduces the truncation error significantly

Smaller step size leads to better accuracy (smaller truncation error)

24
Dealing with Unequal Segments
• In many engineering situations, the independent data is not
evenly spaced

• This is because in many cases the experimenter has no


control over the independent variables

• Having unequal segments is also useful as it allows finer


integration in parts of the curve that “turns” sharply

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.

• Using 2nd and 3rd degree polynomials help reduce


truncation error

• The formulas that result from taking the integrals under


these polynomials are called Simpson’s rules.

27
Trapezoidal rule
• Trapezoidal Rule (Composite, n=4)

Degree 1: linear lines joining data points together

28
Simpson’s rules

• Simpson’s Rules fit quadratic and cubic curves to multiple


data points
Simpson’s 1/3 Rule Simpson’s 3/8 Rule
(Degree 2) (Degree 3)

Similar to 2-segment Similar to 3-segment


composite trapezoidal composite trapezoidal
rule, but uses parabola rule, but uses x3 curve
curve instead of instead of straight line
straight line

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

• Another way to write Simpson’s 1/3 Rule is


f (x1 ) + 4 f (x2 ) + f (x3 )
I = (b - a)
6

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%

Simpson’s 1/3 Rule


εt = 16.64%

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

• The error is dependent upon the fourth-derivative of the


function being integrated and the integration limits
• Cubic (x3) function f(x) can be integrated perfectly with
Simpson’s 1/3 Rule
d d d d

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

Much smaller errors


than trapezoidal

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

• However, there must be an odd number of points (n = 3, 5,


7 … etc)
• In other words, even number of segments

• Because of the heavy weighting of the internal points, the


formula is a little more complicated than the trapezoidal rule

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

• Each integral is replaced with Simpson’s 1/3 Rule


h h h
I = [ f x1 + 4 f x2 + f x3 ] + [ f x3 + 4 f x4 + f x5 ] +  + [ f (xn-2 ) + 4 f (xn-1 ) + f (xn )]
( ) ( ) ( ) ( ) ( ) ( )
3 3 3
• There is a common factor of h/3 with the odd and even f(x)
terms overlapping (1,4,2,…4,2,4,1 pattern)
é n -1 n-2
ù Composite
f (x1 ) + 4 å f (xi ) + 2 å f (x j ) + f (xn )ú

I= Simpson 1/3
3ê i = 2 , 4 , 6 ,... j =3, 5, 7 ,...
ú
Rule
êë i , even j , odd úû

39
Composite Simpson’s 1/3 Rule
é n -1 n-2
ù
f (x1 ) + 4 å f (xi ) + 2 å f (x j ) + f (xn )ú

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

• The single Simpson’s 1/3 Rule integration used two


segments (n=3) but reduced the error of the Composite
Trapezodial rule from 34.85% to 16.64%

• Can we improve this result further by applying the


Simpson’s 1/3 Rule twice (n=5, that is, four segments)

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

• Generally, Simpson’s 3/8 rule


is used with the 1/3 rule when
there is an even number of
points (n=4,6,8 … etc)

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

• As the denominator is larger than the error equation for


Simpson’s 1/3 Rule, the 3/8 rule is a little bit more accurate

• However, as the 1/3 rule only requires 3 points, it is more


commonly used
• The 3/8 Rule is usually applied once in combination with
the 1/3 Rule when dealing with an even number of
points (n = 4,6,8 etc)

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

• Again, integrating from a=0 to b=0.8 but using Simpson’s


3/8 Rule instead of Simpson’s 1/3 rule
• For a single application of the 3/8 Rule, we need to have 4
evenly spaced points (n = 4, h = 0.8/3)
• Evaluating f(x) at these points give
f (0) = 0.2 f (0.2667) = 1.432724
f (0.5333) = 3.487177 f (0.8) = 0.232

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

Simpson’s 1/3 Rule

Composite
Simpson’s 1/3 Rule
Simpson’s 3/8

50
Combining Simpson’s 1/3 and 3/8 Rule

• What if we want to integrate f(x)


over 5 segments, n=6?
• Simpson’s 1/3: n is not odd!
• Composite Simpson’s 1/3: 1
missing segment!
• Simpson’s 3/8: 2 extra
segments!
• Composite Simpson’s 3/8: 2
missing segments!
• We can combine the Simpson’s
1/3 AND 3/8 rules to integrate
f(x) from a=0 to b=0.8

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

Simpson’s 1/3 Rule


Composite
Simpson’s 1/3 Rule
Simpson’s 3/8
A Simpson’s 1/3
+
A Simpson’s 3/8

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

You might also like