Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Kuliah5.1 - (Integrasi Numerik)

Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 120

Numerical Integration

Roger Crawfis
Outline
• Definite Integrals
• Lower and Upper Sums
– Reimann Integration or Reimann Sums
• Uniformly-spaced samples
– Trapezoid Rules
– Simpson’s Rules
– Adaptive Simpson’s Scheme
• Non-uniformly spaced samples
– Gaussian Quadrature Formulas

February 4, 2020 OSU/CSE 541 2


Motivation
What does an integral represent?
d b
 f ( x)dxdy  volume
b
a
f ( x)dx  area c a

Basic definition of an integral: f(x)


n
f ( x )dx  lim  f ( xk )x
b
a n 
k 1

where x  b  a
n

sum of height  width x


February 4, 2020 OSU/CSE 541 3
Motivation
b
• Evaluate the integral, I   a
f ( x)dx without doing the
calculation 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

– Integrand is not precisely defined by an equation, i.e.,


we are given a set of data (xi, yi), i = 1, 2, 3, …,n
February 4, 2020 OSU/CSE 541 4
Reimann Integral Theorem
• Integration is a summing process. Thus virtually
all numerical approximations can be represented
by n
I   f ( x )dx   wi f ( xi )  Et
b

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.

February 4, 2020 OSU/CSE 541 5


Partitioning the Integral
• The most common numerical integration formula
is based on equally spaced data points.
xn
 x0
f ( x)dx

• Divide [x0 , xn] into n intervals (n1)


x1 x2 xn
xn
 x0
f ( x)dx   f ( x)   f ( x) 
x0 x1
 
xn1
f ( x)

February 4, 2020 OSU/CSE 541 6


Upper Sums

• Assume that f(x)>0 everywhere.


• If within each interval, we could determine
the maximum value of the function, then we
have: xn n 1

 f ( x)   M  x
x0 i 0
i i 1  xi 

• where
M i  sup f ( x) : xi  x  xi 1 Supremum - least
upper bound

February 4, 2020 OSU/CSE 541 7


Upper Sums

• Graphically:

x0 x1 x2 x3 x4

February 4, 2020 OSU/CSE 541 8


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: f ( x)  m x  x
xn n 1

  
i 0
i i 1 i 
x0

• where
mi  inf  f ( x) : xi  x  xi 1
Infimum - greatest
lower bound

February 4, 2020 OSU/CSE 541 9


Lower Sums

• Graphically

x0 x1 x2 x3 x4

February 4, 2020 OSU/CSE 541 10


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.

February 4, 2020 OSU/CSE 541 11


Bounding the Integral

• Graphically

x0 x1 x2 x3 x4

February 4, 2020 OSU/CSE 541 12


Bounding the Integral

• Halving each interval (much like Lab1):

x0 x3 x5 x7 x9

February 4, 2020 OSU/CSE 541 13


Bounding the Integral

• One more time:

x0 x5 x7 x9 x11

February 4, 2020 OSU/CSE 541 14


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 x7 x9
February 4, 2020 OSU/CSE 541 15
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?

February 4, 2020 OSU/CSE 541 16


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

February 4, 2020 OSU/CSE 541 17


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

• Different choices for m’s lead to different


formulas: m Polynomial Formula Error
1 linear Trapezoid O(h 2 )
2 quadratic Simpson' s 1/3 O(h 4 )
3 cubic Simpson' s 3/8 O(h 4 )
February 4, 2020 OSU/CSE 541 18
Trapezoid Rule
• Simplest way to approximate the area under a
curve – using first order polynomial (a straight
line)
• Using Newton’s form of the interpolating
polynomial: f b   f a 
p1  x   f a   x  a 
ba
• Now, solve for the integral:

I   f x dx   p1 x dx
b b

a a

February 4, 2020 OSU/CSE 541 19


Trapezoid Rule

 f b   f a  
 f a   x  a dx
b
I 
a ba 
Trapezoid Rule
f(b)

I
b  a
 f a   f b
f(a)
2

I  width  average height


a b
February 4, 2020 OSU/CSE 541 20
Trapezoid Rule

• Improvement?

x0 x1 x2 x3 x4

February 4, 2020 OSU/CSE 541 21


Trapezoid Rule Error
• The integration error is:
(b  a )
f  h 3   f  h 2
1
Et  
12 12
O(h 3 )
• 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)

February 4, 2020 OSU/CSE 541 22


Example
 x2
Integrate from f ( x)  e a = 0 to b = 2

