0% found this document useful (0 votes)
149 views19 pages

1D FDM

The document introduces the finite difference method for numerically approximating derivatives of functions. It discusses: 1) Taylor series expansions which provide the basis for finite difference approximations by truncating terms in the Taylor series. This leads to forward, backward, and central difference formulas of varying orders of accuracy. 2) Graphical interpretations of forward, backward, and central difference approximations as approximations of the tangent line using different chords. 3) Examples calculating the derivative of a sample function using different finite difference formulas and step sizes to demonstrate their accuracy. Higher-order formulas provide greater accuracy.

Uploaded by

Syed M Adnan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
149 views19 pages

1D FDM

The document introduces the finite difference method for numerically approximating derivatives of functions. It discusses: 1) Taylor series expansions which provide the basis for finite difference approximations by truncating terms in the Taylor series. This leads to forward, backward, and central difference formulas of varying orders of accuracy. 2) Graphical interpretations of forward, backward, and central difference approximations as approximations of the tangent line using different chords. 3) Examples calculating the derivative of a sample function using different finite difference formulas and step sizes to demonstrate their accuracy. Higher-order formulas provide greater accuracy.

Uploaded by

Syed M Adnan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 19

INTRODUCTION TO THE FINITE DIFFERENCE

METHOD
Basic Theory
Taylors Theorem
If a function f and its first n+1 derivatives are continuous in an
interval containing the points xi and xi+1, then the value of the
function f at the point xi+1 is given by:
f ( xi 1 ) f ( xi ) f '( xi ) ( xi 1 xi )

f ''( xi )
f '''( xi )
( xi 1 xi ) 2
( xi 1 xi )3
2!
3!

f ( n ) ( xi )
( xi 1 xi ) n Rn
n!

...

where the remainder Rn is defined as:


Rn

xi 1

xi

( xi 1 t ) n ( n 1)
f
(t ) dt
n!

If the remainder Rn is omitted in the above series expansion,


then the expression obtained is the Taylor polynomial
approximation to f(x). After some manipulation, the remainder
Rn can be written in the form:
Rn

where

f ( n 1) ( )
( xi 1 xi ) n 1
(n 1)!

xi xi 1 .

A Taylor series provides a means of predicting the value of a


function f at a point xi+1 in terms of the value of the function f
and its derivatives at a point xi. The more terms are included in
the series, the greater the accuracy of the approximation.

Consider the following cases:


First Approximation (zero order):
f ( x i 1 ) f ( xi )

This approximation is reasonable if the points xi+1 and xi are


close to each other, and is a correct estimate if the function f(x)
is constant.
Second Approximation (first order):
f ( xi 1 ) f ( xi ) f ' ( xi )( xi 1 xi )

This approximation includes a term which is the slope


multiplied by the distance between the points. The expression is
now of the form y = c + mx, and is a correct estimate if the
function f(x) is linear.
Third Approximation (second order):
f ( xi 1 ) f ( xi ) f ' ( xi )( xi 1 xi )

f ' ' ( xi )
( x i 1 x i ) 2
2!

This approximation now includes the effect of curvature due to


the second-order derivative. The expression is a correct estimate
if the function f(x) is quadratic.
Additional terms can be included as required. Writing
xi 1 xi h

the Taylor series can be written as


f ( xi 1 ) f ( xi ) h f '( xi ) h 2

f ''( xi )
f '''( xi )
f ( n ) ( xi )
h3
... h n
Rn
n!
2!
3!

with the remainder Rn given by


Rn h n 1

f ( n 1) ( )
(n 1)!

If we truncate the Taylor series after the n+1th term, we will


introduce a truncation error of order hn+1, or O(hn+1).
The notation O(hn+1) means that the truncation error is of the
order hn+1. That is, the error is proportional to the step size h
raised to the power n+1.
Although this approximation implies nothing regarding the
magnitude of the derivatives that multiply hn+1, it is useful in
judging the comparative error of numerical methods based on
Taylor series expansions. For example, if the error is O(h),
halving the step size h will halve the error. On the other hand, if
the error is O(h2), halving the step size h will quarter the error.
Errors
Two types of errors will occur in the numerical approximation
of a function by a Taylor series:
Truncation error: due to the truncation of the Taylor
series
Round-off error: due to the fact that all types of
computing machines have a finite precision.
Taylor series approximations are basically dominated by
truncation errors for large values of h, and by round-off errors
for small values of h.

