16
Numerical Integration Numerical Methods with MATLAB”, Recktenwald, Chapter 11 and Numerical Methods for Engineers”, Chapra and Canale, 5 th Ed., Part Six, Chapters 21 and 22 and Applied Numerical Methods with MATLAB”, Chapra, 2 nd Ed., Part Five, Chapters 17 and 18 PGE 310: Formulation and Solution in Geosystems Engineering Dr. Balhoff 1 Numerical Integration Definition: Area underneath the curve Basic Idea: Approximate continuous function with discrete points to approximate integral 2

Ch11 Numerical Integration

  • Upload
    lythuy

  • View
    255

  • Download
    1

Embed Size (px)

Citation preview

10/19/2011

1

Numerical Integration

“Numerical Methods with MATLAB”, Recktenwald, Chapter 11and

“Numerical Methods for Engineers”, Chapra and Canale, 5th Ed., Part Six, Chapters 21 and 22 and

“Applied Numerical Methods with MATLAB”, Chapra, 2nd Ed., Part Five, Chapters 17 and 18

PGE 310: Formulation and Solution in Geosystems Engineering

Dr. Balhoff

1

Numerical Integration

Definition: Area underneath the curve

Basic Idea: Approximate continuous function with discrete points to approximate integral

2

10/19/2011

2

Methods for Numerical Integration

Curve-Fitting Fit a curve to the discrete data Analytically integrate curve

Newton-Coates Complicated function or tabulated data Replace with approximating function that is easy to integrate Single function OR piecewise polynomials can be used Trapezoidal, Simpson’s rules

Other methods where the function is given Gauss quadrature Integration

3

Newton-Coates Integration Examples

Integration by (a) single straight line and (b) parabola

4

10/19/2011

3

Trapezoidal Rule – Simplest Newton-Coates

5

Integral is the Area under the Curve

( ) ( )( )

2

f a f bI b a

1 ( ) 2 ( )H f a H f b Width b a

6

10/19/2011

4

Trapezoidal Rule

First of the Newton-Coates formulas; corresponds to 1st order polynomial

Recall from “INTERPOLATION” that a straight line can be represented:

Area under line is an estimate of the integral b/w the limits “a” and “b”

Result of the integration is called the trapezoidal rule

1( ) ( )b b

a a

I f x dx f x dx

( ) ( )( )

2

f a f bI b a

7

xab

