You are on page 1of 24

Numerical Methods

(CENG-2084)
Chapter 6
Numerical Differentiation and
Integration

Mastewal S 1 5/22/2020
Numerical Differentiation and Integration

• Standing in the heart of calculus are the mathematical concepts


of differentiation and integration:

y f ( xi  x)  f ( xi )

x x
dy f ( xi  x)  f ( xi )
 x lim 0
dx x
b
I   f ( x)dx
a

by Mastewal S. Chapter 6 2
The graphical definition of a derivative: as Δx approaches
zero in going from (a) to (c), the difference approximation
becomes a derivative.

by Mastewal S. Chapter 6 3
b
I   f ( x)dx
a

Graphical representation of the integral of 𝑓(𝑥) between the limits 𝑥 = 𝑎 𝑡𝑜 𝑏.


The integral is equivalent to the area under the curve
by Mastewal S. Chapter 6 4
Non-computer Methods for Differentiation
and Integration
• The function to be differentiated or integrated
will typically be in one of the following three
forms:
– A simple continuous function such as polynomial,
an exponential, or a trigonometric function.
– A complicated continuous function that is difficult
or impossible to differentiate or integrate directly.
– A tabulated function where values of x and f(x) are
given at a number of discrete points, as is often the
case with experimental or field data.

by Mastewal S. Chapter 6 5
Equal-area differentiation
(a) Centered finite divided differences are used to estimate the derivative for
each interval between the data points.
(b) The derivative estimates are plotted as a bar graph. A smooth curve is
superimposed on this plot to approximate the area under the bar graph. This
is accomplished by drawing the curve so that equal positive and negative
areas are balanced.
𝑑𝑦
(c) Values of can then be read off the smooth curve.
by Mastewal S. 𝑑𝑥 Chapter 6 6
Equal-area differentiation.
(a) Centered finite divided differences
are used to estimate the derivative
for each interval between the data
points.
(b) The derivative estimates are
plotted as a bar graph. A
smooth curve is superimposed
on this plot to approximate the
area under the bar graph. This
is accomplished by drawing the
curve so that equal positive and
negative areas are balanced.
(c) Values of dy/dx can then be
read off the smooth curve.

by Mastewal S. Chapter 6 7
High-Accuracy Differentiation Formulas
• High-accuracy divided-difference formulas can be generated by including additional
terms from the Taylor series expansion.
f ( xi ) 2
f ( xi 1 )  f ( xi )  f ( xi )h  h 
2
f ( xi 1 )  f ( xi ) f ( xi )
f ( xi )   h 
h 2

f ' ( xi 1 )  f ' ( xi ) f ( xi  2 )  2 f ( xi 1 )  f ( xi )
f ( xi )    O ( h)
h h2
f ( xi 1 )  f ( xi ) f ( xi  2 )  2 f ( xi 1 )  f ( xi )
f ( xi )   2
h  O ( h 2
)
h 2h
 f ( xi  2 )  4 f ( xi 1 )  3 f ( xi )
f ( xi )   O( h2 )
2h

• Inclusion of the 2nd derivative term has improved the accuracy to O(h2).
• Similar improved versions can be developed for the backward and centered formulas
by Mastewal S. Chapter 6 8
Forward finite-divided-difference formulas

First Derivative Error


f ( xi 1 )  f ( xi )

f ( xi )  O(h)
h
 f ( xi  2 )  4 f ( xi 1 )  3 f ( xi )
f ( xi )  O(h2)
2h
Second Derivative Error
f ( xi  2 )  2 f ( xi 1 )  f ( xi )
f " ( xi )  O(h)
h2
 f ( xi 3 )  4 f ( xi  2 )  5 f ( xi 1 )  2 f ( xi )
f " ( xi )  O(h2)
h2

by Mastewal S. Chapter 6 9
Backward finite-divided-difference formulas

First Derivative Error


f ( xi )  f ( xi 1 )

f ( xi )  O(h)
h
3 f ( xi )  4 f ( xi 1 )  f ( xi 2 )
f ( xi )  O(h2)
2h
Second Derivative Error
f ( xi )  2 f ( xi 1 )  f ( xi  2 )
f " ( xi )  O(h)
h2
2 f ( xi )  5 f ( xi 1 )  4 f ( xi 2 )  f ( xi 3 )
f " ( xi )  O(h2)
h2

by Mastewal S. Chapter 6 10
Centered finite-divided-difference formulas
First Derivative Error
f ( xi 1 )  f ( xi 1 )

f ( xi )  O(h2)
2h
 f ( xi  2 )  8 f ( xi 1 )  8 f ( xi 1 )  f ( xi 2 )
f ( xi )  O(h4)
12h
Second Derivative Error
f ( xi 1 )  2 f ( xi )  f ( xi 1 )
f " ( xi )  O(h2)
h2
 f ( xi  2 )  16 f ( xi 1 )  30 f ( xi )  16 f ( xi 1 )  f ( xi 2 )
f " ( xi )  O(h4)
12h 2