Finite Differences
Simple difference formulas can be created by truncating the
Taylor series after the first derivative term. Examples are:
Forward Difference:
f ( xi 1 ) f ( xi ) h f '( xi ) O (h 2 )
f '( xi )

f ( xi 1 ) f ( xi )
O ( h)
h

Backward Difference:
f ( xi 1 ) f ( xi ) h f '( xi ) O ( h 2 )
f '( xi )

f ( xi ) f ( xi 1 )
O ( h)
h

Higher-order approximations can be created by retaining


additional terms of the Taylor series, as follows:
f ( xi 1 ) f ( xi ) h f '( xi ) h 2

f ''( xi )
O ( h3 )
2!

f ( xi 2 ) f ( xi ) 2h f '( xi ) (2h) 2

f ''( xi )
O(h3 )
2!

Multiplying the first equation above by 4 gives:


4 f ( xi 1 ) 4 f ( xi ) 4h f '( xi ) 2h 2 f ''( xi ) O ( h3 )

(1)

f ( xi 2 ) f ( xi ) 2h f '( xi ) 2h 2 f ''( xi ) O ( h3 )

(2)

Subtracting equation (2) from equation (1) gives


4 f ( xi 1 ) f ( xi 2 ) 3 f ( xi ) 2h f '( xi ) O ( h3 )

and rearranging produces the final result:


f '( xi )

f ( xi 2 ) 4 f ( xi 1 ) 3 f ( xi )
O(h 2 )
2h

The above expression is a higher-order forward difference


approximation. Proceeding in a similar fashion, the following
higher-order backward difference approximation can be
produced:
f '( xi )

3 f ( xi ) 4 f ( xi 1 ) f ( xi 2 )
O(h 2 )
2h

Another possibility is to generate what is known as a central


finite difference formula. To this end, we start from the
following two Taylor series:

f ( xi 1 ) f ( xi ) h f '( xi ) h 2

f ''( xi )
O ( h3 )
2!

f ( xi 1 ) f ( xi ) h f '( xi ) h 2

f ''( xi )
O ( h3 )
2!

Subtracting the second expression from the first gives:


f ( xi 1 ) f ( xi 1 ) 2h f '( xi ) O ( h3 )

and rearranging produces the final result:


f '( xi )

f ( xi 1 ) f ( xi 1 )
O(h 2 )
2h

Notice that the addition of the above two expressions including


the third-order derivative gives:
f ( xi 1 ) f ( xi 1 ) 2 f ( xi ) h 2 f ''( xi ) O ( h 4 )

and rearranging produces a central finite


approximation for the second-order derivative:
f ''( xi )

difference

f ( xi 1 ) 2 f ( xi ) f ( xi 1 )
O(h 2 )
h2

Forward, backward and central finite difference formulas for


higher-order derivatives can also be obtained by using the above
procedures (see tables).
Note: The appropriate use of forward, backward and central
finite difference formulas depends on the available data.

Graphical Interpretation
Given a function y = u(x), the derivative du/dx at a point x = xi is
given by:
u ( xi h) u ( xi )
du
lim h0

h
dx x xi

This represents the slope of the tangent to the curve at the point
x = xi.

T
Q

f (a + h)

f (a)

y = f (x)
P

a+h

Approximately, we can write:


u ( xi h) u ( xi )
du


h
dx x xi

where h is a small but finite increment. This forward difference


formula represents an approximation of the tangent at x = xi by
the chord AB.
Similarly, the backward difference formula:
u ( xi ) u ( xi h)
du


h
dx x xi

represents an approximation of the tangent at x = xi by the chord


AC.
The central difference formula:
u ( xi h) u ( xi h)
du


2h
dx x xi

can also be viewed as an approximation of the tangent at x = xi


by the chord BC.

Example 1
Estimate the first derivative of the function:
f ( x) 0.1x 4 0.15 x 3 0.5 x 2 0.25 x 12

at the point x = 0.5, using a step size h = 0.5.


Since
f '( x) 0.4 x 3 0.45 x 2 1.0 x 0.25

the correct value of the derivative at the point x = 0.5 is


f '(0.5) 0.9125 .
We start by using a forward difference formula:
f '( xi )

f ( xi 1 ) f ( xi )
xi 1 xi

In this case, xi = 0.5, xi+1 = 1.0 and xi+1


f '(0.5)

% error t