Use trapezoidal rule:

2
I   e dx  x2
0


b  a
 f a   f b  (2  0)
 f (2)  f (0)
2 2
 1  (e 4  e0 )  1.0183

February 4, 2020 OSU/CSE 541 23


Example

Estimate error: Et   1 f  h 3


12
Where h = b - a and a <  < b

Don’t know  - use average value


f ( x)  (2  4 x )e
2  x2 f (0)  2
h  20  2 f ( 2)  0.2564

23  f 0  f 2
Et  Ea    0.58
12 2
February 4, 2020 OSU/CSE 541 24
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

February 4, 2020 OSU/CSE 541 25


Composite Trapezoid Rule
• If we do multiple intervals, we can avoid duplicate
function evaluations and operations:
• Use n+1 equally spaced points.
• Each interval has: h  b n a
• Break up the limits of integration and expand.
a h a 2 h
f x dx   f x dx  ...   f x dx
b
I 
a a h bh

February 4, 2020 OSU/CSE 541 26


Composite Trapezoid Rule
• Substituting the trapezoid rule for each integral.
ah a2h
I   f  x  dx  ah f  x  dx  ...  bh f  x  dx
b

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

• Results in the Composite Trapezoid Formula:


h n 1

I   f a   2 f a  ih   f b 
2 i 1 
February 4, 2020 OSU/CSE 541 27
Composite Trapezoid Rule

• Think of this as the width times the average


height.
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

February 4, 2020 OSU/CSE 541 28


Error

• The error can be estimated as:



b  a h 2
f  
b  a
3
f 
2
Ea
12 12n 2 O(h )

• Where, f  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.
February 4, 2020 OSU/CSE 541 29
Example

• Integrate: f x   0.3  20 x  140 x 2  730 x 3  810 x 4  200 x5

• 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
February 4, 2020 OSU/CSE 541 30
Example

• A single application of the Trapezoid rule.


f a   f b 
I  b  a 
2
34.22  3.81
 0.8  0.2
2
 11.26
• Error:
f  b  a 
1
Et  
3

12

February 4, 2020 OSU/CSE 541 31


Example

• We don’t know  so approximate with


average f
f x   20  280 x  2190 x 2  3240 x 3  1000 x 4

f x   280  4380 x  9720 x 2  4000 x 3


0.8

f  x  

0.2
f  dx
0.8  0.2
f (0.8)  f (0.2)
  131.6
0.8  0.2
February 4, 2020 OSU/CSE 541 32
Example

• The error can thus be estimated as:


 b  a h  b  a
2 3

Et  f   2
f 
12 12n
1
   131.6  0.8  0.2   2.37
3

12

February 4, 2020 OSU/CSE 541 33


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

February 4, 2020 OSU/CSE 541 34


Using Three Intervals

• Use intervals (0.2,0.4),(0.4,0.6),(0.6,0.8):


– (n = 3, h = 0.2)
n 1
f a   2 f a  ih   f b 
I  b  a  i 1
2n
f 0.2   2 f 0.4   f 0.6   f 0.8
 0.8  0.2 
( 2)( 3)
3.31  213.93  30.16  34.22
 0.6
6
 12.57
True value of integral is 12.82
February 4, 2020 OSU/CSE 541 35
Et is now 2%
40

35

30

25

20

15

10

0
0 0.2 0.4 0.6 0.8 1 1.2

February 4, 2020 OSU/CSE 541 36


Using Six Intervals

• Use intervals (0.2,0.3),(0.3,0,4), etc.


– (n = 6, h = 0.1)

f 0.2   2 f 0.3  f 0.4   f 0.5  f 0.6  f 0.7   f 0.8


I  0.8  0.2 
(2)( 6)
3.31  27.34  13.93  22.18  30.16  35.22   34.22
 0.6
12
 12.76

True value of integral is 12.82


February 4, 2020 OSU/CSE 541 37
Et is now 0.5%
40

35

30

25

20

15

10

0
0 0.2 0.4 0.6 0.8 1 1.2

February 4, 2020 OSU/CSE 541 38


Multi-dimensional Integration

• Consider a two-dimensional case.


i 
1 1 1 n

 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 Aj f  , 
i 0 j 0 n n
February 4, 2020 OSU/CSE 541 39
Multi-dimensional Integration

• For the Trapezoid Rule, this leads to


weights in the following pattern:
1 1 2 2 2 2 2 1
2 2 4 4 4 4 4 2 1 i  0, n j  0, n