by Mastewal S. Chapter 6 11
Derivation of the centered formula for f’’(xi)

f ( xi ) 2
f ( xi 1 )  f ( xi )  f ( xi )h  h 
2
2( f ( xi 1 )  f ( xi )  f ( xi )h)
f ( xi ) 
h2
f ( xi 1 )  f ( xi 1 )
2( f ( xi 1 )  f ( xi )  h)
 2h
h2
2 f ( xi 1 )  2 f ( xi )  f ( xi 1 )  f ( xi 1 )

h2
f ( xi 1 )  2 f ( xi )  f ( xi 1 )
f ( xi ) 
h2
by Mastewal S. Chapter 6 12
Example
f(x) = -0.1x4 – 0.15x3 – 0.5x2 – 0.25x + 1.2
f’(x) = -0.4x3 – 0.45x2 – x – 0.25

At x = 0.5 True value for First Derivative = -0.9125


Using finite divided differences and a step size of h = 0.25 we obtain:

x f(x) Forward Backward


i-2 0 1.2 O(h) O(h)

i-1 0.25 1.1035 Estimate -1.155 -0.714

i 0.50 0.925 εt (%) 26.5 21.7


i+1 0.75 0.6363
i+2 1 0.2

Forward difference of accuracy O(h2) is computed as:


0.2  4(0.6363)  3(0.925)
f (0.5)   0.8593  t  5.82%
2(0.25)
Backward difference of accuracy O(h2) is computed as:
3(0.925)  4(1.1035)  1.2
f (0.5)   0.8781  t  3.77%
by Mastewal S. 2(0.25) Chapter 6
13
Newton-Cotes Integration Formulas
• The Newton-Cotes formulas are the most common
numerical integration schemes.

• They are based on the strategy of replacing a


complicated function or tabulated data with an
approximating function that is easy to integrate:
b b
I   f ( x)dx   f n ( x)dx
a a

f n ( x)  a0  a1 x    an1 x n 1  an x n

by Mastewal S. Chapter 6 14
The approximation of an integral by the area under
(a) a single straight line and
(b) a single parabola.

by Mastewal S. Chapter 6 15
The approximation of an integral by the area under
three straight-line segments.

by Mastewal S. Chapter 6 16
The Trapezoidal Rule
• The Trapezoidal rule is the first of the Newton-Cotes
closed integration formulas, corresponding to the
case where the polynomial is first order:
b b
I   f ( x)dx   f1 ( x)dx
a a
• The area under this first order polynomial is an
estimate of the integral of f(x) between the limits of a
and b:
f (a )  f (b)
I  (b  a ) Trapezoidal rule
2

by Mastewal S. Chapter 6 17
Graphical depiction of the trapezoidal rule

by Mastewal S. Chapter 6 18
Error of the Trapezoidal Rule/

• When we employ the integral under a straight line


segment to approximate the integral under a curve,
error may be substantial:
1
Et   
f (x )(b  a ) 3

12
where x lies somewhere in the interval from a to b.

by Mastewal S. Chapter 6 19
The Multiple Application Trapezoidal Rule
• One way to improve the accuracy of the trapezoidal rule is to
divide the integration interval from a to b into a number of
segments and apply the method to each segment.
• The areas of individual segments can then be added to yield
the integral for the entire interval.
ba
h a  x0 b  xn
n
x1 x2 xn

I  f ( x)dx   f ( x)dx     f ( x)dx


x0 x1 xn1

Substituting the trapezoidal rule for each integral yields:


f ( x0 )  f ( x1 ) f ( x1 )  f ( x2 ) f ( xn 1 )  f ( xn )
I h h  h
2 2 2

by Mastewal S. Chapter 6 20
The general format and nomenclature for multiple-application integrals.

by Mastewal S. Chapter 6 21
Simpson’s Rules
• More accurate estimate of an integral is
obtained if a high-order polynomial is used to
connect the points. The formulas that result
from taking the integrals under such
polynomials are called Simpson’s rules.

Simpson’s 1/3 Rule/


• Results when a second-order interpolating
polynomial is used.

by Mastewal S. Chapter 6 22
(a) Graphical depiction of Simpson’s 1/3 rule: It consists of taking the area
under a parabola connecting three points.
(b) Graphical depiction of Simpson’s 3/8 rule: It consists of taking the area
under a cubic equation connecting four points.

by Mastewal S. Chapter 6 23
b b
I   f ( x)dx   f 2 ( x)dx
a a

a  x0 b  x2
 ( x  x1 )( x  x2 ) ( x  x0 )( x  x2 ) ( x  x0 )( x  x1 ) 
x2

I   f ( x0 )  f ( x1 )  f ( x2 )dx
x0 
( x0  x1 )( x0  x 2 ) ( x1  x0 )( x1  x 2 ) ( x 2  x0 )( x 2  x1 ) 

ba
I
h
 f ( x0 )  4 f ( x1 )  f ( x2 ) h
3 2

Simpson’s 1/3 Rule

by Mastewal S. Chapter 6 24

You might also like