xi = 0.5, thus

f (1.0) f (0.5)
1.45
0.5

True Approx 0.9125 ( 1.45)

58.9%
True
0.9125

Now using a backward difference formula:


f '( xi )

f ( xi ) f ( xi 1 )
xi xi 1

with xi = 0.5, xi-1 = 0 and xi xi-1 = 0.5,


f '(0.5)

% error t

f (0.5) f (0.0)
0.55
0.5
0.9125 (0.55)
39.7%
0.9125

Using a central difference formula:


f '( xi )

f ( xi 1 ) f ( xi 1 )
xi 1 xi 1

with xi = 0.5, xi+1 = 1.0, xi-1 = 0 and xi+1 xi-1 = 2h = 1.0,


f '(0.5)

f (1.0) f (0.0)
1.0
1.0

% error t

0.9125 ( 1.0)
9.6%
0.9125

Let us now decrease the step size from h = 0.5 to h = 0.25:


Forward difference:
f '(0.5)

f (0.75) f (0.5)
1.155
0.25

% error t 26.5%

Backward difference:

f '(0.5)

f (0.5) f (0.25)
0.714
0.25

% error t 21.7%

Central difference:

f '(0.5)

f (0.75) f (0.25)
0.934
0.5

% error t 2.4%

Note: The backward and forward difference formulas used in


the above example only have an accuracy of O(h). So, halving
the step size h halved the error. On the other hand, the central
difference formula has an accuracy of O(h2). So, halving the
step size h has quartered the error since 0.52 = 0.25.

Example 2
Use high accuracy formulas to estimate the first derivative of the
function:
f ( x) 0.1x 4 0.15 x 3 0.5 x 2 0.25 x 12

at the point x = 0.5, using a step size h = 0.25.


High accuracy forward difference formula:
f '( xi )

f ( xi 2 ) 4 f ( xi 1 ) 3 f ( xi )
2h

Here xi = 0.5, xi+1 = 0.75, xi+2 = 1.0 and h = 0.25, thus


f '(0.5)

f (1.0) 4 f (0.75) 3 f (0.5)


0.860
0.5

t 5.82%

High accuracy backward difference formula:


f '( xi )

3 f ( xi ) 4 f ( xi 1 ) f ( xi 2 )
2h

Here xi = 0.5, xi-1 = 0.25, xi-2 = 0.0 and h = 0.25, thus


f '(0.5)

3 f (0.5) 4 f (0.25) f (0.0)


0.878
0.5
t 3.77%

High accuracy central difference formula:


f '( xi )

f ( xi 2 ) 8 f ( xi 1 ) 8 f ( xi 1 ) f ( xi 2 )
12h

Here xi = 0.5, xi+1 = 0.75, xi+2 = 1.0, xi-1 = 0.25, xi-2 = 0.0 and h =
0.25, thus
f '(0.5)

f (1.0) 8 f (0.75) 8 f (0.25) f (0.0)


0.9125
12 0.25

t 0

In this case, the high accuracy central difference formula


produced the exact solution. This is not unexpected since the
exact expression for the derivative is a cubic function and the
error of the above formula is O(h4), as can be seen in the table.
Example 3
Given a problem described by the following differential
equation:
f ''( x) f ( x) x 0

and the boundary conditions


f ( x) 0

at

x0

f ( x) 0

at

x 1

estimate the value of f(x) at the point x = 0.5.


Substituting the central difference formula:
f ''( xi )

f ( xi 1 ) 2 f ( xi ) f ( xi 1 )
h2

into the original differential equation applied at the point xi


gives:
f ( xi 1 ) 2 f ( xi ) f ( xi 1 )
f ( xi ) xi 0
h2

which can be rewritten in the form:


f ( xi 1 ) 2 f ( xi ) f ( xi 1 ) h 2 f ( xi ) h 2 xi 0

2 f ( xi ) f ( xi 1 ) f ( xi 1 ) h 2 xi

In this case, xi = 0.5, xi+1 = 1.0, xi-1 = 0.0 and h = 0.5, thus

0.5

2 f (0.5) f (1.0) f (0.0) 0.52 0.5

Since the boundary conditions of the problem state that f(1.0) =


0 and f(0.0) = 0, we have
f (0.5)

0.52 0.5
0.0714
0.52 2

The exact value is f(0.5) = 0.0697, thus the error is


% error t 2.4%