afbfafxf

)()(

)()(1

dxxab

afbfafI

b

a

])()(

)([

Error of the Trapezoidal Rule

Straight line segment to approximate integral results in error (which may be substantial)

)(12

)(

))((12

1

3

3

fab

E

or

abfE

t

t

ConstantSecond derivation of the function at a

point in between a and b

a < < b

10/19/2011

5

What does this error mean? )(fkEt

x

yf(x) is a line (1st order)

0)( f

Zero for all : a < < b

Error of using Trapezoidal method = 0It is exact.

x

yf(x) is 2nd order

antconstf )(Constant for all : a < < b

Error of using Trapezoidal method is a constant value.

x

yf(x) is a 3rd order or higher

f ’’( is not constant.f ’’( is a function of 1st or higher order.The value of f ’’( changes for different functions and different . ( a < < b)

Error of using Trapezoidal method for 3rd or higher order functions changes from case to case.

Multiple Application Trapezoidal Rule

Improve accuracy by using multiple segments

n+1 equally spaced data, so n segments of equal width

The total integral can be represented by:

Substituting the trapezoidal rule yields

Grouping terms:

3 12

1 2

1 2( ) ( ) ( )n

n

x xx

n

x x x

I f x dx f x dx f x dx

1 1

21 1

2

( ) 2 ( ) ( )( ) 2 ( ) ( )

2 2

n

i n ni

i ni

WidthAverage Height

f x f x f xh

I b a f x f x f xn

b ah

n

2 3 11 2 ( ) ( ) ( ) ( )( ) ( )

2 2 2n nf x f x f x f xf x f x

I h h h

10

10/19/2011

6

Simpson’s Rules

Higher-order polynomials another way to get more accurate estimate

Three points make a parabola, 4 points make a cubic

11

Simpson’s 1/3 Rule

Second-order Lagrange polynomial, in the integral becomes

After the integration and algebraic manipulation:

3

1

2 3 1 3 1 21 2 3

1 2 1 3 2 1 3 2 3 1 3 2

( )x

x

x x x x x x x x x x x xI f x f x f x dx

x x x x x x x x x x x x

2( ) ( )b b

a a

I f x dx f x dx

1 2 31 2 3

( ) 4 ( ) ( )( ) 4 ( ) ( )

3 6width

Average Height

f x f x f xhI f x f x f x b a

12

10/19/2011

7

Error estimate of Simpson’s 1/3 rule

Single segment application of Simpson’s 1/3 rule has truncation error:

Simpson’s rule is more accurate than the trapezoidal rule

It’s actually more accurate than expected: Expect proportional to third derivative, but instead is proportional to

the 4th derivative

Yields an exact result for cubic polynomials even though its derived from a parabola!

55 (4) (4)1 ( )

( ) ( )90 2880t

b aE h f f

Constant4th derivation of the function at a point in

between a and b

a < < b

Error in Simpson’s 1/3 rule )()4( fmEt

x

y f(x) is from 1st order to 3rd order

0)()4( f

Zero for all : a < < b

Error of using Simpson’s 1/3 method = 0It is exact.

x

yf(x) is 4th order

antconstf )()4( Constant for all : a < < b

Error of using Simpson’s 1/3 method is a constant value.

x

yf(x) is a 5th order or higher

f ’’( is not constant.f ’’( is a function of 1st or higher order.The value of f ’’( changes for different functions and different . ( a < < b)

Error of using Simpson’s 1/3 method for 5th or higher order functions changes from case to case.

10/19/2011

8

Multiple Application 1/3 Rule

n segments of equal width

Total integral can be represented by:

Substituting the Simpson’s 1/3 rule yields

Grouping terms

3 12

1 2

1 2( ) ( ) ( )n

n

x xx

n

x x x

I f x dx f x dx f x dx

1

1 1 12,4,6 3,5,7

1 12,4,6 3,5,7

( ) 4 ( ) 2 ( ) ( )

( ) 4 ( ) 2 ( ) ( )3 3

n n

i j n n ni j

i j ni j

WidthAverage Height

f x f x f x f xh

I b a f x f x f x f xn

b ah

n

1 2 3 3 4 5 1 1( ) 4 ( ) ( ) ( ) 4 ( ) ( ) ( ) 4 ( ) ( )2 2 2

6 6 6n n nf x f x f x f x f x f x f x f x f x

I h h h

15

16

10/19/2011

9

Simpson’s 3/8 Rule

Integration and algebraic manipulation of the Lagrange Polynomials:

Error: Same order accuracy as Simpson’ 1/3 rule – so 1/3 rule is usually desired

Sometimes combine 1/3 and 3/8 rule when the segments are odd

1 2 3 41 2 3 4

( ) 3 ( ) 3 ( ) ( )3( ) 3 ( ) 3 ( )

8 8width

Average Height

f x f x f x f xhI f x f x f x f x b a

3( ) ( )b b

a a

I f x dx f x dx

55 (4) (4)3 ( )

( ) ( )80 6480t

b aE h f f

17

Integration with Unequal Segments

Until now all formulas have been based on equally spaced data

In practice, there are many situations where this does not hold

Trapezoid rule for example:

Program can easily be created to accommodate unequal sized segments

2 3 11 21 2

( ) ( ) ( ) ( )( ) ( )

2 2 2n n

n

f x f x f x f xf x f xI h h h

18

10/19/2011

10

Use of the Trapezoidal rule to determine the integral of unevenly spaced data. Notice how the shaded segments could be evaluated with Simpson’s rule to attain higher accuracy

19

Method Equation Error

Trapezoid

1/3 Simpson’s Rule

3/8 Simpson’s Rule

Comparison of Methods

1 2( )2

f x f xb a

1 12

2( )

2

n

i ni

f x f x f xb a

n

3"12

1abfEt

n

ia f

n

abE

13

3

"12

1 2 34( )

6

f x f x f xb a

1

1 12,4,6 3,5,7

4 2

( )3

n n

i j ni j

f x f x f x f x

b an

)4(5

2880f

abEt

n

ia f

n

abE

1

)4(

4

5

180

1 2 3 43 3( )

8

f x f x f x f xb a

5

(4)

6480t

b aE f

b

a

b

a

dxxfdxxfI )()( 1

20

10/19/2011

11

Higher Order Newton-Coates

* To keep consistent notation in the above table replace x0 with x1, x1 with x2, etc.

21

True Percent Relative Error

22

10/19/2011

12

Gauss Quadrature

Newton-Coates uses predetermined or fixed base points

Suppose we could evaluate the area under a straight line joining anytwo points on the curve We could balance the positive and negative errors if chosen wisely

Gauss Quadrature: class of techniques that implements this strategy Particular Formulas discussed here a Gauss Legendre

Trapezoidal Rule Gauss Quadrature

23

Method of Undetermined Coefficients: 2-Point Gauss-Legendre

• In this method, for whatever function it is the integral expressed as:

• This is another approach for calculating integrals. Not using before-mentioned methods such as Trapezoidal and Simpsons.

)()( 2211 xfcxfcI

Constant Coefficients

Value of the function at two indicative points within the interval

a <x1 & x2 < b

x

y

a bx1 x2

)()()( 2211 xfcxfcdxxfIb

a

• So, the question is what are these 4 unknowns (c1, c2, x1 & x2) such that:

10/19/2011

13

Solve it for a Special Case:

What special?

Want to have exact solution for any 3rd order function

Integral limits are -1 to +1

We solve for this special case and generalize it

33

2210)( xaxaxaaxg General 3rd order

function

1

1

33

2210

1

1)()( dxxaxaxaadxxgI

1

1

33

1

1

22

1

11

1

10 1 dxxadxxadxxadxa

So, if the Gauss-Quadrature formula (c1x1+c2x2) can calculate exact solution for these 4 components, it can find exact solution for the whole function.

Solve it for a Special Case:

If f(x)=1:

)()(1 2211

1

1xfcxfcdx

1 1

112 21 cc

If f(x)=x:

)()( 2211

1

1xfcxfcdxx

x1x2

22110 xcxc

If f(x)=x2:

)()( 2211

1

1

2 xfcxfcdxx

x12 x2

2

222

2113

2xcxc

If f(x)=x3:

)()( 2211

1

1

3 xfcxfcdxx

x13 x2

3

322

3110 xcxc

4 Equations and 4 unknowns:

When solved:

5773503.03

15773503.0

3

1

11

21

21

xx

cc

10/19/2011

14

Method of Undetermined Coefficients:2-Point Gauss-Legendre

• So, we found two points (x1=-0.5773503 &x2=+0.5773503) and two constants (c1=1 & c2=1)such that they can return Exact Integral Value forthese four functions.

x

y

+1x1 x2-1

x

y

+1

x1

x2

-1

x

y

+1x1 x2-1

x

y

+1

x1

x2

-1

)()()( 2211

1

1xfcxfcdxxf

)3

1(1)

3

1(1 ff

f(x)=1

f(x)=x

f(x)=x2

f(x)=x3

For these four functions:

Method of Undetermined Coefficients:2-Point Gauss-Legendre

• Any general 3rd order function composed of the four mentioned functions (go back totwo last slide).

• So, by using 2-Point Gauss-Legendre method we can find the Exact Solution for the integral ofany 3rd function:

)3

1(1)

3

1(1)()()( 2211

1

1

ffxfcxfcdxxfFor these any 3rd (or lower) order functions:

• For any higher order (4th order or higher) polynomial, or any non-polynomial function 2-PointGauss-Legendre method does not return the Exact Solution. It will have some errors and returnsApproximate Solution.

10/19/2011

15

Method of Undetermined Coefficients:Multiple-Point Gauss-Legendre

• We can use even more points

)(...)()()( 2211

1

1 nn xfcxfcxfcdxxf

In general form:

n-Point Gauss-Legendre formula:

Method Exact answer for functions: Error on the order of :

2-Point Gauss-Legendre up to 3rd order ~f(4)()3-Point Gauss-Legendre up to 5th order ~f(6)()4-Point Gauss-Legendre up to 7th order ~f(8)()…. …. ….n-Point Gauss-Legendre up to (2n-1)th order ~f(2n)()

What about the Integration Limits?

Integration limits: -1 to 1; change of variables can be made to translate the limits of integration from “a” to “b”

Introduce new variable xd related to original variable x in a linear fashion

Limits: x = a and x = b corresponds to xd=-1 and xd=1

Equations can be solved simultaneously for:

They can substituted into equation and differentiated to yield:

dxaax 10

22 10

aba

aba

)1(10 aaa )1(10 aab

d

d dxab

dxxabab

x22

10/19/2011

16

General Form of Using Gauss-Quadrature Method Using Higher-Point Formulas

Error:

~f 4()

~f 6()

~f 8()

~f 10()

~f 12()

1

1 10 )2

()()( dd

b

adx

abxaafdxxf

g(xd)

xd i

)(...)()()( 2211

1

1 dnndddd xgcxgcxgcdxxg

In summary

Numerical Integration necessary for discrete data, complicated functions, etc.

Newton-Coates (good for predetermined and/or equally spaced data) Trapezoid Rule (1st order accurate i.e. 2nd derivative error)

Simpson’s 1/3 (3rd order accurate i.e. 4th derivative error)

Simpson’s 3/8 (3rd order accurate i.e. 4th derivative error)

Gauss Quadrature/Gauss Legendre More accurate, but requires us to be able to evaluate function at specific points

2-point Legendre is 3rd order accurate (Truncation error is proportional to 4th derivative)

Higher-Point Formulas Available

32