2 2 4 4 4 4 4 2 1 2 i  1, , n  2 j  1, n  1
Aij  2 
2 2 4 4 4 4 4 2
4n  2 i  {1, n  1} j  1, , n  1
4 i  1, , n  2 j  1, , n  2
2 2 4 4 4 4 4 2
2 2 4 4 4 4 4 2 
1 1 2 2 2 2 2 1

1 2 2 2 2 2 1

February 4, 2020 OSU/CSE 541 40


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.

February 4, 2020 OSU/CSE 541 41


Multi-dimensional Integration
• In general, given k dimensions, we have
N= nk function evaluations:
 2
   
2
O  h2   O  n2   O   nk  k   O  N k 

   
• 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.

February 4, 2020 OSU/CSE 541 42


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

February 4, 2020 OSU/CSE 541 43


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?
h n 1

I h   f  a   2 f  a  ih   f  b  
2 i 1 

February 4, 2020 OSU/CSE 541 44


Adding More Intervals

• This is called the Recursive Trapezoid


Rule in the book.
• We have n 2n and hh/2.
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 
February 4, 2020 OSU/CSE 541 45
Higher-Order Polynomials

• 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

m Polynomial Formula Error


1 linear Trapezoid O(h 2 )
2 quadratic Simpson' s 1/3 O(h 4 )
3 cubic Simpson' s 3/8 O(h 4 )

February 4, 2020 OSU/CSE 541 46


Simpson Rule
Simpson’s 1/3 Rule

• If we use a 2nd order polynomial (need 3


points or 2 intervals):
 x  x2 
– Lagrange form.  x1  0 
 2 

  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  

February 4, 2020 OSU/CSE 541 48


Simpson’s 1/3 Rule

• Requiring equally-spaced intervals:


  x  x0  h  x  x0  2h   x  x0  x  x0  2h 
f  x0   f  x1 
x2
I  
x0
 h  2h   h  h 

 x  x0  x  x0  h  
f  x2   dx
 2h  h  

February 4, 2020 OSU/CSE 541 49


Simpson’s 1/3 Rule

• Integrate and simplify:


ba
I   f  x0   4 f  x1   f  x2 
h
h
12 3 2
10

Quadratic
8 Polynomial

0
3 5 7 9 11 13 15

February 4, 2020 OSU/CSE 541 50


Simpson’s 1/3 Rule

• If we use a = x0 and b = x2, and


x1 = (b+a)/2

f  x0   4 f  x1   f  x2 
I  b  a 
6
width
average height

February 4, 2020 OSU/CSE 541 51


Simpson’s 1/3 Rule

• Error for Simpson’s 1/3 rule

Et  
h 5 4 
f    
b  a  4 
5
f   5
90 2880
O(h )

ba
h
2

Integrates a cubic exactly: f 4 


   0

February 4, 2020 OSU/CSE 541 52


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.

February 4, 2020 OSU/CSE 541 53


Composite Simpson’s 1/3 Rule

• Example: 9 points, 4 intervals


25

20

15

10

0
0 0.5 1 1.5 2 2.5 3 3.5 4 4.5

-5

February 4, 2020 OSU/CSE 541 54


Composite Simpson’s 1/3 Rule

• As in composite trapezoid, break integral up


into n/2 sub-integrals:
I   f  x dx   f  x dx  ...   f  x dx
x 2 x 4 x n

x 0 x 2 x n 2

• Substitute Simpson’s 1/3 rule for each


integral and collect terms.
n 1 n2
f  x0   4  f x  2  f x  f x 
i j n

I  b  a 
i 1,3,5 j  2,4,6

3n
n+1 data points, an odd number
February 4, 2020 OSU/CSE 541 55
Composite Simpson’s 1/3 Rule

• Odd coefficients receive a weight of 4, even


receive a weight of 2.
• Doesn’t seem very fair, does it?

4 1 coefficients on
1
4 1 i=n numerator
1 1
1 4
i=0

February 4, 2020 OSU/CSE 541 56


Error Estimate
• The error can be estimated by:
nh5 ( 4 ) b  a h 4 ( 4 ) 4
Ea  f  f O(h )
180 180
• If n is doubled, h  h/2 and Ea  Ea/16

f ( 4 ) is the average 4th derivative

February 4, 2020 OSU/CSE 541 57


Example
 x2