If we now adopt a step size h = 0.25 instead of h = 0.5, the


number of unknowns will increase. This means that the number
of equations will also have to be increased, as follows.
First, we apply the general equation

2 f ( xi ) f ( xi 1 ) f ( xi 1 ) h 2 xi

at the point xi = 0.25, giving:

0.25

2 f (0.25) f (0.5) f (0.0) 0.252 0.25

But since f(0.0) = 0, the above equation can be rearranged as


follows:
f (0.25)

f (0.5) 0.252 0.25


0.252 2

(1)

Now apply the general equation at the point xi = 0.5, giving:

0.25

2 f (0.5) f (0.75) f (0.25) 0.252 0.5

(2)

Finally, apply the general equation at the point xi = 0.75, giving:

0.25

2 f (0.75) f (1.0) f (0.5) 0.252 0.75

But since f(1.0) = 0, the above equation can be rearranged as


follows:
f (0.5) 0.252 0.75
f (0.75)
0.252 2

(3)

Substituting equations (1) and (3) above into equation (2), the
value of f(0.5) can be calculated as follows:

0.252 2 f (0.5)

f (0.5) 0.252 0.75 f (0.5) 0.252 0.25

0.252 0.5
2
2
0.25 2
0.25 2
f (0.5) 0.0701

with an error
% error t 0.6%

We can also calculate the following values:


f (0.25) 0.0442

(exact value: f (0.25) 0.0440)

f (0.75) 0.0604

(exact value: f (0.75) 0.0601)

Exact solution:
f ( x)

sin x
x
sin1

Example 4
Given the problem of mass balance within a chemical reactor,
described by the following advection-dispersion equation with
first-order chemical reaction:
D

d 2c
dc
U
kc 0
2
dx
dx

where:
c: concentration, moles/m3
U: velocity of the water flowing through the reactor, m/h
D: dispersion coefficient, m2/h
k: first-order decay coefficient, h-1
and the boundary conditions
c 1

at

x0

c0

at

xL

with L the reactor length, estimate the value of


concentration at the mid-point of the reactor.
Central finite difference approximations:
d 2c
c( xi 1 ) 2c ( xi ) c( xi 1 )

2
h2
dx x xi
c( xi 1 ) c( xi 1 )
dc


2h
dx x xi

Substituting the above approximations into the original


differential equation applied at the point xi gives:

the

c( xi 1 ) 2c ( xi ) c( xi 1 )
c( xi 1 ) c( xi 1 )
U
kc( xi ) 0
2
h
2h

which can be rearranged in the form:


D U
2D

D U
2 c( xi 1 ) 2 k c( xi ) 2 c( xi 1 ) 0
2h
h 2h
h

The above expression is a finite difference approximation of the


original differential equation.
Given the values:
D = 0.2; U = 1; k = 0.5; L = 0.5; and assuming that h = L/2 =
0.25, we have that
D U
0.2
1

3.2 2 1.2
2
2
h 2h 0.25 2 0.25

2D
2 0.2
k
0.5 6.4 0.5 6.9
2
h
0.252

D U
0.2
1

3.2 2 5.2
2
2
h
2h 0.25 2 0.25

Thus:
1.2c (0.5) 6.9c (0.25) 5.2c(0.0) 0
c(0.25)

5.2
0.753
6.9

If we now assume that h = L/4 = 0.125, we have that


D U
0.2
1

12.8 4 8.8
2
2
h 2h 0.125 2 0.125

2D
2 0.2
k
0.5 25.6 0.5 26.1
2
h
0.1252

D U
0.2
1

12.8 4 16.8
2
2
h
2h 0.125 2 0.125

General equation:
8.8c( xi 1 ) 26.1c ( xi ) 16.8c( xi 1 ) 0

Applying the above equation at xi = 0.125 gives:


8.8c(0.25) 26.1c (0.125) 16.8c (0.0) 0
c(0.125)

8.8c(0.25) 16.8
26.1

Applying the above equation at xi = 0. 25 gives:


8.8c(0.375) 26.1c (0.25) 16.8c (0.125) 0

Applying the above equation at xi = 0.375 gives:


8.8c(0.5) 26.1c (0.375) 16.8c (0.25) 0
c(0.375)

16.8c (0.25)
26.1

Therefore:
8.8

16.8c (0.25)
8.8c (0.25) 16.8
26.1c (0.25) 16.8
0
26.1
26.1

