Metnum 2 Integration RM
Metnum 2 Integration RM
Numerical Method
Robiatul Muztaba, S.Si., M.Si.
SAP – ITERA
Quadrature
December 6, 2023 2
Outline
• Definite Integrals
• Lower and Upper Sums
– Reimann Integration or Reimann Sums
• Uniformly-spaced samples
– Trapezoid Rules
– Romberg Integration
– Simpson’s Rules
– Adaptive Simpson’s Scheme
• Non-uniformly spaced samples
– Gaussian Quadrature Formulas
December 6, 2023 3
Motivation
What does an integral represent?
d b
f ( x)dxdy = volume
b
a
f ( x)dx = area c a
where x = b − a
n
analytically.
• Necessary when either:
– Integrand is too complicated to integrate analytically
2 2 + cos(1 + x ) 0.5 x
0
1 + 0.5 x
e dx
a
i =1
in which wi are the weights, xi are the sampling points, and Et is the
truncation error
• Valid for any function that is continuous on the closed and bounded
interval of integration.
December 6, 2023 6
Partitioning the Integral
• The most common numerical integration formula is based on equally
spaced data points.
xn
x0
f ( x)dx
December 6, 2023 7
Upper Sums
f ( x) M ( x
i i +1 − xi )
• where x0 i =0
December 6, 2023 8
Upper Sums
• Graphically:
x0 x1 x2 x3 x4
December 6, 2023 9
Lower Sums
xn n −1
• where f ( x) m ( x
i i +1 − xi )
x0 i =0
mi = inf f ( x) : xi x xi +1
Infimum - greatest
lower bound
December 6, 2023 10
Lower Sums
• Graphically
x0 x1 x2 x3 x4
December 6, 2023 11
Finer Partitions
• We now have a bound on the integral of the function for some
partition (x0,..,xn):
n −1 xn n −1
m (x
i =0
i i +1 − xi ) f ( x) M i ( xi +1 − xi )
i =0
x0
• As n→, one would assume that the sum of the upper bounds and
the sum of the lower bounds approach each other.
• This is the case for most functions, and we call these Riemann-
integrable functions.
December 6, 2023 12
Bounding the Integral
• Graphically
x0 x1 x2 x3 x4
December 6, 2023 13
Bounding the Integral
x0 x3 x5 x7 x9
December 6, 2023 14
Bounding the Integral
x0 x5 x7 x9 x11
December 6, 2023 15
Monotonic Functions
x0 x3 x5 x7 x9
December 6, 2023 16
Lab1 and Integration
December 6, 2023 17
Polynomial Approximation
• Rather than search for the maximum or minimum, we replace f(x)
with a known and simple function.
• Within each interval we approximate f(x) by an mth order
polynomial.
pm ( x) = a0 + a1 x + a2 x 2 + ... + am x m
December 6, 2023 18
Newton-Cotes Formulas
• The m’s (order of the polynomials) may be the same or different.
xn x0+m1 x0+m1+m2 xn
x0
f ( x)dx =
x0
pm1 ( x)dx +
x0+ m1
pm2 ( x)dx + ... +
xn−mn
pmn ( x)dx
I = f ( x )dx p1 (x )dx
b b
a a
December 6, 2023 20
Trapezoid Rule
f (b ) − f (a )
I f (a ) + (x − a )dx
b
a
b−a
Trapezoid Rule
f(b)
I
(b − a)
f (a ) + f (b)
f(a)
2
• Improvement?
x0 x1 x2 x3 x4
December 6, 2023 22
Trapezoid Rule Error
• The integration error is:
(b − a )
f ( )h 3 = − f ( )h 2
1
Et = −
12 12
O(h 2 )
December 6, 2023 23
Example
− x2
Integrate from f ( x) = e a = 0 to b = 2
2
I = e − x2
dx
0
(b − a)
f (a ) + f (b) = ( 2 − 0)
f (2) + f (0)
2 2
= 1 (e −4 + e0 ) = 1.0183
December 6, 2023 24
Example
23 f (0) + f (2 )
Et E a = − = 0.58
12 2
December 6, 2023 25
More intervals, better result [error O(h2)]
7 7
6
n=2 6
n=3
5 5
4 4
3 3
2 2
1 1
0 0
3 5 7 9 11 13 15 3 5 7 9 11 13 15
7 7
6
n=4 6
n=8
5 5
4 4
3 3
2 2
1 1
0 0
3 5 7 9 11 13 15 3 5 7 9 11 13 15
December 6, 2023 26
Composite Trapezoid Rule
• If we do multiple intervals, we can avoid duplicate function
evaluations and operations:
• Use n+1 equally spaced points.
b−a
• Each interval has: h=
n
• Break up the limits of integration and expand.
a +h a +2h
f (x )dx + f (x )dx + ... + f (x )dx
b
I =
a a +h b−h
December 6, 2023 27
Composite Trapezoid Rule
• Substituting the trapezoid rule for each integral.
a+h a+2h
I = f ( x ) dx + a+h f ( x ) dx + ... f ( x ) dx
b
+
a b−h
(a + h − a) ( a + 2h − a − h )
= f ( a ) + f ( a + h ) + f ( a + h ) + f ( a + 2h )
2 2
(b − b + h)
+.... + f ( b − h ) + f ( b )
2
h n −1
I = f (a ) + 2 f (a + ih ) + f (b )
2 i =1
n −1
f (a ) + 2 f (a + ih ) + f (b )
= (b − a ) i =1
2n
width
Average height
December 6, 2023 29
Error
• Where, is thef average second derivative.
• If n is doubled, h → h/2 and Ea → Ea/4
• Note, that the error is dependent upon the width of the area
being integrated.
December 6, 2023 30
Example
• from 40
a=0.2 35
to 30
b=0.8 25
20
15
10
0
0 0.2 0.4 0.6 0.8 1 1.2
December 6, 2023 31
Example
12
December 6, 2023 32
Example
f ( x ) =
0.2
f dx
0.8 − 0.2
f (0.8) − f (0.2)
= = −131.6
0.8 − 0.2
December 6, 2023 33
Example
Et = f = 2
f
12 12n
1
= − ( −131.6 )( 0.8 − 0.2 ) = 2.37
3
12
December 6, 2023 34
True value of integral is 12.82. Trapezoid
rule is 11.26 - within approx error - Et is 12%
40
35
30
25
20
15
10
0
0 0.2 0.4 0.6 0.8 1 1.2
December 6, 2023 35
Using Three Intervals
35
30
25
20
15
10
0
0 0.2 0.4 0.6 0.8 1 1.2
December 6, 2023 37
Using Six Intervals
35
30
25
20
15
10
0
0 0.2 0.4 0.6 0.8 1 1.2
December 6, 2023 39
We will back with the same topic on the next week.
THANKS FOR YOUR ATTENTION
December 6, 2023 40
December 6, 2023 OSU/CSE 541 41
December 6, 2023 OSU/CSE 541 42
Multi-dimensional Integration
f ( x, y ) dxdy Ai f , y dy
0 0 0 i =0 n
i
n 1
= Ai f , y dy
i =0 0 n
n
i j n
Ai Aj f ,
i =0 j =0 n n
n
i j
n
= Ai A j f ,
i =0 j =0 n n
December 6, 2023 OSU/CSE 541 43
Multi-dimensional Integration
1 2 2 2 2 2 1
• If the dimension is high, this leads to a significant amount
of additional work in going from h→h/2.
– Remember this for Monte-Carlo Integration.
h n −1
I h = f ( a ) + 2 f ( a + ih ) + f ( b )
2 i =1
h 2 n −1
h
I h = f (a) + 2 f a + i + f (b)
2 4 i =1 2
h n −1 n −1
h
= f (a) + 2 f ( a + ih ) + 2 f a + ih + + f (b)
4 i =1 i =0 2
I h h n −1 h
= + 2 f a + ih +
2 4 i =0 2
December 6, 2023 OSU/CSE 541 49
Recall Richardson Extrapolation
A= A
• Suppose we reduce + Oto
n
step
(h1 )size (hh1 2)
A = A(h2 ) + O (h2 )
n
order of error in
• where c is a constant trapezoidal rule
• Therefore:
E (h1 ) h1
2
2
E (h2 ) h2
December 6, 2023 OSU/CSE 541 53
Richardson Extrapolation
I = I (h2 ) + E (h2 )
December 6, 2023 OSU/CSE 541 55
Richardson Extrapolation
• Leads to:
I I (h2 ) +
1
I (h2 ) − I (h1 )
(h1 / h2 ) − 1
2
• Letting h2 = h1 /2
I I (h2 ) + 2
1
I (h2 ) − I (h1 )
2 −1
I I (h2 ) − I (h1 )
4 1
3 3
December 6, 2023 OSU/CSE 541 56
Romberg Integration
I I (hm ) − I (hl )
16 1
15 15
I (hm ) − I (hl )
64 1
I
63 63
December 6, 2023 OSU/CSE 541 58
Romberg Integration
• Trapezoid Rules:
k=0 k=1
k intervals h Integral
j j=0 1 0.8 0.1728
j=1 2 0.4 1.0688
j=2 4 0.2 1.4848
4 1
I = (1.0688) − (0.1728) = 1.3674667
3 3 (j=1, k=1)
Exact integral is 1.64053334
December 6, 2023 OSU/CSE 541 62
Example
k=0 k=1
k
j segments h O (h 2 ) O (h 4 )
1 0.8 0.1728
2 0.4 1.0688 1.3674667
4 0.2 1.4848
4 1
I = (1.4848) − (1.0688) = 1.62346667 (j=2, k=1)
3 3
Exact integral is 1.64053334
December 6, 2023 OSU/CSE 541 63
Example
k=1 k=2
k
segments h O (h 2 ) O (h 4 )
j
1 0.8 0.1728
2 0.4 1.0688 1.3674667
4 0.2 1.4848 1.62346667
16 1
(j=2, k=2) I = 15 (1.62346667) − 15 (1.3674667) = 1.64053334
k k=3
• Recall:
xn x0+m1 x0+m1+m2 xn
x0
f ( x)dx =
x0
pm1 ( x)dx +
x0+ m1
pm2 ( x)dx + ... +
xn−mn
pmn ( x)dx
( x − x1 )( x − x2 ) (x − x0 )(x − x2 ) f (x )
f ( x0 ) +
x2
I =
x0 ( x − x )( x − x ) ( − )( − ) 1
0 1 0 2 x1 x0 x1 x 2
+
( x − x0 )( x − x1 )
f ( x2 )dx
(x2 − x0 )(x2 − x1 )
Quadratic
8 Polynomial
0
3 5 7 9 11 13 15
Et = −
h 5 (4 )
f ( ) = −
(b − a ) (4 )
5
f ( ) 4
90 2880
O(h )
b−a
h=
2
20
15
10
0
0 0.5 1 1.5 2 2.5 3 3.5 4 4.5
-5
x 0 x 2 x n −2
I = (b − a )
i =1,3,5 j = 2,4,6
3n
n+1 data points, an odd number
December 6, 2023 OSU/CSE 541 76
Composite Simpson’s 1/3 Rule
4 1 coefficients on
1
4 1 i=n numerator
1 1
1 4
i=0
• Error estimate: Et = −
h 5 (4 )
f ( )
90
I = f ( x )dx h f ( x0 ) + 4 f ( x1 ) + f ( x2 )
2 1
0 3
=
( 0.4 )
f (0) + 4 f (0.4) + f (0.8)
3
= 1.36746667
Exact integral is 1.64053334
December 6, 2023 OSU/CSE 541 81
Error
0.4 5 0.45
Et Ea =− f (4 )
( x1 ) = − f (4 )
(0.4 ) = 0.27306667
90 90
middle point
3n
f (0) + 4 f (0.2) + 2 f (0.4) + 4 f (0.6) + f (0.8)
= (0.8 − 0)
(3)( 4)
0.2 + 4(1.288 + 3.464) + 2( 2.456) + 0.232
= 0.8
12
= 1.6234667 Exact integral is 1.64053334
December 6, 2023 OSU/CSE 541 84
Error
0.25 (4 ) 0.25 (4 )
Et Ea = − f ( x2 ) = − f (0.4) = −0.0085
90 90
middle point
max
x 0,0.8
f ( 4)
( x) = f ( 4)
(0) = −21600
f ( 4)
(0.4) = 2400
f ( x) p3 ( x) = a0 + a1 x + a2 x 2 + a3 x 3
I = f (x )dx p3 (x )dx
x3 x3
x0 x0
b−a
h=
3
Simpson’s 1/3
Simpson’s 3/8
f ( x ) dx − f ( a ) + 4 f ( x1 ) + f ( b )
a 3
h
b
f ( x ) dx − f ( a ) + 4 f ( x1 ) + 2 f ( x2 ) + 4 f ( x3 ) + f ( b )
a 6
f ( x ) dx − f ( a ) + 4 f ( x1 ) + f ( x2 ) , and
a 6 2
h
b
f ( x ) dx − f ( x2 ) + 4 f ( x3 ) + f ( b ) , where
c 6 2
a+b
c = x2 =
2
• The left and the right are within one-half of the error.
b−a a+b
S ( a, b ) = f (a) + 4 f + f ( b ) , and
6 2
1 b − a ( 4)
5
E ( a, b ) = − f
90 2
S ( ) = S ( a, b ) , E ( ) = E ( a, b )
1 1
• Now, since
E ( 2)
= E ( a , c ) + E ( c, b )
• We can solve for E(2) in terms of E(1).
5 5
1 h / 2 ( 4) 1 h / 2 ( 4)
E(
2)
=− f − f
90 2 90 2
5
1 1 h ( 4) 1 (1)
= 4 − f = E
2 90 2 16
I =S ( 2)
+E ( 2)
=S ( 2)
+
1 ( 2)
15
S −S((1)
)
December 6, 2023 OSU/CSE 541 103
Adaptive Simpson’s Scheme
(S ( 2)
−S (1)
) 15
December 6, 2023 OSU/CSE 541 104
Adaptive Simpson’s Scheme
I=Ileft + Iright
−1
i =1
ci : weighting factors
xi : sampling points selected optimally New!!
(b − a) x + b + a 2t − b − a
t= x=
2 b−a
December 6, 2023 OSU/CSE 541 120
Guassian Quadrature
• As t = a x = -1
• As t = b x = 1
(b − a)
dt = dx
2
(b − a) x + b + a
f (t ) = f
2
b (b − a) 1 (b − a) x + b + a
a f (t )dt = 2 −1 f 2
dx
December 6, 2023 OSU/CSE 541 121
Guassian Quadrature
• Basic form of Gaussian quadrature:
n
I = f ( x)dx ci f ( xi )
1
−1
i =1
• What we need now, are four known values for the equation.
• If we had these, we could then attempt to solve for the four
unknowns.
• Let’s make it work for polynomials!!!
• f(x)=1 1dx = 2
−1
= c1 f (x1 ) + c2 f ( x2 ) = c1 x1 + c2 x2
– Linear 1
• f(x)=x xdx = 0
−1
– Quadratic
−1x dx = 3 = c1 f (x1 ) + c2 f (x2 ) = c1 x1 + c2 x2
1
2 2 2 2
• f(x)=x2
– Cubic
x 3 dx = 0 = c1 f (x1 ) + c2 f (x2 ) = c1 x1 + c2 x2
1
3 3
• f(x)=x3 −1
c1 = c2 = 1
1
x1 = − = −0.577
3
1
x2 = = 0.577
3
1 1
I f − + f
3 3
( ax + bx + cx + d )dx = a x dx + b x dx + c xdx + d dx
3 2 3 2
−1 −1 −1 −1 −1
−1 3 1 3 −1 2 1 2 −1 1
= a + + b + +c + + d (1 + 1)
3 3 3 3 3 3
( ax +bx +cx+d )
1
3 2 3
=
−1
3
-0.577 0.577
x
-1 1
December 6, 2023 OSU/CSE 541 129
Example
• Integrate f(x) from a = 0 to b = 0.8
f ( x ) = 0.2 + 25x − 200x 2 + 675x 3 − 900x 4 + 400x 5
• Transform from [0, 0.8] to [-1, 1]
b (b − a ) 1 (b − a ) x + b + a
a
f (t )dt =
2 −1
f
2
dx
−1
December 6, 2023 OSU/CSE 541 130
Example
• Solving
I = 0.4 f (0.4t + 0.4 )dt
1
−1
−1 t = 1 / 3
I 0.51674055+ 1.30583723= 1.82257778
Exact integral is 1.64053334
December 6, 2023 OSU/CSE 541 131
Higher-order Gaussian Quadrature
−1
i =1
= c1 f ( x1 ) + c2 f ( x2 ) + c3 f ( x3 ) = c1 + c2 + c3
1
1dx = 2
−1
= c1 f ( x1 ) + c2 f ( x2 ) + c3 f ( x3 ) = c1 x1 + c2 x2 + c3 x3
1
xdx = 0
−1
2
( ) ( ) ( )
1
−1 = = + + = + +
2 2 2 2
x dx c1 f x1 c2 f x 2 c3 f x3 c x
1 1 c x
2 2 c x
3 3
3
x 3dx = 0 = c1 f ( x1 ) + c2 f ( x2 ) + c3 f ( x3 ) = c1 x1 + c2 x2 + c3 x3
1
3 3 3
−1
2
( ) ( ) ( )
1
−1 = = + + = + +
4 4 4 4
x dx c1 f x1 c2 f x 2 c3 f x3 c x
1 1 c x
2 2 c x
3 3
5
x 5dx = 0 = c1 f ( x1 ) + c2 f ( x2 ) + c3 f ( x3 ) = c1 x1 + c2 x2 + c3 x3
1
5 5 5
−1
• Can solve these equations (or have some one smarter than us, like
Guass solve them).
c1 = 5 / 9 c2 = 8 / 9 c3 = 5 / 9
x1 = − 3 / 5 = −0.77459669 x2 = 0 x2 = 3 / 5 = 0.77459669
x
-0.775 0.775
December 6, 2023 -1 OSU/CSE 541 1 135
Example
3 8 3
+ f (0) +
5 5
I
f − f
9
5 9 9 5
−1
n 2 3 4 5 6
ci 1.0 0.5555555556 0.3478548451 0.2369268850 0.1713245
1.0 0.8888888889 0.6521451549 0.4786286705 0.3607616
0.5555555556 0.6521451549 0.5688888889 0.4679139
0.3478548451 0.4786286705 0.4679139
0.2369268850 0.3607616
0.1713245