Numerical Integration
Roger Crawfis
September 19, 2022 OSU/CSE 541 2
Quadrature
• We talk in terms of Quadrature Rules
– 1. The process of making something square. 2.
Mathematics The process of constructing a square equal
in area to a given surface. 3. Astronomy A configuration
in which the position of one celestial body is 90° from
another celestial body, as measured from a third.
– The American Heritage® Dictionary: Fourth Edition. 2000
September 19, 2022 OSU/CSE 541 3
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
September 19, 2022 OSU/CSE 541 4
What does an integral represent?
Basic definition of an integral:
Motivation
 
b
a
dx
x
f area
)
(   
d
c
b
a
dxdy
x
f volume
)
(

 




n
k
k
b
a n
x
x
f
dx
x
f
1
)
(
lim
)
(
sum of height  width
f(x)
x
n
a
b
x



where
September 19, 2022 OSU/CSE 541 5
• Evaluate the integral, without doing the
calculation analytically.
• Necessary when either:
– Integrand is too complicated to integrate analytically
– Integrand is not precisely defined by an equation, i.e.,
we are given a set of data (xi, yi), i = 1, 2, 3, …,n
Motivation


b
a
dx
x
f
I )
(
dx
e
x
x x
5
.
0
2
0
5
.
0
1
)
1
cos(
2
 


September 19, 2022 OSU/CSE 541 6
• Integration is a summing process. Thus virtually
all numerical approximations can be represented
by
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.
t
b
a
i
n
i
i E
x
f
w
dx
x
f
I 

  

)
(
)
(
1
Reimann Integral Theorem
September 19, 2022 OSU/CSE 541 7
• The most common numerical integration formula
is based on equally spaced data points.
• Divide [x0 , xn] into n intervals (n1)

n
x
x
dx
x
f
0
)
(
Partitioning the Integral
1 2
0
0 1 1
( ) ( ) ( ) ( )
n
n
n
x
x x
x
x
x x x
f x dx f x f x f x

   
   
September 19, 2022 OSU/CSE 541 8
Upper Sums
• Assume that f(x)>0 everywhere.
• If within each interval, we could determine
the maximum value of the function, then we
have:
• where
 
0
1
1
0
( )
n
x n
i i i
i
x
f x M x x



 


 
1
sup ( ):
i i i
M f x x x x
   Supremum - least
upper bound
September 19, 2022 OSU/CSE 541 9
Upper Sums
• Graphically:
x0 x1 x2 x4
x3
September 19, 2022 OSU/CSE 541 10
Lower Sums
• Likewise, still assuming that f(x)>0
everywhere.
• If within each interval, we could determine
the minimum value of the function, then we
have:
• where
 
0
1
1
0
( )
n
x n
i i i
i
x
f x m x x



 


 
1
inf ( ):
i i i
m f x x x x
  
Infimum - greatest
lower bound
September 19, 2022 OSU/CSE 541 11
Lower Sums
• Graphically
x0 x1 x2 x4
x3
September 19, 2022 OSU/CSE 541 12
Finer Partitions
• We now have a bound on the integral of the
function for some partition (x0,..,xn):
• 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.
   
0
1 1
1 1
0 0
( )
n
x
n n
i i i i i i
i i
x
m x x f x M x x
 
 
 
   
 

September 19, 2022 OSU/CSE 541 13
Bounding the Integral
• Graphically
x0 x1 x2 x4
x3
September 19, 2022 OSU/CSE 541 14
Bounding the Integral
• Halving each interval (much like Lab1):
x0 x3 x5 x9
x7
September 19, 2022 OSU/CSE 541 15
Bounding the Integral
• One more time:
x0 x5 x7 x11
x9
September 19, 2022 OSU/CSE 541 16
Monotonic Functions
• Note that if a function is monotonically
increasing (or decreasing), then the lower
sum corresponds to the left partition values,
and the upper sum corresponds to the right
partition values.
x0 x3 x5 x9
x7
September 19, 2022 OSU/CSE 541 17
Lab1 and Integration
• Thinking back to lab1, what were the limits
or the integration?
• Is the sin function monotonic on this
interval?
• Should the Reiman sum be an upper or
lower sum?
September 19, 2022 OSU/CSE 541 18
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.
m
m
m x
a
x
a
x
a
a
x
p 



 ...
)
( 2
2
1
0
September 19, 2022 OSU/CSE 541 19
• The m’s (order of the polynomials) may be the
same or different.
• Different choices for m’s lead to different
formulas:
1 1 2
1
0 0
1 2
0 0 0
( ) ( ) ( ) ... ( )
n m m m n
n
m n mn
x x x x
m m m
x x x x
f x dx p x dx p x dx p x dx
  
 
   
   
)
(
3/8
s
Simpson'
cubic
3
)
(
1/3
s
Simpson'
quadratic
2
)
(
Trapezoid
linear
1
Error
Formula
Polynomial
4
4
2
h
O
h
O
h
O
m
Newton-Cotes Formulas
September 19, 2022 OSU/CSE 541 20
• Simplest way to approximate the area under a
curve – using first order polynomial (a straight
line)
• Using Newton’s form of the interpolating
polynomial:
• Now, solve for the integral:
   

 

b
a
b
a
dx
x
p
dx
x
f
I 1
        
a
x
a
b
a
f
b
f
a
f
x
p 




1
Trapezoid Rule
September 19, 2022 OSU/CSE 541 21
Trapezoid Rule
      
 










b
a
dx
a
x
a
b
a
f
b
f
a
f
I
     
 
b
f
a
f
a
b
I 


2
a b
f(a)
f(b)
height
average
width

I
Trapezoid Rule
September 19, 2022 OSU/CSE 541 22
Trapezoid Rule
• Improvement?
x0 x1 x2 x4
x3
September 19, 2022 OSU/CSE 541 23
• The integration error is:
• Where h = b - a and  is an unknown point where
a <  < b (intermediate value theorem)
• You get exact integration if the function, f, is
linear (f = 0)
    2
3
12
)
(
12
1
h
f
a
b
h
f
Et 
 







 )