The final solution is c(0.25) = 0.732, c(0.125) = 0.890 and


c(0.375) = 0.471.

Forward Finite Difference Approximations


First Derivative
f '( xi )

f '( xi )

f ( xi 1 ) f ( xi )
O ( h)
h

f ( xi 2 ) 4 f ( xi 1 ) 3 f ( xi )
O(h 2 )
2h

Second Derivative
f ''( xi )

f ''( xi )

f ( xi 2 ) 2 f ( xi 1 ) f ( xi )
O ( h)
h2

f ( xi 3 ) 4 f ( xi 2 ) 5 f ( xi 1 ) 2 f ( xi )
O(h 2 )
2
h

Third Derivative
f '''( xi )

f '''( xi )

f ( xi 3 ) 3 f ( xi 2 ) 3 f ( xi 1 ) f ( xi )
O ( h)
h3

3 f ( xi 4 ) 14 f ( xi 3 ) 24 f ( xi 2 ) 18 f ( xi 1 ) 5 f ( xi )
O(h2 )
3
2h

Fourth Derivative
f ''''( xi )

f '''( xi )

f ( xi 4 ) 4 f ( xi 3 ) 6 f ( xi 2 ) 4 f ( xi 1 ) f ( xi )
O ( h)
h4

2 f ( xi 5 ) 11 f ( xi 4 ) 24 f ( xi 3 ) 26 f ( xi 2 ) 14 f ( xi 1 ) 3 f ( xi )
O(h 2 )
4
h

Backward Finite Difference Approximations


First Derivative
f '( xi )

f '( xi )

f ( xi ) f ( xi 1 )
O ( h)
h

3 f ( xi ) 4 f ( xi 1 ) f ( xi 2 )
O(h 2 )
2h

Second Derivative
f ''( xi )

f ''( xi )

f ( xi ) 2 f ( xi 1 ) f ( xi 2 )
O (h)
h2

2 f ( xi ) 5 f ( xi 1 ) 4 f ( xi 2 ) f ( xi 3 )
O(h 2 )
2
h

Third Derivative
f '''( xi )

f '''( xi )

f ( xi ) 3 f ( xi 1 ) 3 f ( xi 2 ) f ( xi 3 )
O ( h)
h3

5 f ( xi ) 18 f ( xi 1 ) 24 f ( xi 2 ) 14 f ( xi 3 ) 3 f ( xi 4 )
O(h 2 )
3
2h

Fourth Derivative
f ''''( xi )

f '''( xi )

f ( xi ) 4 f ( xi 1 ) 6 f ( xi 2 ) 4 f ( xi 3 ) f ( xi 4 )
O ( h)
h4

3 f ( xi ) 14 f ( xi 1 ) 26 f ( xi 2 ) 24 f ( xi 3 ) 11 f ( xi 4 ) 2 f ( xi 5 )
O (h 2 )
4
h

Central Finite Difference Approximations


First Derivative
f '( xi )

f '( xi )

f ( xi 1 ) f ( xi 1 )
O(h 2 )
2h

f ( xi 2 ) 8 f ( xi 1 ) 8 f ( xi 1 ) f ( xi 2 )
O(h 4 )
12h

Second Derivative
f ''( xi )

f ''( xi )

f ( xi 1 ) 2 f ( xi ) f ( xi 1 )
O(h 2 )
2
h

f ( xi 2 ) 16 f ( xi 1 ) 30 f ( xi ) 16 f ( xi 1 ) f ( xi 2 )
O(h 4 )
2
12h

Third Derivative
f '''( xi )

f '''( xi )

f ( xi 2 ) 2 f ( xi 1 ) 2 f ( xi 1 ) f ( xi 2 )
O(h 2 )
3
2h

f ( xi 3 ) 8 f ( xi 2 ) 13 f ( xi 1 ) 13 f ( xi 1 ) 8 f ( xi 2 ) f ( xi 3 )
O(h4 )
3
8h

Fourth Derivative
f ''''( xi )

f '''( xi )

f ( xi 2 ) 4 f ( xi 1 ) 6 f ( xi ) 4 f ( xi 1 ) f ( xi 2 )
O (h 2 )
4
h

f ( xi 3 ) 12 f ( xi 2 ) 39 f ( xi 1 ) 56 f ( xi ) 39 f ( xi 1 ) 12 f ( xi 2 ) f ( xi 3 )
O(h 4 )
6h 4

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy