Docsity

Numerical Integration and Differentiation - Lecture Notes | CGN 3421, Study notes of Civil Engineering

Toggle sidebar
bg1
CGN 3421 - Computer Methods Gurley
Numerical Methods Lecture 4 - Numerical Integration / Differentiation page 83 of 88
Numerical Methods Lecture 4 - Numerical Integration / Differentiation
Numerical Integration - approximating the area under a function
We will consider 3 methods: Trapezoidal rule, Simpson’s rule, Gauss Quadrature
The basic concept is to break the area up into smaller pieces with simple shapes fitted to approximate the func-
tion over short lengths. The area under these simple shapes are then added up to approximate the total.
Trapezoidal rule - piece-wise linear approximation to curve f(x)
• chop area into N smaller pieces (sample of two pieces on the right)
• connect areas with straight lines, creating trapezoids
• find the area under each trapezoid A=1/2*dx*(f(x)+f(x+dx))
• add up the trapezoids
Example, the figure at the bottom of the page chops a function into 4 trapezoids
over the range [a, b]. The trapeziodal rule adds up the area of each of the 4 trape-
zoids to represent the area under the fucntion f(x).
dx = (b-a)/N (a, b are limits, N is number of pieces we divide the area into)
A1= 1/2 * dx * (f(x1) + f(x2))
A2 = 1/2* dx * ( f(x2) + f(x3))
A3 = 1/2* dx * ( f(x3) + f(x4))
A4 = 1/2* dx * ( f(x4) + f(x5))
_____________________________________________
Area = 1/2 * dx * (f(x1)+2*f(x2)+2*f(x3)+2*f(x4)+f(x5))
The generalized equation expressing trapezoidal rule is then ==>
Error: The error (in blue) between the area of the trapezoid and the curve it represents builds up
Error reduction: We can reduce error if we can shorten the straight lines used to estimate curves. Increasing
the number (increase N) of trapezoids used over the same range [a,b] will reduce error in the area estimate.
A1
A2
x1 x2 x3
dx f(x)
A1=1/2*dx*(f(x1)+f(x2))
!"#$ !
"
---%& ' &!
()"'&
(
()
("#
)
∑'&
)!$
()$$≅
fx
x
ab
Numerically estimate area
under fx within [a, b]
A1 A2 A3 A4
dx
x1 x2 x3 x4 x5
Error
pf3
pf4
pf5

Partial preview of the text

Download Numerical Integration and Differentiation - Lecture Notes | CGN 3421 and more Study notes Civil Engineering in PDF only on Docsity!

Numerical Methods Lecture 4 - Numerical Integration / Differentiation

Numerical Integration - approximating the area under a function

We will consider 3 methods: Trapezoidal rule, Simpson’s rule, Gauss Quadrature

The basic concept is to break the area up into smaller pieces with simple shapes fitted to approximate the func-

tion over short lengths. The area under these simple shapes are then added up to approximate the total.

Trapezoidal rule - piece-wise linear approximation to curve f(x)

  • chop area into N smaller pieces (sample of two pieces on the right)
  • connect areas with straight lines, creating trapezoids
  • find the area under each trapezoid A=1/2dx(f(x)+f(x+dx))
  • add up the trapezoids

Example, the figure at the bottom of the page chops a function into 4 trapezoids

over the range [a, b]. The trapeziodal rule adds up the area of each of the 4 trape-

zoids to represent the area under the fucntion f(x).

dx = (b-a)/N (a, b are limits, N is number of pieces we divide the area into)

A1= 1/2 * dx * (f(x1) + f(x2))

A2 = 1/2* dx * ( f(x2) + f(x3))

A3 = 1/2* dx * ( f(x3) + f(x4))

A4 = 1/2* dx * ( f(x4) + f(x5))


Area = 1/2 * dx * (f(x1)+2f(x2)+2f(x3)+2*f(x4)+f(x5))

The generalized equation expressing trapezoidal rule is then ==>

Error: The error (in blue) between the area of the trapezoid and the curve it represents builds up

Error reduction: We can reduce error if we can shorten the straight lines used to estimate curves. Increasing

the number (increase N) of trapezoids used over the same range [a,b] will reduce error in the area estimate.

A

A

x1 (^) x2 x

dx f(x)

A1=1/2dx(f(x1)+f(x2))

!"#$

!

"

--- %& ' & !

( ) " ' & (

( )

( #"

)

' & ) $!

≅ $ $ ( )

fx

x

a

b

Numerically estimate area

under fx within [a, b]

A1 A2 A^

A

dx

x1 x2 x3 x4 x

Error

We can also get a little more sophisticated than straight line estimates

The error using trapezoidal rule comes from using a series of straight lines to represent a curve. To reduce

this error, we’ll now look at using a series of second order curves to represt the function.

Simpson’s Rule - piece-wise second order approximation of f(x).

  • chop area into N smaller pieces
  • connect every 3 points with second order curve fit, this gives us

N/2 smaller areas representing the curve in the range [a,b]

  • Find the area under each second order polynomial estimate.

A=dx/3[f(x1)+4f(x2)+f(x3)]

  • Add up the smaller areas to estimate area under the curve

Example, the figure at the bottom of the page chops a function into 8 pieces over the range [a, b]. Every

two adjacent pieces are joined to form a sub-area. The Simpson’s rule adds up the 4 sub-areas to represent

the area under the fucntion f(x).

A1 = dx/3 * (f(x1)+4f(x2)+f(x3))

A2 = dx/3 * ( f(x3)+4*f(x4)+f(x5))

A3 = dx/3 * ( f(x5)+4*f(x6)+f(x7))

A4 = dx/3 * ( f(x7)+4*f(x8)+f(x9))


Area = A1+A2+A3+A

=dx/3 * (f(x1)+4f(x2)+2f(x3)+4f(x4)+2f(x5)+4f(x6)+2f(x7)+4f(8)+f(x9))

Simpson’s rule is ==>

  • Reduce error by increasing N
  • This method requires N to be even, since we need 3 points per sub-area

x i-

x i

x i+

A=dx/3*[f(x i-

)+4*f(x i

)+f(x i+

)]

A

dx

!"#$

!

%

--- %& ' & !

( )! & ' & *

( )

* #" '('),

)

" & ' & +

( )

+ #% *++,

) ,!

' & ) $!

≅ $ $ $ ( )

fx

x

a b

Numerically estimate area

under fx within [a, b]

dx

x

x

x

x

x

A A2 A3 A

x

x

x

x

N = 8 segments

gives us 4 areas to add up

each area is a topped off with a

second order polynomial (red line)

dx

(2) (3)

To apply this change of variables, we re-write equation (1) now as:

(4)

Example: Integrate the following function over the range [ 0 , 0.8 ]

Re-writing Eq. 4 above gives the general solution:

(5)

where and (6)

For this example, and. Plugging these values into (6), then plugging into (5),

we get

Gauss Quadrature Area estimate = f(0.169) + f(0.631) = 0.516741+1.305837 = 1.

Exact Answer = 1.

Note, if we truncated the function being integrated after the term, then the area we estimate using (5)

and (6) will be exact, since Gauss quadrature is exact for up to third order polynomial equations.

Extension

Gauss quadrature can be extended to higher orders, involving more evaluation points, for example

$

-

"

120 &

%

3-- &

4

4-- &

0

( $ , $ , $ ) %&

-/

$

-

!

!

$ # - - # -/5 &

& !

,

%

,!

!

!

!

"

"

. ,!

. ,!

Numerical Differentiation

Read section 23.

Derivative represents the slope of a function f(x) at a particular value

slope is = change in f(x)/change in x

formally the definition of the derivative is:

Here we will approximate the limit by taking some small rather than mathematically letting it go to 0.

Forward difference method

Backward difference method

Central difference method

.

( ) &

∆ & →-

∆ & →-

x

x+dx

forward difference

find the derivative at x

.

( ) &

∆ & →-

x-dx x

backward difference

find the derivative at x

.

( ) &

∆ & →-

x-dx x

Central difference

x+dx

find the derivative at x

.

( ) &

∆ & →-