Download as pdf or txt
Download as pdf or txt
You are on page 1of 3

Discrete approximation of derivatives

F. S. Guzmán
Instituto de Fı́sica y Matemáticas, Universidad Michoacana de San Nicolás de Hidalgo. Edificio C-3,
Cd. Universitaria, 58040 Morelia, Michoacán, México.
(Dated: March 18, 2020)
Kickass chapter....

I. DOMAIN

Consider the doman I = [xmin , xmax ] ⊆ R. We define the uniform discrete version of I, Id as

Id = {x ∈ I | xi = xmin + i∆x} (1)

where xi = xmin + i∆x with i = 0, ..., Nx and ∆x = (xmax − xmin )/Nx is the resolution of the discretization.

Let f : I ⊆ R → R a function. We define the discrete version of f as the set fi = f (xi ), with xi ∈ Id .

A. Spatial derivatives

The construction of derivatives uses Taylor expansions of fin at near neighbors. For derivatives along x, the values
of the function at nearest neighbors are fi−1 and fi+1 . In terms of a Taylor expansion around xi these values are

∆x2 ∂ 2 f ∆x3 ∂ 3 f

∂f
fi−1 = fin − ∆x + − + O(∆x4 )
∂x xi 2! ∂x2 xi 3! ∂x3 xi
fi = fin
∆x2 ∂ 2 f ∆x3 ∂ 3 f

∂f
fi+1 = fin + ∆x + + + O(∆x4 ) (2)
∂x xi 2! ∂x2 xi 3! ∂x3 xi

Appropriate combinations of these three expansions isolate the first and second derivatives with respect to x.

For the first order derivative there are various suitable combinations


∂f ∂f fi+1 − fi
fi+1 − fi = ∆x + O(∆x2 ) ⇒ = + O(∆x) (3)
∂x xi ∂x xi ∆x

∂f ∂f fi − fi−1
fi − fi−1 = ∆x + O(∆x2 ) ⇒ = + O(∆x) (4)
∂x xi ∂x xi
∆x

∂f ∂f fi+1 − fi−1
fi+1 − fi−1 = 2∆x + O(∆x3 ) ⇒ = + O(∆x2 ) (5)
∂x xi ∂x xi 2∆x

which are three discrete versions of the first order derivative with respect to x defined on Id . The first two formulas
are first order accurate, whereas the third one is second order accurate. Their respective names are upwind, downwind
and centered stencils, or discrete versions of the first order derivative. Illustration of these formulas appear in Figure 1.

These three expressions suffice to accurately solve many interesting problems. Nevertheless we want to go slightly
forward and construct upwind and downwind second order accurate operators because they will be needed, and most
importantly, to illustrate and reinforce the process of construction of these operators. For this, it is necessary to
Taylor expand the values of f at the second nearest neighbors that should accompany the expansions (2)
2

FIG. 1: Illustration formulas (3), (4) and (5). The curve is supposed to be the drawing of the continuous y(x).

22 ∆x2 ∂ 2 f 23 ∆x3 ∂ 3 f

∂f
fi−2 = fi − 2∆x + − + O(∆x4 )
∂x xi 2! ∂x2 xi 3! ∂x3 xi
22 ∆x2 ∂ 2 f 23 ∆x3 ∂ 3 f

∂f
fi+2 = fi + 2∆x + + + O(∆x4 ) (6)
∂x xi 2! ∂x2 xi 3! ∂x3 xi

Combinations of the expansions (2) and (6) for second order accurate upwind and downwind versions of the first order
derivative are


∂f ∂f fi+2 − 4fi+1 + 3fi
fi+2 − 4fi+1 + 3fi = −2∆x + O(∆x3 ) ⇒ =− + O(∆x2 ) (7)
∂x xi ∂x xi 2∆x

∂f ∂f fi−2 − 4fi−1 + 3fi
fi−2 − 4fi−1 + 3fi = 2∆x + O(∆x3 ) ⇒ = + O(∆x2 ) (8)
∂x xi ∂x
xi 2∆x

which are second order accurate.

Now we construct expressions for the second order derivative with respect to x using combinations of (2)

2

2∂
f
fi−1 − 2fi + fi+1 = ∆x + O(∆x4 ) ⇒
∂x2 xi
∂ 2 f

fi−1 − 2fi + fi+1
= + O(∆x2 ) (9)
∂x2 xi ∆x2

which is a discrete centered version of the second derivative with respect to x defined on Id . An academic exercise
could be the construction of upwind/downwind versions of the second derivative with second order
accuracy.

Summarizing, the approximate expressions (5), (7), (8) for first order derivatives and (9) for second order
derivatives, constitute a kit that will be very useful to understand and the forthcoming subjects.
2
1. Class exercises. You have to calculate the first order derivative of f = e−x sin(x) in the domain I = [−5, 5]
using the second order accurate stencils of formulas (5), (7) and (8). a) Using the discretization with Nx = 1000
cells, you choose Nx . b) Calculate the derivative using a discretization with 2Nx cells. c) Now using 4Nx cells. Using
TWO plots: i) show that the error in (a) is four times bigger than that of (b) and ii) that the error in (b) is four
times bigger than that of (c).

2. Question. What is the order of convergence of your calculations, first or second order?

3. Class exercise. Use a very small value of Nx . Show the error with three Nx , 2Nx and 4Nx such that there is
no convergence.
3
2
4. Class exercise. Calculate numerically the second order derivative of the function f = e−x sin(x) in the
domain I = [−5, 5] using a discrete domain with Nx = 1000 cells and formula (9). Show with a plot, that it converges
with second order when using 2000 and 4000 cells in the interior points i = 1, ..., Nx − 1.

5. Question. What happens at the boundary points i = 0, i = Nx ?

You might also like