• Integrate f ( x)  e from a = 0 to b = 2.
• Use Simpson’s 1/3 rule:
ba ab
h 1 x0  a  0 x1  1 x2  b  2
2 2
1
dx  h  f  x0   4 f ( x1 )  f  x2  
2
I  e  x2
0 3
1
  f  0   4 f (1)  f  2  
3
1 0
 (e  4e 1  e 4 )  0.82994
3
February 4, 2020 OSU/CSE 541 58
Example

• Error estimate: Et  
h 5 4 
f  
90

• Where h = b - a and a <  < b


• Don’t know 
– use average value
5
1  4 1 5f  4
 x0   f  4
 x1   f  4
 x2 
Et  Ea   f 
90 90 3

February 4, 2020 OSU/CSE 541 59


Another Example

• Let’s look at the polynomial again:


f ( x)  0.2  25x  200 x 2  675x 3  900 x 4  400 x5
– From a = 0 to b = 0.8
ba ab
h  0.4 x0  a  0 x1   0.4 x2  b  0.8
2 2

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
February 4, 2020 OSU/CSE 541 60
Error

• Actual Error: (using the known exact value)


E  1.64053334 - 1.36746667  0.27306666 16%

• Estimate error: (if the exact value is not


available)
h 5 4 
Et   f  
90
• Where a <  < b.

February 4, 2020 OSU/CSE 541 61


Error

• Compute the fourth-derivative


f ( 4)
( x )  21600  48000 x

0.45 0.45
Et  Ea  f 4 
x1    f 4 
0.4  0.27306667
90 90

middle point

• Matches actual error pretty well.


February 4, 2020 OSU/CSE 541 62
Example Continued
• If we use 4 segments instead of 1: h
ba
 0.2
– x = [0.0 0.2 0.4 0.6 0.8] n
f (0)  0.2 f (0.2)  1.288 f (0.4)  2.456
f (0.6)  3.464 f (0.8)  0.232
n 1 n 2
f  x0   4  f x   2  f x   f x 
i j n

I  b  a  i 1, 3, 5 j 2, 4,6

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
February 4, 2020 OSU/CSE 541 63
Error

• Actual Error: (using the known exact value)


E  1.64053334 - 1.6234667  0.01706667 1%

• Estimate error: (if the exact value is not


available)
0.25 4  0.25 4 
Et  Ea   f x2    f 0.4  0.0085
90 90
middle point

February 4, 2020 OSU/CSE 541 64


Error

• Actual is twice the estimated, why?


• Recall:
f ( 4)
( x )  21600  48000 x

max
x 0,0.8 
 f ( 4)
( x)  f ( 4)
(0)  21600

f ( 4)
(0.4)  2400

February 4, 2020 OSU/CSE 541 65


Error

• Rather than estimate, we can bound the


absolute value of the error:
0.25  4 0.25  4
Ea   f    f  0   0.0768
90 90

• Five times the actual, but provides a safer


error metric.

February 4, 2020 OSU/CSE 541 66


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.

February 4, 2020 OSU/CSE 541 67


Simpson’s 3/8 Rule
• Simpson’s 3/8 rule uses a third order polynomial
– need 3 intervals (4 data points)

f ( x)  p3 ( x)  a0  a1 x  a2 x 2  a3 x 3

I   f  x dx   p3  x dx
x3 x3

x0 x0

February 4, 2020 OSU/CSE 541 68


Simpson’s 3/8 Rule
• Determine a’s with Lagrange polynomial
• For evenly spaced points

I  h f  x0   3 x1   3 f  x2   f  x3 
3
8

ba
h
3

February 4, 2020 OSU/CSE 541 69


Error

• Same order as 1/3 Rule.


– More function evaluations.
– Interval width, h, is smaller.
3 5  4
Et   h f   O(h 4 )
80

• Integrates a cubic exactly:


 f 4     0

February 4, 2020 OSU/CSE 541 70


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
February 4, 2020 OSU/CSE 541 71
Mixing Techniques
• 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

February 4, 2020 OSU/CSE 541 72


Newton-Cotes Formulas

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

February 4, 2020 OSU/CSE 541 73


Adaptive Simpson’s Scheme

• Recall Simpson’s 1/3 Rule:


I
h
 f x0   4 f x1   f x2 
3

• Where initially, we have a=x0 and b=x2.


• Subdividing the integral into two:
h
I  f  a   4 f  x1   2 f  x2   4 f  x3   f  b  
6

February 4, 2020 OSU/CSE 541 74


Adaptive Simpson’s Scheme

• We want to keep subdividing, until we


