Вы находитесь на странице: 1из 12

Introduction to Finite Differences

KnutAndreas Lie SINTEF ICT, Dept. Applied Mathematics

INF2340 / Spring 2005 p.

Ordinary and Partial Differential Equations


What is an ordinary differential equation (ODE)? An equation relating a function to its derivatives of a single variable (in such a way that the function itself can be determined) Convention regarding notation:
time-derivatives are denoted by a dot y(t) =
dy dt (t) dy dx (x)

other derivatives are denoted by a prime y (x) =

Equations relating derivatives of more than one independent variables are called partial differential equations (PDEs) Different notations: u = x u = u x x
INF2340 / Spring 2005 p.

Motivation
In this course we will study simulation of differential equations:
steady heat equation (elliptic): heat equation (parabolic): wave equation (hyperbolic): transport equations:
2

u = uxx + uyy = q
2 u, 2

ut = utt = ut +

u,
2 u,

f (u) =

Common for all: relates various derivatives of unknown functions These are all continous quantities, which cannot be represented on a computer we need discrete quantities.

INF2340 / Spring 2005 p.

Computing Derivatives
Question: How do we compute the derivative of a given function f (x) on a computer? Consider the denition of the derivative:
f (x) = lim f (x + h) f (x) . h0 h

Idea: use a nite h to estimate f (x):


f (x + h) f (x) f (x) h

This is a forward nite difference. Clearly h must be small for this to be a good approximation
INF2340 / Spring 2005 p.

Finite differences
Rigorous derivation from MacLoren series:
h2 f (x + h) = f (x) + hf (x) + f (), 2

for x x + h. We can rearrange to


f (x + h) f (x) h f (). f (x) = h 2

Thus, the error we make by using forward differences is


f (x + h) f (x) f (x) M h, h

where M depends on f . We call the approximation rst order since the error is O(h).
INF2340 / Spring 2005 p.

Finite differences contd


Similarly, we derive backward differences
f (x) f (x) f (x h) , h

which also are rst order: | f (x)f (xh) f (x)| M h. h We can also derive central differences
f (x + h) f (x h) f (x) 2h

which are second order | f (x+h)f (xh) f (x)| M h2 . 2h Here M depends on f .

INF2340 / Spring 2005 p.

Finite differences contd


Higher-order approximations:
second order: f (x) = third order: f (x) = fourth order:
f (x+2h)+4f (x+h)3f (x) 3h

+ O(h2 ) + O(h3 )

2f (x+h)+3f (x)6f (xh)+f (x2h) 6h

f (x) =
....

f (x+2h)+8f (x+h)8f (xh)+f (x2h) 12h

+ O(h4 )

Playing with Taylor series, one can dene a host of approximations....

Exercise: Verify some of the above formulas.

INF2340 / Spring 2005 p.

Graphical interpretation

backward forward central

exact backward forward central

discrete points

slopes

INF2340 / Spring 2005 p.

Example: f (1.0) for f (x) = x2 + sin(x)


Ff (x) = f (x + h) f (x) , h Fb (x) = f (x + h) f (x) , h Fc (x) = f (x + h) f (x h) 2h

N 8 16 32 64 128 256

f (x) 2.5403 2.5403 2.5403 2.5403 2.5403 2.5403

Ff (x) 2.6114 2.5762 2.5583 2.5493 2.5448 2.5426

Ff f 7.11e-02 3.59e-02 1.80e-02 9.03e-03 4.52e-03 2.26e-03

Fb (x) 2.4664 2.5037 2.5221 2.5312 2.5358 2.5380

Fb f -7.39e-02 -3.66e-02 -1.82e-02 -9.07e-03 -4.53e-03 -2.26e-03

Fc (x) 2.5389 2.5400 2.5402 2.5403 2.5403 2.5403

Fc f -1.41e-03 -3.52e-04 -8.79e-05 -2.20e-05 -5.50e-06 -1.37e-06

INF2340 / Spring 2005 p.

Second order derivatives


Consider once more the Taylor series
h2 h3 (3) f (x + h) = f (x) + hf (x) + f (x) + f (x) + 2 6 h2 h3 (3) f (x h) = f (x) hf (x) + f (x) f (x) + 2 6 h4 (4) f () 24 h4 (4) f () 24

Adding and rearranging terms we obtain


f (x + h) 2f (x) + f (x h) = f (x) + eh (x) h2

where the error is bounded by |eh (x)| supx |f (4) (x)|h2 /12.

INF2340 / Spring 2005 p. 1

Second order derivatives contd


Alternatively, we can use forward and backward approximations:
f (x)
fwd.

f (x + h) f (x) h f (x + h) 2f (x) + f (x h) = h2

f (x+h)f (x) bwd. h

f (x)f (xh) h

or the other way around


f (x) f (x h) fwd. h f (x) h f (x + h) 2f (x) + f (x h) = h2
bwd.

f (x+h)f (x)

f (x)f (xh) h

INF2340 / Spring 2005 p. 1

Second order derivatives contd


And to make the confusion complete; we can apply central differences twice
ctr. f (x + h/2) f (x h/2) ctr. f (x) h f (x + h) 2f (x) + f (x h) = h2

f (x+h)f (x) h

f (x)f (xh) h

Higher-order approximation:
f (x + 2h) + 16f (x) 30f (x) + 16f (x h) f (x 2h) f (x) = +O(h4 ) 12h2

INF2340 / Spring 2005 p. 1

Вам также может понравиться