## Description

MATH 340

LAB 8

Numerical Differentiation:

First Order Derivative

To approximate the first order derivative of a function f(x) at a point x0, we can

use the forward finite difference:

f

0

(x0) ≈ Df (f(x0)) = f(x0 + h) − f(x0)

h

(1)

where h is the stepsize (difference between two consecutive grid points h = xi −

xi−1). The notation used here with the subscript f means that the finite difference

used is a forward difference. This numerical approximation is of first order of accuracy,

meaning that the error between the approximated discrete derivative and the actual derivative, namely Errf = |f

0

(x0) − Df (f(x0))| depends on h. So halving the

stepsize h will roughly halve the error as well.

We can use a more accurate approximation of the first order derivative, using

a centered difference:

f

0

(x0) ≈ Dc(f(x0)) = f(x0 + h) − f(x0 − h)

2h

(2)

where again, the notation used here with the subscript c means that the finite difference used is now a centered difference. This approximation is of second order of accuracy,

meaning that the error Errc = |f

0

(x0) − Dc(f(x0))| depends on h

2

; so halving the

stepsize h will roughly divide by 4 the error.

Problem 1:

Use both the forward difference Df (f(x0)) and the centered one Dc(f(x0)) to

approximate the first order derivative of the function f(x) = e

x at the point

x0 = 0, with h = 10−n

, with n = 1, 2, . . . , 9. To compute the errors Errf and Errc

you need the actual value of f

0

(x0). Use the built-in diff in MATLAB, that works

1

with symbolic functions; do not calculate this by hand. Print your results in a

table with the following columns: h, Df (f(x0)), Errf , Dc(f(x0)), Errc, Ratiofn

,

Ratiocn

, where Ratiofn

, Ratiocn are respectively the ratio of two consecutive errors

for different n’s using the forward scheme: Ratiofn =

Errfn

Errfn−1

and the ratio of two

consecutive errors for different n’s using the centered scheme: Ratiocn =

Errcn

Errcn−1

(for n ≥ 2 of course). Compare your table with table of example 5.3 on page 247

of your textbook. To compare with the book’s results, note that you need 14 digits

of precision.

To print out your numerical results in tabular form (the border lines don’t matter)

use the fprintf command and make sure to end it with a new line \n.

Second Order Derivative

To approximate the second order derivative of a function f(x) at a point x0, we

can use the centered finite difference of second order:

f

00(x0) ≈ D

(2)

c

(f(x0)) = f(x0 − h) − 2f(x0) + f(x0 + h)

h

2

(3)

This approximation is of second order of accuracy, meaning that the error Errc =

|f

00(x0) − D

(2)

c (f(x0))| depends on h

2

; so halving the stepsize h will roughly divide

by 4 the error.

Errors due to round-off

If we assume that the evaluation of the function at the grid points f(xi) has some

round off errors, it can be written as a truncated version fb(xi), and this leads to

a truncated version of the second derivative:

f

00(x0) ≈ D

(2)

c

(fb(x0)) = fb(x0 − h) − 2fb(x0) + fb(x0 + h)

h

2

(4)

Problem 2:

Use both the exact version of the evaluations (3) and the truncated one (4) to

approximate the second order derivative of f(x) = cos x at the point x0 = π/6,

with h =

1

2n , with n = 1, 2, . . . , 9. To truncate the evaluations f(xi) with 6 digits of

precision, use round(f(x_i)*10^6)/10^6, or if you use recent versions of Matlab

you can take advantage of the command round(f(x_i),n) where n represents

2

how many decimal digits you want to truncate at. Print your results in a table

with the following columns: h, D

(2)

c (f(x0)), D

(2)

c (fb(x0)), ErrD(2)

c (f), ErrD(2)

c (fb),

RatioD(2)

c (f), RatioD(2)

c (fb), where the ratios are defined as above.

Finally, plot in the same figure both ErrDc(f), ErrDc(fb) versus a vector of all

h’s. You can constraint the axes to better visualize curves closely. What happens

to ErrDc(fb)? Write your conclusions on your results.

3