reach a desired error tolerance, .
• Mathematically:
h 
b

 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 

February 4, 2020 OSU/CSE 541 75


Adaptive Simpson’s Scheme
• This will be satisfied if:
h  
c

 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.
February 4, 2020 OSU/CSE 541 76
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?

February 4, 2020 OSU/CSE 541 77


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.

February 4, 2020 OSU/CSE 541 78


Adaptive Simpson’s Scheme

• How do we know whether to continue to


subdivide or terminate?
b
I   f  x  dx  S  a, b   E  a, b  , where
a

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 

February 4, 2020 OSU/CSE 541 79


Adaptive Simpson’s Scheme

• The first iteration can then be defined as:


I  S    E   , where
1 1

S    S  a, b  , E    E  a , b 
1 1

• Subsequent subdivision can be defined as:


S  2
 S  a, c   S  c, b 

February 4, 2020 OSU/CSE 541 80


Adaptive Simpson’s Scheme

• 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

February 4, 2020 OSU/CSE 541 81


Adaptive Simpson’s Scheme

• Finally, using the identity:


1 1  2  2
I S E S E
• We have:
 2 1 1  2  2
S S  E E  15E
• Plugging into our definition:

I S  2
E  2
S  2

15

1  2
S S 1

February 4, 2020 OSU/CSE 541 82
Adaptive Simpson’s Scheme

• Our error criteria is thus:


 2
I S 
15

1  2
S S 1
 
• Simplifying leads to the termination

S   15
formula:  2 1
S

February 4, 2020 OSU/CSE 541 83


Adaptive Simpson’s Scheme

• What happens graphically:

February 4, 2020 OSU/CSE 541 84


S2  S1  15  subdivide

February 4, 2020 OSU/CSE 541 85


February 4, 2020 OSU/CSE 541 86

S2  S1  15  subdivide
2

February 4, 2020 OSU/CSE 541 87



S2  S1  15  done
4
1
I  S2   S2  S1 
15

February 4, 2020 OSU/CSE 541 88



S2  S1  15  subdivide
4

February 4, 2020 OSU/CSE 541 89


1
I  S2   S2  S1 
15

February 4, 2020 OSU/CSE 541 90


1
I  S2   S2  S1 
15

February 4, 2020 OSU/CSE 541 91


Iright =Ileft + Iright

I=Ileft + Iright

February 4, 2020 OSU/CSE 541 92


February 4, 2020 OSU/CSE 541 93
February 4, 2020 OSU/CSE 541 94
Adaptive Simpson’s Scheme

• We gradually capture the difficult spots.

February 4, 2020 OSU/CSE 541 95


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;

February 4, 2020 OSU/CSE 541 96


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;
}

February 4, 2020 OSU/CSE 541 97


Guassian Quadrature
• 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: I  f ( x)dx   c f ( x )
1 n


1
i 1
i i

ci : weighting factors
xi : sampling points selected optimally New!!

February 4, 2020 OSU/CSE 541 98


Guassian Quadrature
• 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]:
b
a
f (t )dt

• We have for x[-1,1]:


(b  a) x  b  a 2t  b  a
t x
2 ba
February 4, 2020 OSU/CSE 541 99
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

February 4, 2020 OSU/CSE 541 100


Guassian Quadrature
• Basic form of Gaussian quadrature:
n
I   f ( x)dx   ci f ( xi )
1

1
i 1

• For n=2, we have:


I  c1 f x1   c2 f x2 
• This leads to 4 unknowns: c1, c2, x1, and x2
– two unknown weights (c1, c2)
– two unknown sampling points (x1, x2)
February 4, 2020 OSU/CSE 541 101
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!!!

February 4, 2020 OSU/CSE 541 102


Guassian Quadrature
• 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

February 4, 2020 OSU/CSE 541 103


Guassian Quadrature
• Recalling the formula: I  c1 f x1   c2 f x2 
– Constant
 c1 f  x1   c2 f  x2   c1  c2
1

• 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 2
   
1
1     
2 2 2
• f(x)=x2
x dx c1 f x1 c 2 f x 2 c x
1 1 c x
2 2
3
– Cubic
x 3 dx  0  c1 f  x1   c2 f  x2   c1 x1  c2 x2
1

3 3
• f(x)=x3 1

February 4, 2020 OSU/CSE 541 104


Guassian Quadrature

• We can now solve for our unknowns:


– Note, this is not an easy problem and will not be
covered in this class.
c1  c2  1
1
x1    0.577
3
1
x2   0.577
3