( 2
h
O
Trapezoid Rule Error
September 19, 2022 OSU/CSE 541 24
2
)
( x
e
x
f 

Integrate from a = 0 to b = 2
     
   
0183
.
1
)
(
1
)
0
(
)
2
(
2
)
0
2
(
2
0
4
2
0
2














e
e
f
f
b
f
a
f
a
b
dx
e
I x
Use trapezoidal rule:
Example
September 19, 2022 OSU/CSE 541 25
Estimate error:   3
12
1
h
f
Et 




Where h = b - a and a <  < b
Don’t know  - use average value
2
)
4
2
(
)
( 2 x
e
x
x
f 





2564
.
0
)
2
(
2
)
0
(







f
f
   
  58
.
0
2
2
0
12
23









f
f
E
E a
t
2
0
2 


h
Example
September 19, 2022 OSU/CSE 541 26
More intervals, better result [error  O(h2)]
0
1
2
3
4
5
6
7
3 5 7 9 11 13 15
n = 2
0
1
2
3
4
5
6
7
3 5 7 9 11 13 15
n = 3
0
1
2
3
4
5
6
7
3 5 7 9 11 13 15
n = 4
0
1
2
3
4
5
6
7
3 5 7 9 11 13 15
n = 8
September 19, 2022 OSU/CSE 541 27
• If we do multiple intervals, we can avoid duplicate
function evaluations and operations:
• Use n+1 equally spaced points.
• Each interval has:
• Break up the limits of integration and expand.
n
a
b
h


     


 







b
h
b
h
a
h
a
h
a
a
dx
x
f
dx
x
f
dx
x
f
I ...
2
Composite Trapezoid Rule
September 19, 2022 OSU/CSE 541 28
• Substituting the trapezoid rule for each integral.
• Results in the Composite Trapezoid Formula:
     
 
   
 
 
   
 
 
   
 
2
2
2
2 2
....
2
...
a h a h b
a a h b h
a h a a h a h
f a f a h f a h f a h
b b h
f b h f b
I f x dx f x dx f x dx
 
 
    
      
 
   
   
  
     








 


b
f
ih
a
f
a
f
h
I
n
i
1
1
2
2
Composite Trapezoid Rule
September 19, 2022 OSU/CSE 541 29
     
 
     
n
b
f
ih
a
f
a
f
a
b
b
f
ih
a
f
a
f
h
I
n
i
n
i
2
2
2
2
1
1
1
1





















width
Average height
Composite Trapezoid Rule
• Think of this as the width times the average
height.
September 19, 2022 OSU/CSE 541 30
• The error can be estimated as:
• Where, is the 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.
    f
n
a
b
f
h
a
b
Ea







 2
3
2
12
12
f 

)
( 2
h
O
Error
September 19, 2022 OSU/CSE 541 31
0
5
10
15
20
25
30
35
40
0 0.2 0.4 0.6 0.8 1 1.2
Example
• Integrate:
• from
a=0.2
to
b=0.8
  5
4
3
2
200
810
730
140
20
3
.
0 x
x
x
x
x
x
f 





September 19, 2022 OSU/CSE 541 32
     
 
26
.
11
2
81
.
3
22
.
34
2
.
0
8
.
0
2







b
f
a
f
a
b
I
Example
• A single application of the Trapezoid rule.
• Error:
  3
12
1
a
b
f
Et 



 
September 19, 2022 OSU/CSE 541 33
Example
• We don’t know  so approximate with
average f
  3
2
4000
9720
4380
280 x
x
x
x
f 






  4
3
2
1000
3240
2190
280
20 x
x
x
x
x
f 





 
6
.
131
2
.
0
8
.
0
)
2
.
0
(
)
8
.
0
(
2
.
0
8
.
0
8
.
0
2
.
0














f
f
dx
f
x
f
September 19, 2022 OSU/CSE 541 34
   
  
3
2
2
3
12 12
1
131.6 0.8 0.2 2.37
12
t
b a h b a
E f f
n
 
 
 
    
Example
• The error can thus be estimated as:
September 19, 2022 OSU/CSE 541 35
0
5
10
15
20
25
30
35
40
0 0.2 0.4 0.6 0.8 1 1.2
True value of integral is 12.82. Trapezoid
rule is 11.26 - within approx error - Et is 12%
September 19, 2022 OSU/CSE 541 36
• Use intervals (0.2,0.4),(0.4,0.6),(0.6,0.8):
– (n = 3, h = 0.2)
 
     
       
   
 
57
.
12
6
22
.
34
16
.
30
93
.
13
2
31
.
3
6
.
0
)
3
)(
2
(
8
.
0
6
.
0
4
.
0
2
2
.
0
2
.
0
8
.
0
2
2
1
1


















f
f
f
f
n
b
f
ih
a
f
a
f
a
b
I
n
i
True value of integral is 12.82
Using Three Intervals
September 19, 2022 OSU/CSE 541 37
0
5
10
15
20
25
30
35
40
0 0.2 0.4 0.6 0.8 1 1.2
Et is now 2%
September 19, 2022 OSU/CSE 541 38
             
   
 
76
.
12
12
22
.
34
22
.
35
16
.
30
18
.
22
93
.
13
34
.
7
2
31
.
3
6
.
0
)
6
)(
2
(
8
.
0
7
.
0
6
.
0
5
.
0
4
.
0
3
.
0
2
2
.
0
2
.
0
8
.
0
















f
f
f
f
f
f
f
I
Using Six Intervals
• Use intervals (0.2,0.3),(0.3,0,4), etc.
– (n = 6, h = 0.1)
True value of integral is 12.82
September 19, 2022 OSU/CSE 541 39
0
5
10
15
20
25
30
35
40
0 0.2 0.4 0.6 0.8 1 1.2
Et is now 0.5%
September 19, 2022 OSU/CSE 541 40
Multi-dimensional Integration
• Consider a two-dimensional case.
 
1 1 1
0
0 0 0
1
0 0
0 0
0 0
, ,
,
,
,
n
i
i
n
i
i
n n
i j
i j
n n
i j
i j
i
f x y dxdy A f y dy
n
i
A f y dy
n
i j
A A f
n n
i j
A A f
n n


 
 
 
  
 
 
  
 
 
  
 
 
  
 

 
 
 

September 19, 2022 OSU/CSE 541 41
Multi-dimensional Integration
• For the Trapezoid Rule, this leads to
weights in the following pattern:
1 2 2 2 2 2 1
2 4 4 4 4 4 2
2 4 4 4 4 4 2
2 4 4 4 4 4 2
2 4 4 4 4 4 2
2 4 4 4 4 4 2
1 2 2 2 2 2 1
   
   
 
   
2
1 0, 0,
2 1, , 2 1, 1
1
2 {1, 1} 1, , 1
4
4 1, , 2 1, , 2
ij
i n j n
i n j n
A
i n j n
n
i n j n
 


   

 
   

    

1
2
2
2
2
2
1
1 2 2 2 2 2 1
September 19, 2022 OSU/CSE 541 42
Multi-dimensional Integration
• If we use the weights from the Trapezoid
rule, the error is still O(h2).
• However, there are now n2 function
evaluations.
– Equally-spaced samples on a square region.
September 19, 2022 OSU/CSE 541 43
Multi-dimensional Integration
• In general, given k dimensions, we have
N= nk function evaluations:
• 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.
     
2
2
2 2 k k
k
O h O n O n O N


  
 
    
 
   
September 19, 2022 OSU/CSE 541 44
Reducing the Error
• To improve the estimate of the integral, we
can either:
– Add more intervals
– Use a higher order polynomial
– Use Richardson Extrapolation to examine the
limit as h0.
• Called Romberg Integration
September 19, 2022 OSU/CSE 541 45
Adding More Intervals
• If we have an estimate for one value of h,
do we need to recompute everything for a
value of h/2?
     
1
1
2
2
n
h
i
h
I f a f a ih f b


 
   
 
 

September 19, 2022 OSU/CSE 541 46
Adding More Intervals
• This is called the Recursive Trapezoid
Rule in the book.
• We have n 2n and hh/2.
 
2 1
1
2
1 1
1 0
1
0
( ) 2 ( )
4 2
( ) 2 2 ( )
4 2
2
2 4 2
n
h
i
n n
i i
n
h
i
h h
I f a f a i f b
h h
f a f a ih f a ih f b
I h h
f a ih


 
 


 
 
   
 
 
 
 
 
 
      
 
 
 
 
 
 
   
 
 
 
 

 

September 19, 2022 OSU/CSE 541 47
• Given two numerical estimates obtained
using different h’s, compute higher-order
estimate
• Starting with a step size h1, the exact value is
• Suppose we reduce step size to h2
)
(
)
( 1
1
n
h
O
h
A
A 

)
(
)
( 2
2
n
h
O
h
A
A 

Recall Richardson Extrapolation
September 19, 2022 OSU/CSE 541 48
• Multiplying the 2nd eqn by (h1/h2)n
and
subtracting from the 1st eqn:
• The new error term is generally O(h1
n+1
) or
O(h1
n+2
).
1
)
(
)
(
2
1
1
2
2
1


















 n
n
h
h
h
A
h
A
h
h
A
Richardson Extrapolation
September 19, 2022 OSU/CSE 541 49
• The true integral value can be written
• This is true for any iteration
Richardson Extrapolation
   
h
E
h
I
I 

       
2
2
1
1 h
E
h
I
h
E
h
I
I 



September 19, 2022 OSU/CSE 541 50
Richardson Extrapolation
• For example: Using (n = 2)
• where c is a constant
• Therefore:
 
  2
2
2
1
2
1
h
h
h
E
h
E

f
ch
E 

 2
[error = O(h2)]
order of error in
trapezoidal rule
September 19, 2022 OSU/CSE 541 51
• This leads to:
• For integration, we have:
    2
2
2
1
2
1
h
h
h
E
h
E 
       
2
2
2
2
2
1
2
1 h
E
h
I
h
h
h
E
h
I 


Richardson Extrapolation
September 19, 2022 OSU/CSE 541 52
Richardson Extrapolation
• Solving for E(h2):
• And plugging back into the estimated
integral.
     
2
2
2
1
2
1
2
1
h
h
h
I
h
I
h
E



   
2
2 h
E
h
I
I 

September 19, 2022 OSU/CSE 541 53
• Leads to:
• Letting h2 = h1 /2
Richardson Extrapolation
 
 
   
 
1
2
2
2
1
2
1
/
1
h
I
h
I
h
h
h
I
I 



     
 
   
1
2
1
2
2
2
3
1
3
4
1
2
1
h
I
h
I
I
h
I
h
I
h
I
I






September 19, 2022 OSU/CSE 541 54
• We combined two O(h2) estimates to get an
O(h4) estimate.
• Can also combine two O(h4) estimates to
get an O(h6) estimate.
   
l
m h
I
h
I
I
15
1
15
16


Romberg Integration
September 19, 2022 OSU/CSE 541 55
Romberg Integration
• Greater weight is placed on the more
accurate estimate.
• Weighting coefficients sum to unity
– i.e, (16-1)/15=1
• Can continue, by combining two O(h6)
estimates to get an O(h8) estimate.
   
l
m h
I
h
I
I
63
1
63
64


September 19, 2022 OSU/CSE 541 56
• General pattern is called Romberg Integration
– j : level of subdivision, j+1 has more intervals.
– k : level of integration, k = 1 is original trapezoid
estimate [O(h2)], k = 2 is improved [O(h4)], etc.
 
, 1,
, 1 , , 1,
4 1
4 1 4 1
k
j k j k
j k j k j k j k
k k
I I
I I I I

 

   
 
Romberg Integration
September 19, 2022 OSU/CSE 541 57
Romberg Integration
• For example, j = 1, k = 1 leads to
 
1,0 0,0 1
1,1 1
4 4 1
3 3 2 3
I I h
I I I h
  
  
 
 
September 19, 2022 OSU/CSE 541 58
• Consider the function:
• Integrate from a = 0 to b = 0.8
• Using the trapezoidal rule yields the
following results:
5
4
3
2
400
900
675
200
25
2
.
0
)
( x
x
x
x
x
x
f 





Example
September 19, 2022 OSU/CSE 541 59
intervals Integral
1 0.8 0.1728
2 0.4 1.0688
4 0.2 1.4848
h
Example
• Trapezoid Rules:
Exact integral is 1.64053334
3674667
.
1
)
1728
.
0
(
3
1
)
0688
.
1
(
3
4



I
(j=1, k=1)
k
j
k = 0 k = 1
j = 0
j = 1
j = 2
September 19, 2022 OSU/CSE 541 60
2 4
segments ( ) ( )
1 0.8 0.1728
2 0.4 1.0688 1.3674667
4 0.2 1.4848
h O h O h
k
j
62346667
.
1
)
0688
.
1
(
3
1
)
4848
.
1
(
3
4



I (j=2, k=1)
k = 1
k = 0
Exact integral is 1.64053334
Example
September 19, 2022 OSU/CSE 541 61
Example
2 4
segments ( ) ( )
1 0.8 0.1728
2 0.4 1.0688 1.3674667
4 0.2 1.4848 1.62346667
h O h O h
k
j
(j=2, k=2)
k = 2
k = 1
Exact integral is 1.64053334
64053334
.
1
)
3674667
.
1
(
15
1
)
62346667
.
1
(
15
16



I
September 19, 2022 OSU/CSE 541 62
2 4 6
segments ( ) ( ) ( )
1 0.8 0.1728
2 0.4 1.0688 1.3674667
4 0.2 1.
h O h O h O h
4848 1.62346667 1.64053334
k
j
k = 3
k = 2
k = 1
Example
September 19, 2022 OSU/CSE 541 63
2 4 6 8
segments ( ) ( ) ( ) ( )
1 0.8 0.1728
2 0.4 1.0688 1.3674667
4
h O h O h O h O h
0.2 1.4848 1.62346667 1.64053334
8 0.1 ?? ?? ?? ??
??
)
64053334
.
1
(
63
1
(??)
63
64



I (j=3, k=3)
k
j
k = 3
Example
• Better and better results can be obtained by
continuing this
September 19, 2022 OSU/CSE 541 64
Romberg Integration
• Is this that significant?
• Consider the cost of computing the
Trapezoid Rule for 1000 data points.
– Refinement would lead to 2000 data points.
• Implies an additional 1003 operations using the
Recursive Trapezoid Rule.
• Not to mention the 1000 (expensive) function evals.
– Romberg Integration cost:
• Three additional operations – no function evals!!!
September 19, 2022 OSU/CSE 541 65
Higher-Order Polynomials
• Recall:
)
(
3/8
s
Simpson'
cubic
3
)
(
1/3
s
Simpson'
quadratic
2
)
(
Trapezoid
linear
1
Error
Formula
Polynomial
4
4
2
h
O
h
O
h
O
m
1 1 2
1
0 0
1 2
0 0 0
( ) ( ) ( ) ... ( )
n m m m n
n
m n mn
x x x x
m m m
x x x x
f x dx p x dx p x dx p x dx
  
 
   
   
September 19, 2022 OSU/CSE 541 66
• If we use a 2nd order polynomial (need 3
points or 2 intervals):
– Lagrange form.
  
  
    
  
 
  
  
  dx
x
f
x
x
x
x
x
x
x
x
x
f
x
x
x
x
x
x
x
x
x
f
x
x
x
x
x
x
x
x
I
x
x




















 
2
1
2
0
2
1
0
1
2
1
0
1
2
0
0
2
0
1
0
2
1
2
0





 

2
2
0
1
x
x
x
Simpson’s 1/3 Rule
September 19, 2022 OSU/CSE 541 67
Simpson’s 1/3 Rule
• Requiring equally-spaced intervals:
  
 
 
  
  
 
  
  
 
2
0
0 0 0 0
0 1
0 0
2
2 2
2
2
x
x
x x h x x h x x x x h
I f x f x
h h h h
x x x x h
f x dx
h h
       
 

  


  
 


September 19, 2022 OSU/CSE 541 68
• Integrate and simplify:
0
2
4
6
8
10
12
3 5 7 9 11 13 15
     
 
2
1
0 4
3
x
f
x
f
x
f
h
I 


2
a
b
h


Simpson’s 1/3 Rule
Quadratic
Polynomial
September 19, 2022 OSU/CSE 541 69
• If we use a = x0 and b = x2, and
x1 = (b+a)/2
Simpson’s 1/3 Rule
       
6
4 2
1
0 x
f
x
f
x
f
a
b
I




width
average height
September 19, 2022 OSU/CSE 541 70
• Error for Simpson’s 1/3 rule
Integrates a cubic exactly:
 
  0
4


f
 
     
 

 4
5
4
5
2880
90
f
a
b
f
h
Et





2
a
b
h


)
( 4
h
O
Simpson’s 1/3 Rule
September 19, 2022 OSU/CSE 541 71
Composite Simpson’s 1/3 Rule
• As with Trapezoidal rule, can use multiple
applications of Simpson’s 1/3 rule.
• Need even number of intervals
– An odd number of points are required.
September 19, 2022 OSU/CSE 541 72
Composite Simpson’s 1/3 Rule
• Example: 9 points, 4 intervals
-5
0
5
10
15
20
25
0 0.5 1 1.5 2 2.5 3 3.5 4 4.5
September 19, 2022 OSU/CSE 541 73
• As in composite trapezoid, break integral up
into n/2 sub-integrals:
• Substitute Simpson’s 1/3 rule for each
integral and collect terms.
Composite Simpson’s 1/3 Rule
     


 




n
n
x
x
x
x
x
x
dx
x
f
dx
x
f
dx
x
f
I
2
4
2
2
0
...
 
       
1 2
0
1,3,5 2,4,6
4 2
3
n n
i j n
i j
f x f x f x f x
I b a
n
 
 
  
 
 
n+1 data points, an odd number
September 19, 2022 OSU/CSE 541 74
• Odd coefficients receive a weight of 4, even
receive a weight of 2.
• Doesn’t seem very fair, does it?
coefficients on
numerator
1
4 1
1 4
1
1
4 1
i = 0
i = n
Composite Simpson’s 1/3 Rule
September 19, 2022 OSU/CSE 541 75
• The error can be estimated by:
• If n is doubled, h  h/2 and Ea  Ea/16
  )
4
(
4
)
4
(
5
180
180
f
h
a
b
f
nh
Ea



)
4
(
f is the average 4th derivative
)
( 4
h
O
Error Estimate
September 19, 2022 OSU/CSE 541 76
• Integrate from a = 0 to b = 2.
• Use Simpson’s 1/3 rule:
   
   
2
2
0 1 2
0
0 1 4
1
4 ( )
3
1
0 4 (1) 2
3
1
( 4 ) 0.82994
3
x
I e dx h f x f x f x
f f f
e e e

 
   
 
 
  
 
 
   

2
)
( x
e
x
f 

2
1
2
0
1
2
2
1
0 








 b
x
b
a
x
a
x
a
b
h
Example
September 19, 2022 OSU/CSE 541 77
• Error estimate:
• Where h = b - a and a <  < b
• Don’t know 
– use average value
 
 

4
5
90
f
h
Et 

 
 
   
   
 
4 4 4
5 5
0 1 2
4
1 1
90 90 3
t a
f x f x f x
E E f
 
 
 
    
Example
September 19, 2022 OSU/CSE 541 78
• Let’s look at the polynomial again:
– From a = 0 to b = 0.8
5
4
3
2
400
900
675
200
25
2
.
0
)
( x
x
x
x
x
x
f 





8
.
0
4
.
0
2
0
4
.
0
2
2
1
0 








 b
x
b
a
x
a
x
a
b
h
   
 
   
 
36746667
.
1
8
.
0
)
4
.
0
(
4
0
3
)
4
.
0
(
)
(
4
3
1
)
( 2
1
0
2
0







 
f
f
f
x
f
x
f
x
f
h
dx
x
f
I
Exact integral is 1.64053334
Another Example
September 19, 2022 OSU/CSE 541 79
• Actual Error: (using the known exact value)
• Estimate error: (if the exact value is not
available)
• Where a <  < b.
 
 

4
5
90
f
h
Et 

27306666
.
0
36746667
.
1
-
1.64053334 

E 16%
Error
September 19, 2022 OSU/CSE 541 80
• Compute the fourth-derivative
• Matches actual error pretty well.
Error
x
x
f 48000
21600
)
(
)
4
(



 
   
  27306667
.
0
4
.
0
90
4
.
0
90
4
.
0 4
5
1
4
5





 f
x
f
E
E a
t
middle point
September 19, 2022 OSU/CSE 541 81
• If we use 4 segments instead of 1:
– x = [0.0 0.2 0.4 0.6 0.8]
0.2
b a
h
n

 
232
.
0
)
8
.
0
(
464
.
3
)
6
.
0
(
456
.
2
)
4
.
0
(
288
.
1
)
2
.
0
(
2
.
0
)
0
(





f
f
f
f
f
 
       
 
6234667
.
1
12
232
.
0
)
456
.
2
(
2
)
464
.
3
288
.
1
(
4
2
.
0
8
.
0
)
4
)(
3
(
)
8
.
0
(
)
6
.
0
(
4
)
4
.
0
(
2
)
2
.
0
(
4
)
0
(
0
8
.
0
3
2
4
1
5
,
3
,
1
2
6
,
4
,
2
0

















 




f
f
f
f
f
n
x
f
x
f
x
f
x
f
a
b
I
n
n
i
n
j
j
i
Exact integral is 1.64053334
Example Continued
September 19, 2022 OSU/CSE 541 82
• Actual Error: (using the known exact value)
• Estimate error: (if the exact value is not
available)
01706667
.
0
6234667
.
1
-
1.64053334 

E 1%
 
   
  0085
.
0
4
.
0
90
2
.
0
90
2
.
0 4
5
2
4
5






 f
x
f
E
E a
t
middle point
Error
September 19, 2022 OSU/CSE 541 83
Error
• Actual is twice the estimated, why?
• Recall:
x
x
f 48000
21600
)
(
)
4
(



 
 
(4) (4)
0,0.8
(4)
max ( ) (0) 21600
(0.4) 2400
x
f x f
f

  

September 19, 2022 OSU/CSE 541 84
Error
• Rather than estimate, we can bound the
absolute value of the error:
• Five times the actual, but provides a safer
error metric.
 
   
 
5 5
4 4
0.2 0.2
0 0.0768
90 90
a
E f f

   
September 19, 2022 OSU/CSE 541 85
Simpon’s 1/3 Rule
• Simpson’s 1/3 rule uses a 2nd order polynomial
– need 3 points or 2 intervals
– This implies we need an even number of intervals.
• What if you don’t have an even number of
intervals? Two choices:
1. Use Simpson’s 1/3 on all the segments except the last
(or first) one, and use trapezoidal rule on the one left.
– Pitfall - larger error on the segment using trapezoid
2. Use Simpson’s 3/8 rule.
September 19, 2022 OSU/CSE 541 86
• Simpson’s 3/8 rule uses a third order polynomial
– need 3 intervals (4 data points)
3
3
2
2
1
0
3 )
(
)
( x
a
x
a
x
a
a
x
p
x
f 




   

 

3
0
3
0
3
x
x
x
x
dx
x
p
dx
x
f
I
Simpson’s 3/8 Rule
September 19, 2022 OSU/CSE 541 87
Simpson’s 3/8 Rule
• Determine a’s with Lagrange polynomial
• For evenly spaced points
3
a
b
h


       
 
3
2
1
0 3
3
8
3
x
f
x
f
x
x
f
h
I 



September 19, 2022 OSU/CSE 541 88
• Same order as 1/3 Rule.
– More function evaluations.
– Interval width, h, is smaller.
• Integrates a cubic exactly:

 
  0
4


f
 
 
4
5
3
80
t
E h f 
  )
( 4
h
O
Error
September 19, 2022 OSU/CSE 541 89
Comparison
• Simpson’s 1/3 rule and Simpson’s 3/8 rule have
the same order of error
– O(h4)
– trapezoidal rule has an error of O(h2)
• Simpson’s 1/3 rule requires even number of
segments.
• Simpson’s 3/8 rule requires multiples of three
segments.
• Both Simpson’s methods require evenly spaced
data points
September 19, 2022 OSU/CSE 541 90
• n = 10 points  9 intervals
– First 6 intervals - Simpson’s 1/3
– Last 3 intervals - Simpson’s 3/8
Simpson’s 1/3
Simpson’s 3/8
Mixing Techniques
September 19, 2022 OSU/CSE 541 91
• We can examine even higher-order
polynomials.
– Simpson’s 1/3 - 2nd order Lagrange (3 pts)
– Simpson’s 3/8 - 3rd order Lagrange (4 pts)
• Usually do not go higher.
• Use multiple segments.
– But only where needed.
Newton-Cotes Formulas
September 19, 2022 OSU/CSE 541 92
Adaptive Simpson’s Scheme
• Recall Simpson’s 1/3 Rule:
• Where initially, we have a=x0 and b=x2.
• Subdividing the integral into two:
     
 
2
1
0 4
3
x
f
x
f
x
f
h
I 


         
1 2 3
4 2 4
6
h
I f a f x f x f x f b
    
 
 
September 19, 2022 OSU/CSE 541 93
Adaptive Simpson’s Scheme
• We want to keep subdividing, until we
reach a desired error tolerance, .
• Mathematically:
       
           
1
1 2 3
4
3
4 2 4
6
b
a
b
a
h
f x dx f a f x f b
h
f x dx f a f x f x f x f b


 
   
 
 
 
 
 
     
 
 
 
 


September 19, 2022 OSU/CSE 541 94
Adaptive Simpson’s Scheme
• This will be satisfied if:
• The left and the right are within one-half of the
error.
       
       
1 2
2 3
2
4 ,
6 2
4 ,
6 2
2
c
a
b
c
h
f x dx f a f x f x and
h
f x dx f x f x f b where
a b
c x


 
   
 
 
 
 
 
   
 
 
 
 

 


September 19, 2022 OSU/CSE 541 95
Adaptive Simpson’s Scheme
• Okay, now we have two separate intervals
to integrate.
• What if one can be solved accurately with
an h=10-3, but the other requires many,
many more intervals, h=10-6?
September 19, 2022 OSU/CSE 541 96
Adaptive Simpson’s Scheme
• Adaptive Simpson’s method provides a
divide and conquer scheme until the
appropriate error is satisfied everywhere.
• Very popular method in practice.
• Problem:
– We do not know the exact value, and hence do
not know the error.
September 19, 2022 OSU/CSE 541 97
Adaptive Simpson’s Scheme
• How do we know whether to continue to
subdivide or terminate?
     
     
   
5
4
, , ,
, 4 ,
6 2
1
,
90 2
b
a
I f x dx S a b E a b where
b a a b
S a b f a f f b and
b a
E a b f
  
   
 
  
 
 
 
 

 
   
 

September 19, 2022 OSU/CSE 541 98
Adaptive Simpson’s Scheme
• The first iteration can then be defined as:
• Subsequent subdivision can be defined as:
 
   
2
, ,
S S a c S c b
 
   
 
   
 
1 1
1 1
,
, , ,
I S E where
S S a b E E a b
 
 
September 19, 2022 OSU/CSE 541 99
Adaptive Simpson’s Scheme
• Now, since
• We can solve for E(2) in terms of E(1).
     
   
5 5
2 4 4
5
4 1
4
1 / 2 1 / 2
90 2 90 2
1 1 1
2 90 2 16
h h
E f f
h
f E
   
  
   
   
   
  
   
   
 
   
2
, ,
E E a c E c b
 
September 19, 2022 OSU/CSE 541 100
Adaptive Simpson’s Scheme
• Finally, using the identity:
• We have:
• Plugging into our definition:
       
1 1 2 2
I S E S E
   
         
2 1 1 2 2
15
S S E E E
   
         
 
2 2 2 2 1
1
15
I S E S S S
    
September 19, 2022 OSU/CSE 541 101
Adaptive Simpson’s Scheme
• Our error criteria is thus:
• Simplifying leads to the termination
formula:
     
 
2 2 1
1
15
I S S S 
   
   
 
2 1
15
S S 
 
September 19, 2022 OSU/CSE 541 102
Adaptive Simpson’s Scheme
• What happens graphically:
September 19, 2022 OSU/CSE 541 103
1
2 15
S subd v
S i ide

  
September 19, 2022 OSU/CSE 541 104
September 19, 2022 OSU/CSE 541 105
1
2 15
2
subdivide
S
S

  
September 19, 2022 OSU/CSE 541 106
 
2 1
2
1
15
S
I S S
  
1
2 15
4
S o
S d ne

  
September 19, 2022 OSU/CSE 541 107
1
2 15
4
subdivide
S
S

  
September 19, 2022 OSU/CSE 541 108
 
2 1
2
1
15
S
I S S
  
September 19, 2022 OSU/CSE 541 109
 
2 1
2
1
15
S
I S S
  
September 19, 2022 OSU/CSE 541 110
I=Ileft + Iright
Iright =Ileft + Iright
September 19, 2022 OSU/CSE 541 111
September 19, 2022 OSU/CSE 541 112
September 19, 2022 OSU/CSE 541 113
Adaptive Simpson’s Scheme
• We gradually capture the difficult spots.
September 19, 2022 OSU/CSE 541 114
Adaptive Simpson’s Code
• Simple Recursive Program
static const int m_nMaximum_Divisions = 1000;
Real IntegrationSimpson( const Real (*f) (Real x), const Real start, const Real end, const Real
error_tolerance, int &level )
{
level += 1;
Real h = (end – start);
Real midpoint = (start + end) / 2.0;
Real f_start = f(start);
Real f_end = f(end);
Real f_mid = f(midpoint );
oneLevel = h*( f_start + 4.0*f_mid + f_end) / 6.0;
Real leftMidpoint = (start+ midpoint ) / 2.0;
Real rightMidpoint = (end+ midpoint ) / 2.0
Real f_midLeft = f(leftMidpoint );
Real f_midRight = f(rightMidpoint );
twoLevel = h*(f_start + 4.0* f_midLeft + 2.0* f_mid + 4.0* f_midRight + f_end) / 12.0;
if( level >= m_nMax_Divisions ) // Terminate the process, converging too slow
return twoLevel;
September 19, 2022 OSU/CSE 541 115
Adaptive Simpson’s Code
if( absf( twoLevel – oneLevel) < 15.0*error_tolerance) // Desired solution reached
return twoLevel + (twoLevel-oneLevel) / 15.0;
//
// Otherwise, split the interval in two and recursively evaluate each half.
//
leftIntegral = IntegrationSimpson( f, start, midpoint , error_tolerance/2.0, level );
rightIntegral = IntegrationSimpson( f, midpoint , end, error_tolerance/2.0, level );
return leftIntegral + rightIntegral;
}
September 19, 2022 OSU/CSE 541 116
• Idea is that if we evaluate the function at certain
points, and sum with certain weights, we will get a
more accurate integral
• Evaluation points and weights are pre-computed
and tabulated
• Basic form:
 




1
1
1
)
(
)
(
n
i
i
i x
f
c
dx
x
f
I
ci : weighting factors
xi : sampling points selected optimally
Guassian Quadrature
New!!
September 19, 2022 OSU/CSE 541 117
• Note that the interval is between –1 and 1
• For other intervals, a change of variables is used to
transfer the problem so that it utilizes the interval
[-1, 1]
• This is a linear transform, such that for t[a,b]:
• We have for x[-1,1]:

b
a
dt
t
f )
(
2
)
( a
b
x
a
b
t




a
b
a
b
t
x




2
Guassian Quadrature
September 19, 2022 OSU/CSE 541 118
• As t = a  x = -1
• As t = b  x = 1
dx
a
b
dt
2
)
( 






 



2
)
(
)
(
a
b
x
a
b
f
t
f
dx
a
b
x
a
b
f
a
b
dt
t
f
b
a 
 





 




1
1 2
)
(
2
)
(
)
(
Guassian Quadrature
September 19, 2022 OSU/CSE 541 119
• Basic form of Gaussian quadrature:
• For n=2, we have:
• This leads to 4 unknowns: c1, c2, x1, and x2
– two unknown weights (c1, c2)
– two unknown sampling points (x1, x2)
 




1
1
1
)
(
)
(
n
i
i
i x
f
c
dx
x
f
I
   
2
2
1
1 x
f
c
x
f
c
I 

Guassian Quadrature
September 19, 2022 OSU/CSE 541 120
Guassian Quadrature
• 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!!!
September 19, 2022 OSU/CSE 541 121
• In particular, let’s look at these simple
polynomials:
– Constant
• f(x)=1
– Linear
• f(x)=x
– Quadratic
• f(x)=x2
– Cubic
• f(x)=x3
Guassian Quadrature
September 19, 2022 OSU/CSE 541 122
Guassian Quadrature
• Recalling the formula:
– Constant
• f(x)=1
– Linear
• f(x)=x
– Quadratic
• f(x)=x2
– Cubic
• f(x)=x3
   
   
   
    3
2
2
3
1
1
2
2
1
1
1
1
3
2
2
2
2
1
1
2
2
1
1
1
1
2
2
2
1
1
2
2
1
1
1
1
2
1
2
2
1
1
1
1
0
3
2
0
2
1
x
c
x
c
x
f
c
x
f
c
dx
x
x
c
x
c
x
f
c
x
f
c
dx
x
x
c
x
c
x
f
c
x
f
c
xdx
c
c
x
f
c
x
f
c
dx




























   
2
2
1
1 x
f
c
x
f
c
I 

September 19, 2022 OSU/CSE 541 123
• We can now solve for our unknowns:
– Note, this is not an easy problem and will not be
covered in this class.
577
.
0
3
1
577
.
0
3
1
1
2
1
2
1








x
x
c
c
Guassian Quadrature
September 19, 2022 OSU/CSE 541 124
Guassian Quadrature
• This yields the two point Gauss-Legendre
formula















3
1
3
1
f
f
I
September 19, 2022 OSU/CSE 541 125
Guassian Quadrature
• This is exact for all polynomials up to and
including degree 3 (cubics).
 
 
 
1 1 1 1 1
3 2 3 2
1 1 1 1 1
3 3 2 2
1
3
1
3
1 1 1 1 1 1
1 1
3 3 3 3 3 3
3 2
ax bx cx d dx a x dx b x dx c xdx d dx
a b c d
ax bx cx d
    

      
     
  
           
       
     
           
   
           
 
   

    
  
September 19, 2022 OSU/CSE 541 126
x
f(x)
-1 1
f(0.577)
f(-0.577)
-0.577 0.577




1
1
)
577
.
0
(
)
577
.
0
(
)
( f
f
dx
x
f
Guassian Quadrature
September 19, 2022 OSU/CSE 541 127
Example
5
4
3
2
400
900
675
200
25
2
.
0
)
( x
x
x
x
x
x
f 





dx
a
b
x
a
b
f
a
b
dt
t
f
b
a 
 





 




1
1 2
)
(
2
)
(
)
(
 dt
t
f
dt
t
f
dx
x
f
I












 





1
1
1
1
8
.
0
0
4
.
0
4
.
0
4
.
0
2
0
8
.
0
)
0
8
.
0
(
2
)
0
8
.
0
(
)
(
• Integrate f(x) from a = 0 to b = 0.8
• Transform from [0, 0.8] to [-1, 1]
September 19, 2022 OSU/CSE 541 128
• Solving
• And substituting for the 2-point formula:
Exact integral is 1.64053334
 
dt
t
t
t
t
t
dt
t
f
I























1
1 5
4
3
2
1
1
)
4
.
0
4
.
0
(
400
)
4
.
0
4
.
0
(
900
)
4
.
0
4
.
0
(
675
)
4
.
0
4
.
0
(
200
)
4
.
0
4
.
0
(
25
2
.
0
4
.
0
4
.
0
4
.
0
4
.
0
3
/
1


t
82257778
.
1
30583723
.
1
51674055
.
0 


I
 dt
t
f
I 

1
1
4
.
0
Example
September 19, 2022 OSU/CSE 541 129
• Recall the basic form:
• Let’s look at n=3.
• We now have 6 unknowns: c1, c2, c3,x1, x2, and x3
– three unknown weights (c1, c2 , c3)
– three unknown sampling points (x1, x2 , x3)
 




1
1
1
)
(
)
(
n
i
i
i x
f
c
dx
x
f
I
     
3
3
2
2
1
1 x
f
c
x
f
c
x
f
c
I 


Higher-order Gaussian Quadrature
September 19, 2022 OSU/CSE 541 130
Use 6 equations - constant, linear, quadratic, cubic,
4th order and 5th order to find those unknowns
     
     
     
     
     
      5
3
3
5
2
2
5
1
1
3
3
2
2
1
1
1
1
5
4
3
3
4
2
2
4
1
1
3
3
2
2
1
1
1
1
4
3
3
3
3
2
2
3
1
1
3
3
2
2
1
1
1
1
3
2
3
3
2
2
2
2
1
1
3
3
2
2
1
1
1
1
2
3
3
2
2
1
1
3
3
2
2
1
1
1
1
3
2
1
3
3
2
2
1
1
1
1
0
5
2
0
3
2
0
2
1
x
c
x
c
x
c
x
f
c
x
f
c
x
f
c
dx
x
x
c
x
c
x
c
x
f
c
x
f
c
x
f
c
dx
x
x
c
x
c
x
c
x
f
c
x
f
c
x
f
c
dx
x
x
c
x
c
x
c
x
f
c
x
f
c
x
f
c
dx
x
x
c
x
c
x
c
x
f
c
x
f
c
x
f
c
xdx
c
c
c
x
f
c
x
f
c
x
f
c
dx






















































September 19, 2022 OSU/CSE 541 131
• Can solve these equations (or have some one
smarter than us, like Guass solve them).
• Produces the three point Gauss-Legendre formula
– Exact for polynomials up to and including degree 5
(because using 5th degree polynomial)
77459669
.
0
5
/
3
0
77459669
.
0
5
/
3
9
/
5
9
/
8
9
/
5
2
2
1
3
2
1










x
x
x
c
c
c
     
3
3
2
2
1
1 x
f
c
x
f
c
x
f
c
I 


Higher-order Gaussian Quadrature
September 19, 2022 OSU/CSE 541 132





1
1
)
775
.
0
(
9
5
)
0
.
0
(
9
8
)
775
.
0
(
9
5
)
( f
f
f
dx
x
f
-1 1
x
f(0.775)
f(-0.775)
-0.775 0.775
f(0)
Higher-order Gaussian Quadrature
September 19, 2022 OSU/CSE 541 133
5
4
3
2
400
900
675
200
25
2
.
0
)
( x
x
x
x
x
x
f 





Integrate from a = 0 to b = 0.8
Transform from [0, 0.8] to [-1, 1]
 
dt
t
t
t
t
t
dx
x
f
I





















1
1 5
4
3
2
8
.
0
0
)
4
.
0
4
.
0
(
400
)
4
.
0
4
.
0
(
900
)
4
.
0
4
.
0
(
675
)
4
.
0
4
.
0
(
200
)
4
.
0
4
.
0
(
25
2
.
0
Example
replace -0.4 with +0.4
September 19, 2022 OSU/CSE 541 134
  



















5
3
9
5
0
9
8
5
3
9
5
f
f
f
I
64053334
.
1
485987599
.
0
873244444
.
0
281301290
.
0




I
Substitute into the transform equation and get
Exact integral is 1.64053334
Example
• Using the 3-point Gauss-Legendre formula:
September 19, 2022 OSU/CSE 541 135
Can develop higher order Gauss-Legendre forms
using
     
n
n x
f
c
x
f
c
x
f
c
I 


 ...
2
2
1
1
Values for c’s and x’s are tabulated
Use the same transformation to map interval onto
[-1, 1]
Gaussian Quadrature
September 19, 2022 OSU/CSE 541 136
1713245
.
0
3607616
.
0
4679139
.
0
4679139
.
0
3607616
.
0
1713245
.
0
50
0.23692688
05
0.47862867
89
0.56888888
05
0.47862867
50
0.23692688
51
0.34785484
49
0.65214515
49
0.65214515
51
0.34785484
56
0.55555555
89
0.88888888
56
0.55555555
0
.
1
0
.
1
932469514
0
661209386
0
238619186
0
238619186
0
661209386
0
932469514
0
9061798459
0
5384693101
0
0000000000
0
5384693101
0
9061798459
0
8611363116
0
3399810436
0
3399810436
0
8611363116
0
7745966692
0
0000000000
0
7745966692
0
5773502692
0
5773502692
0
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.









2 3 4 5 6
n
ci
xi
     
n
n x
f
c
x
f
c
x
f
c
dx
x
f
I 



 
...
)
( 2
2
1
1
1
1
September 19, 2022 OSU/CSE 541 137
Gaussian Quadrature
• Requires function evaluations at non-
uniformly spaced points within the
integration interval
– not appropriate for cases where the function is
unknown
– not suited for dealing with tabulated data that
appear in many engineering problems
• If the function is known, its efficiency can
be a decided advantage
September 19, 2022 OSU/CSE 541 138
Gaussian Quadrature
• Problems:
– If we add more data points, like doubling the
number of sample points.

CIS541_07_Integration.ppt

  • 1.
  • 2.
    September 19, 2022OSU/CSE 541 2 Quadrature • We talk in terms of Quadrature Rules – 1. The process of making something square. 2. Mathematics The process of constructing a square equal in area to a given surface. 3. Astronomy A configuration in which the position of one celestial body is 90° from another celestial body, as measured from a third. – The American Heritage® Dictionary: Fourth Edition. 2000
  • 3.
    September 19, 2022OSU/CSE 541 3 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
  • 4.
    September 19, 2022OSU/CSE 541 4 What does an integral represent? Basic definition of an integral: Motivation   b a dx x f area ) (    d c b a dxdy x f volume ) (        n k k b a n x x f dx x f 1 ) ( lim ) ( sum of height  width f(x) x n a b x    where
  • 5.
    September 19, 2022OSU/CSE 541 5 • Evaluate the integral, without doing the calculation analytically. • Necessary when either: – Integrand is too complicated to integrate analytically – Integrand is not precisely defined by an equation, i.e., we are given a set of data (xi, yi), i = 1, 2, 3, …,n Motivation   b a dx x f I ) ( dx e x x x 5 . 0 2 0 5 . 0 1 ) 1 cos( 2    
  • 6.
    September 19, 2022OSU/CSE 541 6 • Integration is a summing process. Thus virtually all numerical approximations can be represented by 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. t b a i n i i E x f w dx x f I       ) ( ) ( 1 Reimann Integral Theorem
  • 7.
    September 19, 2022OSU/CSE 541 7 • The most common numerical integration formula is based on equally spaced data points. • Divide [x0 , xn] into n intervals (n1)  n x x dx x f 0 ) ( Partitioning the Integral 1 2 0 0 1 1 ( ) ( ) ( ) ( ) n n n x x x x x x x x f x dx f x f x f x         
  • 8.
    September 19, 2022OSU/CSE 541 8 Upper Sums • Assume that f(x)>0 everywhere. • If within each interval, we could determine the maximum value of the function, then we have: • where   0 1 1 0 ( ) n x n i i i i x f x M x x          1 sup ( ): i i i M f x x x x    Supremum - least upper bound
  • 9.
    September 19, 2022OSU/CSE 541 9 Upper Sums • Graphically: x0 x1 x2 x4 x3
  • 10.
    September 19, 2022OSU/CSE 541 10 Lower Sums • Likewise, still assuming that f(x)>0 everywhere. • If within each interval, we could determine the minimum value of the function, then we have: • where   0 1 1 0 ( ) n x n i i i i x f x m x x          1 inf ( ): i i i m f x x x x    Infimum - greatest lower bound
  • 11.
    September 19, 2022OSU/CSE 541 11 Lower Sums • Graphically x0 x1 x2 x4 x3
  • 12.
    September 19, 2022OSU/CSE 541 12 Finer Partitions • We now have a bound on the integral of the function for some partition (x0,..,xn): • 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.     0 1 1 1 1 0 0 ( ) n x n n i i i i i i i i x m x x f x M x x             
  • 13.
    September 19, 2022OSU/CSE 541 13 Bounding the Integral • Graphically x0 x1 x2 x4 x3
  • 14.
    September 19, 2022OSU/CSE 541 14 Bounding the Integral • Halving each interval (much like Lab1): x0 x3 x5 x9 x7
  • 15.
    September 19, 2022OSU/CSE 541 15 Bounding the Integral • One more time: x0 x5 x7 x11 x9
  • 16.
    September 19, 2022OSU/CSE 541 16 Monotonic Functions • Note that if a function is monotonically increasing (or decreasing), then the lower sum corresponds to the left partition values, and the upper sum corresponds to the right partition values. x0 x3 x5 x9 x7
  • 17.
    September 19, 2022OSU/CSE 541 17 Lab1 and Integration • Thinking back to lab1, what were the limits or the integration? • Is the sin function monotonic on this interval? • Should the Reiman sum be an upper or lower sum?
  • 18.
    September 19, 2022OSU/CSE 541 18 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. m m m x a x a x a a x p      ... ) ( 2 2 1 0
  • 19.
    September 19, 2022OSU/CSE 541 19 • The m’s (order of the polynomials) may be the same or different. • Different choices for m’s lead to different formulas: 1 1 2 1 0 0 1 2 0 0 0 ( ) ( ) ( ) ... ( ) n m m m n n m n mn x x x x m m m x x x x f x dx p x dx p x dx p x dx              ) ( 3/8 s Simpson' cubic 3 ) ( 1/3 s Simpson' quadratic 2 ) ( Trapezoid linear 1 Error Formula Polynomial 4 4 2 h O h O h O m Newton-Cotes Formulas
  • 20.
    September 19, 2022OSU/CSE 541 20 • Simplest way to approximate the area under a curve – using first order polynomial (a straight line) • Using Newton’s form of the interpolating polynomial: • Now, solve for the integral:         b a b a dx x p dx x f I 1          a x a b a f b f a f x p      1 Trapezoid Rule
  • 21.
    September 19, 2022OSU/CSE 541 21 Trapezoid Rule                    b a dx a x a b a f b f a f I         b f a f a b I    2 a b f(a) f(b) height average width  I Trapezoid Rule
  • 22.
    September 19, 2022OSU/CSE 541 22 Trapezoid Rule • Improvement? x0 x1 x2 x4 x3
  • 23.
    September 19, 2022OSU/CSE 541 23 • The integration error is: • Where h = b - a and  is an unknown point where a <  < b (intermediate value theorem) • You get exact integration if the function, f, is linear (f = 0)     2 3 12 ) ( 12 1 h f a b h f Et            ) ( 2 h O Trapezoid Rule Error
  • 24.
    September 19, 2022OSU/CSE 541 24 2 ) ( x e x f   Integrate from a = 0 to b = 2           0183 . 1 ) ( 1 ) 0 ( ) 2 ( 2 ) 0 2 ( 2 0 4 2 0 2               e e f f b f a f a b dx e I x Use trapezoidal rule: Example
  • 25.
    September 19, 2022OSU/CSE 541 25 Estimate error:   3 12 1 h f Et      Where h = b - a and a <  < b Don’t know  - use average value 2 ) 4 2 ( ) ( 2 x e x x f       2564 . 0 ) 2 ( 2 ) 0 (        f f       58 . 0 2 2 0 12 23          f f E E a t 2 0 2    h Example
  • 26.
    September 19, 2022OSU/CSE 541 26 More intervals, better result [error  O(h2)] 0 1 2 3 4 5 6 7 3 5 7 9 11 13 15 n = 2 0 1 2 3 4 5 6 7 3 5 7 9 11 13 15 n = 3 0 1 2 3 4 5 6 7 3 5 7 9 11 13 15 n = 4 0 1 2 3 4 5 6 7 3 5 7 9 11 13 15 n = 8
  • 27.
    September 19, 2022OSU/CSE 541 27 • If we do multiple intervals, we can avoid duplicate function evaluations and operations: • Use n+1 equally spaced points. • Each interval has: • Break up the limits of integration and expand. n a b h                    b h b h a h a h a a dx x f dx x f dx x f I ... 2 Composite Trapezoid Rule
  • 28.
    September 19, 2022OSU/CSE 541 28 • Substituting the trapezoid rule for each integral. • Results in the Composite Trapezoid Formula:                               2 2 2 2 2 .... 2 ... a h a h b a a h b h a h a a h a h f a f a h f a h f a h b b h f b h f b I f x dx f x dx f x dx                                                b f ih a f a f h I n i 1 1 2 2 Composite Trapezoid Rule
  • 29.
    September 19, 2022OSU/CSE 541 29               n b f ih a f a f a b b f ih a f a f h I n i n i 2 2 2 2 1 1 1 1                      width Average height Composite Trapezoid Rule • Think of this as the width times the average height.
  • 30.
    September 19, 2022OSU/CSE 541 30 • The error can be estimated as: • Where, is the 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.     f n a b f h a b Ea         2 3 2 12 12 f   ) ( 2 h O Error
  • 31.
    September 19, 2022OSU/CSE 541 31 0 5 10 15 20 25 30 35 40 0 0.2 0.4 0.6 0.8 1 1.2 Example • Integrate: • from a=0.2 to b=0.8   5 4 3 2 200 810 730 140 20 3 . 0 x x x x x x f      
  • 32.
    September 19, 2022OSU/CSE 541 32         26 . 11 2 81 . 3 22 . 34 2 . 0 8 . 0 2        b f a f a b I Example • A single application of the Trapezoid rule. • Error:   3 12 1 a b f Et      
  • 33.
    September 19, 2022OSU/CSE 541 33 Example • We don’t know  so approximate with average f   3 2 4000 9720 4380 280 x x x x f          4 3 2 1000 3240 2190 280 20 x x x x x f         6 . 131 2 . 0 8 . 0 ) 2 . 0 ( ) 8 . 0 ( 2 . 0 8 . 0 8 . 0 2 . 0               f f dx f x f
  • 34.
    September 19, 2022OSU/CSE 541 34        3 2 2 3 12 12 1 131.6 0.8 0.2 2.37 12 t b a h b a E f f n            Example • The error can thus be estimated as:
  • 35.
    September 19, 2022OSU/CSE 541 35 0 5 10 15 20 25 30 35 40 0 0.2 0.4 0.6 0.8 1 1.2 True value of integral is 12.82. Trapezoid rule is 11.26 - within approx error - Et is 12%
  • 36.
    September 19, 2022OSU/CSE 541 36 • Use intervals (0.2,0.4),(0.4,0.6),(0.6,0.8): – (n = 3, h = 0.2)                       57 . 12 6 22 . 34 16 . 30 93 . 13 2 31 . 3 6 . 0 ) 3 )( 2 ( 8 . 0 6 . 0 4 . 0 2 2 . 0 2 . 0 8 . 0 2 2 1 1                   f f f f n b f ih a f a f a b I n i True value of integral is 12.82 Using Three Intervals
  • 37.
    September 19, 2022OSU/CSE 541 37 0 5 10 15 20 25 30 35 40 0 0.2 0.4 0.6 0.8 1 1.2 Et is now 2%
  • 38.
    September 19, 2022OSU/CSE 541 38                     76 . 12 12 22 . 34 22 . 35 16 . 30 18 . 22 93 . 13 34 . 7 2 31 . 3 6 . 0 ) 6 )( 2 ( 8 . 0 7 . 0 6 . 0 5 . 0 4 . 0 3 . 0 2 2 . 0 2 . 0 8 . 0                 f f f f f f f I Using Six Intervals • Use intervals (0.2,0.3),(0.3,0,4), etc. – (n = 6, h = 0.1) True value of integral is 12.82
  • 39.
    September 19, 2022OSU/CSE 541 39 0 5 10 15 20 25 30 35 40 0 0.2 0.4 0.6 0.8 1 1.2 Et is now 0.5%
  • 40.
    September 19, 2022OSU/CSE 541 40 Multi-dimensional Integration • Consider a two-dimensional case.   1 1 1 0 0 0 0 1 0 0 0 0 0 0 , , , , , n i i n i i n n i j i j n n i j i j i f x y dxdy A f y dy n i A f y dy n i j A A f n n i j A A f n n                                          
  • 41.
    September 19, 2022OSU/CSE 541 41 Multi-dimensional Integration • For the Trapezoid Rule, this leads to weights in the following pattern: 1 2 2 2 2 2 1 2 4 4 4 4 4 2 2 4 4 4 4 4 2 2 4 4 4 4 4 2 2 4 4 4 4 4 2 2 4 4 4 4 4 2 1 2 2 2 2 2 1               2 1 0, 0, 2 1, , 2 1, 1 1 2 {1, 1} 1, , 1 4 4 1, , 2 1, , 2 ij i n j n i n j n A i n j n n i n j n                       1 2 2 2 2 2 1 1 2 2 2 2 2 1
  • 42.
    September 19, 2022OSU/CSE 541 42 Multi-dimensional Integration • If we use the weights from the Trapezoid rule, the error is still O(h2). • However, there are now n2 function evaluations. – Equally-spaced samples on a square region.
  • 43.
    September 19, 2022OSU/CSE 541 43 Multi-dimensional Integration • In general, given k dimensions, we have N= nk function evaluations: • 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.       2 2 2 2 k k k O h O n O n O N                  
  • 44.
    September 19, 2022OSU/CSE 541 44 Reducing the Error • To improve the estimate of the integral, we can either: – Add more intervals – Use a higher order polynomial – Use Richardson Extrapolation to examine the limit as h0. • Called Romberg Integration
  • 45.
    September 19, 2022OSU/CSE 541 45 Adding More Intervals • If we have an estimate for one value of h, do we need to recompute everything for a value of h/2?       1 1 2 2 n h i h I f a f a ih f b             
  • 46.
    September 19, 2022OSU/CSE 541 46 Adding More Intervals • This is called the Recursive Trapezoid Rule in the book. • We have n 2n and hh/2.   2 1 1 2 1 1 1 0 1 0 ( ) 2 ( ) 4 2 ( ) 2 2 ( ) 4 2 2 2 4 2 n h i n n i i n h i h h I f a f a i f b h h f a f a ih f a ih f b I h h f a ih                                                               
  • 47.
    September 19, 2022OSU/CSE 541 47 • Given two numerical estimates obtained using different h’s, compute higher-order estimate • Starting with a step size h1, the exact value is • Suppose we reduce step size to h2 ) ( ) ( 1 1 n h O h A A   ) ( ) ( 2 2 n h O h A A   Recall Richardson Extrapolation
  • 48.
    September 19, 2022OSU/CSE 541 48 • Multiplying the 2nd eqn by (h1/h2)n and subtracting from the 1st eqn: • The new error term is generally O(h1 n+1 ) or O(h1 n+2 ). 1 ) ( ) ( 2 1 1 2 2 1                    n n h h h A h A h h A Richardson Extrapolation
  • 49.
    September 19, 2022OSU/CSE 541 49 • The true integral value can be written • This is true for any iteration Richardson Extrapolation     h E h I I           2 2 1 1 h E h I h E h I I    
  • 50.
    September 19, 2022OSU/CSE 541 50 Richardson Extrapolation • For example: Using (n = 2) • where c is a constant • Therefore:     2 2 2 1 2 1 h h h E h E  f ch E    2 [error = O(h2)] order of error in trapezoidal rule
  • 51.
    September 19, 2022OSU/CSE 541 51 • This leads to: • For integration, we have:     2 2 2 1 2 1 h h h E h E          2 2 2 2 2 1 2 1 h E h I h h h E h I    Richardson Extrapolation
  • 52.
    September 19, 2022OSU/CSE 541 52 Richardson Extrapolation • Solving for E(h2): • And plugging back into the estimated integral.       2 2 2 1 2 1 2 1 h h h I h I h E        2 2 h E h I I  
  • 53.
    September 19, 2022OSU/CSE 541 53 • Leads to: • Letting h2 = h1 /2 Richardson Extrapolation           1 2 2 2 1 2 1 / 1 h I h I h h h I I                 1 2 1 2 2 2 3 1 3 4 1 2 1 h I h I I h I h I h I I      
  • 54.
    September 19, 2022OSU/CSE 541 54 • We combined two O(h2) estimates to get an O(h4) estimate. • Can also combine two O(h4) estimates to get an O(h6) estimate.     l m h I h I I 15 1 15 16   Romberg Integration
  • 55.
    September 19, 2022OSU/CSE 541 55 Romberg Integration • Greater weight is placed on the more accurate estimate. • Weighting coefficients sum to unity – i.e, (16-1)/15=1 • Can continue, by combining two O(h6) estimates to get an O(h8) estimate.     l m h I h I I 63 1 63 64  
  • 56.
    September 19, 2022OSU/CSE 541 56 • General pattern is called Romberg Integration – j : level of subdivision, j+1 has more intervals. – k : level of integration, k = 1 is original trapezoid estimate [O(h2)], k = 2 is improved [O(h4)], etc.   , 1, , 1 , , 1, 4 1 4 1 4 1 k j k j k j k j k j k j k k k I I I I I I           Romberg Integration
  • 57.
    September 19, 2022OSU/CSE 541 57 Romberg Integration • For example, j = 1, k = 1 leads to   1,0 0,0 1 1,1 1 4 4 1 3 3 2 3 I I h I I I h          
  • 58.
    September 19, 2022OSU/CSE 541 58 • Consider the function: • Integrate from a = 0 to b = 0.8 • Using the trapezoidal rule yields the following results: 5 4 3 2 400 900 675 200 25 2 . 0 ) ( x x x x x x f       Example
  • 59.
    September 19, 2022OSU/CSE 541 59 intervals Integral 1 0.8 0.1728 2 0.4 1.0688 4 0.2 1.4848 h Example • Trapezoid Rules: Exact integral is 1.64053334 3674667 . 1 ) 1728 . 0 ( 3 1 ) 0688 . 1 ( 3 4    I (j=1, k=1) k j k = 0 k = 1 j = 0 j = 1 j = 2
  • 60.
    September 19, 2022OSU/CSE 541 60 2 4 segments ( ) ( ) 1 0.8 0.1728 2 0.4 1.0688 1.3674667 4 0.2 1.4848 h O h O h k j 62346667 . 1 ) 0688 . 1 ( 3 1 ) 4848 . 1 ( 3 4    I (j=2, k=1) k = 1 k = 0 Exact integral is 1.64053334 Example
  • 61.
    September 19, 2022OSU/CSE 541 61 Example 2 4 segments ( ) ( ) 1 0.8 0.1728 2 0.4 1.0688 1.3674667 4 0.2 1.4848 1.62346667 h O h O h k j (j=2, k=2) k = 2 k = 1 Exact integral is 1.64053334 64053334 . 1 ) 3674667 . 1 ( 15 1 ) 62346667 . 1 ( 15 16    I
  • 62.
    September 19, 2022OSU/CSE 541 62 2 4 6 segments ( ) ( ) ( ) 1 0.8 0.1728 2 0.4 1.0688 1.3674667 4 0.2 1. h O h O h O h 4848 1.62346667 1.64053334 k j k = 3 k = 2 k = 1 Example
  • 63.
    September 19, 2022OSU/CSE 541 63 2 4 6 8 segments ( ) ( ) ( ) ( ) 1 0.8 0.1728 2 0.4 1.0688 1.3674667 4 h O h O h O h O h 0.2 1.4848 1.62346667 1.64053334 8 0.1 ?? ?? ?? ?? ?? ) 64053334 . 1 ( 63 1 (??) 63 64    I (j=3, k=3) k j k = 3 Example • Better and better results can be obtained by continuing this
  • 64.
    September 19, 2022OSU/CSE 541 64 Romberg Integration • Is this that significant? • Consider the cost of computing the Trapezoid Rule for 1000 data points. – Refinement would lead to 2000 data points. • Implies an additional 1003 operations using the Recursive Trapezoid Rule. • Not to mention the 1000 (expensive) function evals. – Romberg Integration cost: • Three additional operations – no function evals!!!
  • 65.
    September 19, 2022OSU/CSE 541 65 Higher-Order Polynomials • Recall: ) ( 3/8 s Simpson' cubic 3 ) ( 1/3 s Simpson' quadratic 2 ) ( Trapezoid linear 1 Error Formula Polynomial 4 4 2 h O h O h O m 1 1 2 1 0 0 1 2 0 0 0 ( ) ( ) ( ) ... ( ) n m m m n n m n mn x x x x m m m x x x x f x dx p x dx p x dx p x dx             
  • 66.
    September 19, 2022OSU/CSE 541 66 • If we use a 2nd order polynomial (need 3 points or 2 intervals): – Lagrange form.                         dx x f x x x x x x x x x f x x x x x x x x x f x x x x x x x x I x x                       2 1 2 0 2 1 0 1 2 1 0 1 2 0 0 2 0 1 0 2 1 2 0         2 2 0 1 x x x Simpson’s 1/3 Rule
  • 67.
    September 19, 2022OSU/CSE 541 67 Simpson’s 1/3 Rule • Requiring equally-spaced intervals:                        2 0 0 0 0 0 0 1 0 0 2 2 2 2 2 x x x x h x x h x x x x h I f x f x h h h h x x x x h f x dx h h                       
  • 68.
    September 19, 2022OSU/CSE 541 68 • Integrate and simplify: 0 2 4 6 8 10 12 3 5 7 9 11 13 15         2 1 0 4 3 x f x f x f h I    2 a b h   Simpson’s 1/3 Rule Quadratic Polynomial
  • 69.
    September 19, 2022OSU/CSE 541 69 • If we use a = x0 and b = x2, and x1 = (b+a)/2 Simpson’s 1/3 Rule         6 4 2 1 0 x f x f x f a b I     width average height
  • 70.
    September 19, 2022OSU/CSE 541 70 • Error for Simpson’s 1/3 rule Integrates a cubic exactly:     0 4   f             4 5 4 5 2880 90 f a b f h Et      2 a b h   ) ( 4 h O Simpson’s 1/3 Rule
  • 71.
    September 19, 2022OSU/CSE 541 71 Composite Simpson’s 1/3 Rule • As with Trapezoidal rule, can use multiple applications of Simpson’s 1/3 rule. • Need even number of intervals – An odd number of points are required.
  • 72.
    September 19, 2022OSU/CSE 541 72 Composite Simpson’s 1/3 Rule • Example: 9 points, 4 intervals -5 0 5 10 15 20 25 0 0.5 1 1.5 2 2.5 3 3.5 4 4.5
  • 73.
    September 19, 2022OSU/CSE 541 73 • As in composite trapezoid, break integral up into n/2 sub-integrals: • Substitute Simpson’s 1/3 rule for each integral and collect terms. Composite Simpson’s 1/3 Rule               n n x x x x x x dx x f dx x f dx x f I 2 4 2 2 0 ...           1 2 0 1,3,5 2,4,6 4 2 3 n n i j n i j f x f x f x f x I b a n            n+1 data points, an odd number
  • 74.
    September 19, 2022OSU/CSE 541 74 • Odd coefficients receive a weight of 4, even receive a weight of 2. • Doesn’t seem very fair, does it? coefficients on numerator 1 4 1 1 4 1 1 4 1 i = 0 i = n Composite Simpson’s 1/3 Rule
  • 75.
    September 19, 2022OSU/CSE 541 75 • The error can be estimated by: • If n is doubled, h  h/2 and Ea  Ea/16   ) 4 ( 4 ) 4 ( 5 180 180 f h a b f nh Ea    ) 4 ( f is the average 4th derivative ) ( 4 h O Error Estimate
  • 76.
    September 19, 2022OSU/CSE 541 76 • Integrate from a = 0 to b = 2. • Use Simpson’s 1/3 rule:         2 2 0 1 2 0 0 1 4 1 4 ( ) 3 1 0 4 (1) 2 3 1 ( 4 ) 0.82994 3 x I e dx h f x f x f x f f f e e e                        2 ) ( x e x f   2 1 2 0 1 2 2 1 0           b x b a x a x a b h Example
  • 77.
    September 19, 2022OSU/CSE 541 77 • Error estimate: • Where h = b - a and a <  < b • Don’t know  – use average value      4 5 90 f h Et                 4 4 4 5 5 0 1 2 4 1 1 90 90 3 t a f x f x f x E E f            Example
  • 78.
    September 19, 2022OSU/CSE 541 78 • Let’s look at the polynomial again: – From a = 0 to b = 0.8 5 4 3 2 400 900 675 200 25 2 . 0 ) ( x x x x x x f       8 . 0 4 . 0 2 0 4 . 0 2 2 1 0           b x b a x a x a b h             36746667 . 1 8 . 0 ) 4 . 0 ( 4 0 3 ) 4 . 0 ( ) ( 4 3 1 ) ( 2 1 0 2 0          f f f x f x f x f h dx x f I Exact integral is 1.64053334 Another Example
  • 79.
    September 19, 2022OSU/CSE 541 79 • Actual Error: (using the known exact value) • Estimate error: (if the exact value is not available) • Where a <  < b.      4 5 90 f h Et   27306666 . 0 36746667 . 1 - 1.64053334   E 16% Error
  • 80.
    September 19, 2022OSU/CSE 541 80 • Compute the fourth-derivative • Matches actual error pretty well. Error x x f 48000 21600 ) ( ) 4 (            27306667 . 0 4 . 0 90 4 . 0 90 4 . 0 4 5 1 4 5       f x f E E a t middle point
  • 81.
    September 19, 2022OSU/CSE 541 81 • If we use 4 segments instead of 1: – x = [0.0 0.2 0.4 0.6 0.8] 0.2 b a h n    232 . 0 ) 8 . 0 ( 464 . 3 ) 6 . 0 ( 456 . 2 ) 4 . 0 ( 288 . 1 ) 2 . 0 ( 2 . 0 ) 0 (      f f f f f             6234667 . 1 12 232 . 0 ) 456 . 2 ( 2 ) 464 . 3 288 . 1 ( 4 2 . 0 8 . 0 ) 4 )( 3 ( ) 8 . 0 ( ) 6 . 0 ( 4 ) 4 . 0 ( 2 ) 2 . 0 ( 4 ) 0 ( 0 8 . 0 3 2 4 1 5 , 3 , 1 2 6 , 4 , 2 0                        f f f f f n x f x f x f x f a b I n n i n j j i Exact integral is 1.64053334 Example Continued
  • 82.
    September 19, 2022OSU/CSE 541 82 • Actual Error: (using the known exact value) • Estimate error: (if the exact value is not available) 01706667 . 0 6234667 . 1 - 1.64053334   E 1%         0085 . 0 4 . 0 90 2 . 0 90 2 . 0 4 5 2 4 5        f x f E E a t middle point Error
  • 83.
    September 19, 2022OSU/CSE 541 83 Error • Actual is twice the estimated, why? • Recall: x x f 48000 21600 ) ( ) 4 (        (4) (4) 0,0.8 (4) max ( ) (0) 21600 (0.4) 2400 x f x f f     
  • 84.
    September 19, 2022OSU/CSE 541 84 Error • Rather than estimate, we can bound the absolute value of the error: • Five times the actual, but provides a safer error metric.         5 5 4 4 0.2 0.2 0 0.0768 90 90 a E f f     
  • 85.
    September 19, 2022OSU/CSE 541 85 Simpon’s 1/3 Rule • Simpson’s 1/3 rule uses a 2nd order polynomial – need 3 points or 2 intervals – This implies we need an even number of intervals. • What if you don’t have an even number of intervals? Two choices: 1. Use Simpson’s 1/3 on all the segments except the last (or first) one, and use trapezoidal rule on the one left. – Pitfall - larger error on the segment using trapezoid 2. Use Simpson’s 3/8 rule.
  • 86.
    September 19, 2022OSU/CSE 541 86 • Simpson’s 3/8 rule uses a third order polynomial – need 3 intervals (4 data points) 3 3 2 2 1 0 3 ) ( ) ( x a x a x a a x p x f              3 0 3 0 3 x x x x dx x p dx x f I Simpson’s 3/8 Rule
  • 87.
    September 19, 2022OSU/CSE 541 87 Simpson’s 3/8 Rule • Determine a’s with Lagrange polynomial • For evenly spaced points 3 a b h             3 2 1 0 3 3 8 3 x f x f x x f h I    
  • 88.
    September 19, 2022OSU/CSE 541 88 • Same order as 1/3 Rule. – More function evaluations. – Interval width, h, is smaller. • Integrates a cubic exactly:      0 4   f     4 5 3 80 t E h f    ) ( 4 h O Error
  • 89.
    September 19, 2022OSU/CSE 541 89 Comparison • Simpson’s 1/3 rule and Simpson’s 3/8 rule have the same order of error – O(h4) – trapezoidal rule has an error of O(h2) • Simpson’s 1/3 rule requires even number of segments. • Simpson’s 3/8 rule requires multiples of three segments. • Both Simpson’s methods require evenly spaced data points
  • 90.
    September 19, 2022OSU/CSE 541 90 • n = 10 points  9 intervals – First 6 intervals - Simpson’s 1/3 – Last 3 intervals - Simpson’s 3/8 Simpson’s 1/3 Simpson’s 3/8 Mixing Techniques
  • 91.
    September 19, 2022OSU/CSE 541 91 • We can examine even higher-order polynomials. – Simpson’s 1/3 - 2nd order Lagrange (3 pts) – Simpson’s 3/8 - 3rd order Lagrange (4 pts) • Usually do not go higher. • Use multiple segments. – But only where needed. Newton-Cotes Formulas
  • 92.
    September 19, 2022OSU/CSE 541 92 Adaptive Simpson’s Scheme • Recall Simpson’s 1/3 Rule: • Where initially, we have a=x0 and b=x2. • Subdividing the integral into two:         2 1 0 4 3 x f x f x f h I              1 2 3 4 2 4 6 h I f a f x f x f x f b         
  • 93.
    September 19, 2022OSU/CSE 541 93 Adaptive Simpson’s Scheme • We want to keep subdividing, until we reach a desired error tolerance, . • Mathematically:                     1 1 2 3 4 3 4 2 4 6 b a b a h f x dx f a f x f b h f x dx f a f x f x f x f b                                  
  • 94.
    September 19, 2022OSU/CSE 541 94 Adaptive Simpson’s Scheme • This will be satisfied if: • The left and the right are within one-half of the error.                 1 2 2 3 2 4 , 6 2 4 , 6 2 2 c a b c h f x dx f a f x f x and h f x dx f x f x f b where a b c x                                   
  • 95.
    September 19, 2022OSU/CSE 541 95 Adaptive Simpson’s Scheme • Okay, now we have two separate intervals to integrate. • What if one can be solved accurately with an h=10-3, but the other requires many, many more intervals, h=10-6?
  • 96.
    September 19, 2022OSU/CSE 541 96 Adaptive Simpson’s Scheme • Adaptive Simpson’s method provides a divide and conquer scheme until the appropriate error is satisfied everywhere. • Very popular method in practice. • Problem: – We do not know the exact value, and hence do not know the error.
  • 97.
    September 19, 2022OSU/CSE 541 97 Adaptive Simpson’s Scheme • How do we know whether to continue to subdivide or terminate?                 5 4 , , , , 4 , 6 2 1 , 90 2 b a I f x dx S a b E a b where b a a b S a b f a f f b and b a E a b f                              
  • 98.
    September 19, 2022OSU/CSE 541 98 Adaptive Simpson’s Scheme • The first iteration can then be defined as: • Subsequent subdivision can be defined as:       2 , , S S a c S c b               1 1 1 1 , , , , I S E where S S a b E E a b    
  • 99.
    September 19, 2022OSU/CSE 541 99 Adaptive Simpson’s Scheme • Now, since • We can solve for E(2) in terms of E(1).           5 5 2 4 4 5 4 1 4 1 / 2 1 / 2 90 2 90 2 1 1 1 2 90 2 16 h h E f f h f E                                     2 , , E E a c E c b  
  • 100.
    September 19, 2022OSU/CSE 541 100 Adaptive Simpson’s Scheme • Finally, using the identity: • We have: • Plugging into our definition:         1 1 2 2 I S E S E               2 1 1 2 2 15 S S E E E                 2 2 2 2 1 1 15 I S E S S S     
  • 101.
    September 19, 2022OSU/CSE 541 101 Adaptive Simpson’s Scheme • Our error criteria is thus: • Simplifying leads to the termination formula:         2 2 1 1 15 I S S S            2 1 15 S S   
  • 102.
    September 19, 2022OSU/CSE 541 102 Adaptive Simpson’s Scheme • What happens graphically:
  • 103.
    September 19, 2022OSU/CSE 541 103 1 2 15 S subd v S i ide    
  • 104.
    September 19, 2022OSU/CSE 541 104
  • 105.
    September 19, 2022OSU/CSE 541 105 1 2 15 2 subdivide S S    
  • 106.
    September 19, 2022OSU/CSE 541 106   2 1 2 1 15 S I S S    1 2 15 4 S o S d ne    
  • 107.
    September 19, 2022OSU/CSE 541 107 1 2 15 4 subdivide S S    
  • 108.
    September 19, 2022OSU/CSE 541 108   2 1 2 1 15 S I S S   
  • 109.
    September 19, 2022OSU/CSE 541 109   2 1 2 1 15 S I S S   
  • 110.
    September 19, 2022OSU/CSE 541 110 I=Ileft + Iright Iright =Ileft + Iright
  • 111.
    September 19, 2022OSU/CSE 541 111
  • 112.
    September 19, 2022OSU/CSE 541 112
  • 113.
    September 19, 2022OSU/CSE 541 113 Adaptive Simpson’s Scheme • We gradually capture the difficult spots.
  • 114.
    September 19, 2022OSU/CSE 541 114 Adaptive Simpson’s Code • Simple Recursive Program static const int m_nMaximum_Divisions = 1000; Real IntegrationSimpson( const Real (*f) (Real x), const Real start, const Real end, const Real error_tolerance, int &level ) { level += 1; Real h = (end – start); Real midpoint = (start + end) / 2.0; Real f_start = f(start); Real f_end = f(end); Real f_mid = f(midpoint ); oneLevel = h*( f_start + 4.0*f_mid + f_end) / 6.0; Real leftMidpoint = (start+ midpoint ) / 2.0; Real rightMidpoint = (end+ midpoint ) / 2.0 Real f_midLeft = f(leftMidpoint ); Real f_midRight = f(rightMidpoint ); twoLevel = h*(f_start + 4.0* f_midLeft + 2.0* f_mid + 4.0* f_midRight + f_end) / 12.0; if( level >= m_nMax_Divisions ) // Terminate the process, converging too slow return twoLevel;
  • 115.
    September 19, 2022OSU/CSE 541 115 Adaptive Simpson’s Code if( absf( twoLevel – oneLevel) < 15.0*error_tolerance) // Desired solution reached return twoLevel + (twoLevel-oneLevel) / 15.0; // // Otherwise, split the interval in two and recursively evaluate each half. // leftIntegral = IntegrationSimpson( f, start, midpoint , error_tolerance/2.0, level ); rightIntegral = IntegrationSimpson( f, midpoint , end, error_tolerance/2.0, level ); return leftIntegral + rightIntegral; }
  • 116.
    September 19, 2022OSU/CSE 541 116 • Idea is that if we evaluate the function at certain points, and sum with certain weights, we will get a more accurate integral • Evaluation points and weights are pre-computed and tabulated • Basic form:       1 1 1 ) ( ) ( n i i i x f c dx x f I ci : weighting factors xi : sampling points selected optimally Guassian Quadrature New!!
  • 117.
    September 19, 2022OSU/CSE 541 117 • Note that the interval is between –1 and 1 • For other intervals, a change of variables is used to transfer the problem so that it utilizes the interval [-1, 1] • This is a linear transform, such that for t[a,b]: • We have for x[-1,1]:  b a dt t f ) ( 2 ) ( a b x a b t     a b a b t x     2 Guassian Quadrature
  • 118.
    September 19, 2022OSU/CSE 541 118 • As t = a  x = -1 • As t = b  x = 1 dx a b dt 2 ) (             2 ) ( ) ( a b x a b f t f dx a b x a b f a b dt t f b a               1 1 2 ) ( 2 ) ( ) ( Guassian Quadrature
  • 119.
    September 19, 2022OSU/CSE 541 119 • Basic form of Gaussian quadrature: • For n=2, we have: • This leads to 4 unknowns: c1, c2, x1, and x2 – two unknown weights (c1, c2) – two unknown sampling points (x1, x2)       1 1 1 ) ( ) ( n i i i x f c dx x f I     2 2 1 1 x f c x f c I   Guassian Quadrature
  • 120.
    September 19, 2022OSU/CSE 541 120 Guassian Quadrature • 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!!!
  • 121.
    September 19, 2022OSU/CSE 541 121 • In particular, let’s look at these simple polynomials: – Constant • f(x)=1 – Linear • f(x)=x – Quadratic • f(x)=x2 – Cubic • f(x)=x3 Guassian Quadrature
  • 122.
    September 19, 2022OSU/CSE 541 122 Guassian Quadrature • Recalling the formula: – Constant • f(x)=1 – Linear • f(x)=x – Quadratic • f(x)=x2 – Cubic • f(x)=x3                 3 2 2 3 1 1 2 2 1 1 1 1 3 2 2 2 2 1 1 2 2 1 1 1 1 2 2 2 1 1 2 2 1 1 1 1 2 1 2 2 1 1 1 1 0 3 2 0 2 1 x c x c x f c x f c dx x x c x c x f c x f c dx x x c x c x f c x f c xdx c c x f c x f c dx                                 2 2 1 1 x f c x f c I  
  • 123.
    September 19, 2022OSU/CSE 541 123 • We can now solve for our unknowns: – Note, this is not an easy problem and will not be covered in this class. 577 . 0 3 1 577 . 0 3 1 1 2 1 2 1         x x c c Guassian Quadrature
  • 124.
    September 19, 2022OSU/CSE 541 124 Guassian Quadrature • This yields the two point Gauss-Legendre formula                3 1 3 1 f f I
  • 125.
    September 19, 2022OSU/CSE 541 125 Guassian Quadrature • This is exact for all polynomials up to and including degree 3 (cubics).       1 1 1 1 1 3 2 3 2 1 1 1 1 1 3 3 2 2 1 3 1 3 1 1 1 1 1 1 1 1 3 3 3 3 3 3 3 2 ax bx cx d dx a x dx b x dx c xdx d dx a b c d ax bx cx d                                                                                           
  • 126.
    September 19, 2022OSU/CSE 541 126 x f(x) -1 1 f(0.577) f(-0.577) -0.577 0.577     1 1 ) 577 . 0 ( ) 577 . 0 ( ) ( f f dx x f Guassian Quadrature
  • 127.
    September 19, 2022OSU/CSE 541 127 Example 5 4 3 2 400 900 675 200 25 2 . 0 ) ( x x x x x x f       dx a b x a b f a b dt t f b a               1 1 2 ) ( 2 ) ( ) (  dt t f dt t f dx x f I                    1 1 1 1 8 . 0 0 4 . 0 4 . 0 4 . 0 2 0 8 . 0 ) 0 8 . 0 ( 2 ) 0 8 . 0 ( ) ( • Integrate f(x) from a = 0 to b = 0.8 • Transform from [0, 0.8] to [-1, 1]
  • 128.
    September 19, 2022OSU/CSE 541 128 • Solving • And substituting for the 2-point formula: Exact integral is 1.64053334   dt t t t t t dt t f I                        1 1 5 4 3 2 1 1 ) 4 . 0 4 . 0 ( 400 ) 4 . 0 4 . 0 ( 900 ) 4 . 0 4 . 0 ( 675 ) 4 . 0 4 . 0 ( 200 ) 4 . 0 4 . 0 ( 25 2 . 0 4 . 0 4 . 0 4 . 0 4 . 0 3 / 1   t 82257778 . 1 30583723 . 1 51674055 . 0    I  dt t f I   1 1 4 . 0 Example
  • 129.
    September 19, 2022OSU/CSE 541 129 • Recall the basic form: • Let’s look at n=3. • We now have 6 unknowns: c1, c2, c3,x1, x2, and x3 – three unknown weights (c1, c2 , c3) – three unknown sampling points (x1, x2 , x3)       1 1 1 ) ( ) ( n i i i x f c dx x f I       3 3 2 2 1 1 x f c x f c x f c I    Higher-order Gaussian Quadrature
  • 130.
    September 19, 2022OSU/CSE 541 130 Use 6 equations - constant, linear, quadratic, cubic, 4th order and 5th order to find those unknowns                                     5 3 3 5 2 2 5 1 1 3 3 2 2 1 1 1 1 5 4 3 3 4 2 2 4 1 1 3 3 2 2 1 1 1 1 4 3 3 3 3 2 2 3 1 1 3 3 2 2 1 1 1 1 3 2 3 3 2 2 2 2 1 1 3 3 2 2 1 1 1 1 2 3 3 2 2 1 1 3 3 2 2 1 1 1 1 3 2 1 3 3 2 2 1 1 1 1 0 5 2 0 3 2 0 2 1 x c x c x c x f c x f c x f c dx x x c x c x c x f c x f c x f c dx x x c x c x c x f c x f c x f c dx x x c x c x c x f c x f c x f c dx x x c x c x c x f c x f c x f c xdx c c c x f c x f c x f c dx                                                      
  • 131.
    September 19, 2022OSU/CSE 541 131 • Can solve these equations (or have some one smarter than us, like Guass solve them). • Produces the three point Gauss-Legendre formula – Exact for polynomials up to and including degree 5 (because using 5th degree polynomial) 77459669 . 0 5 / 3 0 77459669 . 0 5 / 3 9 / 5 9 / 8 9 / 5 2 2 1 3 2 1           x x x c c c       3 3 2 2 1 1 x f c x f c x f c I    Higher-order Gaussian Quadrature
  • 132.
    September 19, 2022OSU/CSE 541 132      1 1 ) 775 . 0 ( 9 5 ) 0 . 0 ( 9 8 ) 775 . 0 ( 9 5 ) ( f f f dx x f -1 1 x f(0.775) f(-0.775) -0.775 0.775 f(0) Higher-order Gaussian Quadrature
  • 133.
    September 19, 2022OSU/CSE 541 133 5 4 3 2 400 900 675 200 25 2 . 0 ) ( x x x x x x f       Integrate from a = 0 to b = 0.8 Transform from [0, 0.8] to [-1, 1]   dt t t t t t dx x f I                      1 1 5 4 3 2 8 . 0 0 ) 4 . 0 4 . 0 ( 400 ) 4 . 0 4 . 0 ( 900 ) 4 . 0 4 . 0 ( 675 ) 4 . 0 4 . 0 ( 200 ) 4 . 0 4 . 0 ( 25 2 . 0 Example replace -0.4 with +0.4
  • 134.
    September 19, 2022OSU/CSE 541 134                       5 3 9 5 0 9 8 5 3 9 5 f f f I 64053334 . 1 485987599 . 0 873244444 . 0 281301290 . 0     I Substitute into the transform equation and get Exact integral is 1.64053334 Example • Using the 3-point Gauss-Legendre formula:
  • 135.
    September 19, 2022OSU/CSE 541 135 Can develop higher order Gauss-Legendre forms using       n n x f c x f c x f c I     ... 2 2 1 1 Values for c’s and x’s are tabulated Use the same transformation to map interval onto [-1, 1] Gaussian Quadrature
  • 136.
    September 19, 2022OSU/CSE 541 136 1713245 . 0 3607616 . 0 4679139 . 0 4679139 . 0 3607616 . 0 1713245 . 0 50 0.23692688 05 0.47862867 89 0.56888888 05 0.47862867 50 0.23692688 51 0.34785484 49 0.65214515 49 0.65214515 51 0.34785484 56 0.55555555 89 0.88888888 56 0.55555555 0 . 1 0 . 1 932469514 0 661209386 0 238619186 0 238619186 0 661209386 0 932469514 0 9061798459 0 5384693101 0 0000000000 0 5384693101 0 9061798459 0 8611363116 0 3399810436 0 3399810436 0 8611363116 0 7745966692 0 0000000000 0 7745966692 0 5773502692 0 5773502692 0 . . . . . . . . . . . . . . . . . . . .          2 3 4 5 6 n ci xi       n n x f c x f c x f c dx x f I       ... ) ( 2 2 1 1 1 1
  • 137.
    September 19, 2022OSU/CSE 541 137 Gaussian Quadrature • Requires function evaluations at non- uniformly spaced points within the integration interval – not appropriate for cases where the function is unknown – not suited for dealing with tabulated data that appear in many engineering problems • If the function is known, its efficiency can be a decided advantage
  • 138.
    September 19, 2022OSU/CSE 541 138 Gaussian Quadrature • Problems: – If we add more data points, like doubling the number of sample points.