February 4, 2020 OSU/CSE 541 105


Guassian Quadrature

• This yields the two point Gauss-Legendre


formula
 1   1 
I  f   f  
 3  3

February 4, 2020 OSU/CSE 541 106


Guassian Quadrature

• This is exact for all polynomials up to and


including degree 3 (cubics).
1 1 1 1 1

  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

February 4, 2020 OSU/CSE 541 107


Guassian Quadrature
1
1
f ( x)dx  f (0.577)  f (0.577)
f(x)
f(-0.577)
f(0.577)

-0.577 0.577
x
-1 1
February 4, 2020 OSU/CSE 541 108
Example
• Integrate f(x) from a = 0 to b = 0.8
f ( x)  0.2  25x  200 x 2  675x 3  900 x 4  400 x5
• 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

0.8 (0.8  0) 1  (0.8  0)t  0.8  0 


I  f ( x )dx   f dt
0 2 1
 2 
 0.4  f 0.4t  0.4 dt
1

1
February 4, 2020 OSU/CSE 541 109
Example

• Solving
I  0.4  f 0.4t  0.4 dt
1

1

1 0.2  25(0.4t  0.4)  200(0.4t  0.4)2 


 0.4   5
dt
 675(0.4t  0.4)  900(0.4t  0.4)  400(0.4t  0.4) 
1 3 4

• And substituting for the 2-point formula:


I  0.4 f t dt
1

1
t  1 / 3
I  0.51674055  1.30583723  1.82257778
Exact integral is 1.64053334
February 4, 2020 OSU/CSE 541 110
Higher-order Gaussian Quadrature

• Recall the basic form:


n
I   f ( x)dx   ci f ( xi )
1

1
i 1

• Let’s look at n=3.


I  c1 f x1   c2 f x2   c3 f x3 
• 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)

February 4, 2020 OSU/CSE 541 111


Use 6 equations - constant, linear, quadratic, cubic,
4th order and 5th order to find those unknowns

 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

February 4, 2020 OSU/CSE 541 112


Higher-order Gaussian Quadrature

• 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

• Produces the three point Gauss-Legendre formula


I  c1 f x1   c2 f x2   c3 f x3 
– Exact for polynomials up to and including degree 5
(because using 5th degree polynomial)

February 4, 2020 OSU/CSE 541 113


Higher-order Gaussian Quadrature
1 5 8 5
1 f ( x)dx  9 f (0.775)  9 f (0.0)  9 f (0.775)
f(-0.775)
f(0)
f(0.775)

x
-0.775 0.775
February 4, 2020 -1 OSU/CSE 541 1 114
Example

f ( x)  0.2  25x  200 x 2  675x 3  900 x 4  400 x5


Integrate from a = 0 to b = 0.8
Transform from [0, 0.8] to [-1, 1]
f x dx
0.8
I  replace -0.4 with +0.4
0

10.2  25(0.4t  0.4)  200(0.4t  0.4)2 


  5
dt
 675(0.4t  0.4)  900(0.4t  0.4)  400(0.4t  0.4) 
1 3 4

February 4, 2020 OSU/CSE 541 115


Example

• Using the 3-point Gauss-Legendre formula:

 3 8  3
 f 0 
5 5
I 
f   f  
9 
5 9 9 5 
  

Substitute into the transform equation and get

I  0.281301290  0.873244444  0.485987599


 1.64053334
Exact integral is 1.64053334
February 4, 2020 OSU/CSE 541 116
Gaussian Quadrature

Can develop higher order Gauss-Legendre forms


using
I  c1 f x1   c2 f x2   ...  cn f xn 

Values for c’s and x’s are tabulated


Use the same transformation to map interval onto
[-1, 1]

February 4, 2020 OSU/CSE 541 117


I   f ( x)dx  c1 f x1   c2 f x2   ...  cn f xn 
1

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

 0.5773502692  0.7745966692  0.8611363116  0.9061798459  0.932469514


0.5773502692 0.0000000000  0.3399810436  0.5384693101  0.661209386
xi 0.7745966692 0.3399810436 0.0000000000  0.238619186
0.8611363116 0.5384693101 0.238619186
0.9061798459 0.661209386
0.932469514
February 4, 2020 OSU/CSE 541 118
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
February 4, 2020 OSU/CSE 541 119
Gaussian Quadrature

• Problems:
– If we add more data points, like doubling the
number of sample points.

February 4, 2020 OSU/CSE 541 120

You might also like