## Errors in finite different approximations of the derivatives¶

In [6]:
f = x -> sqrt(exp(x)) # create a simple function f(x) = sin(x)
df = x -> sqrt(exp(x))/2 # and its derivative

Out[6]:
(anonymous function)
In [13]:
# Look at accuracy
forward_diff = (x, h) -> (f(x+h) - f(x))/h
@show forward_diff(pi/3, 0.0000001)
@show df(pi/3)

forward_diff(pi / 3,1.0e-7) = 0.8440459198233441
df(pi / 3) = 0.8440458974822342

Out[13]:
0.8440458974822342
In [14]:
# Let's look at things in a more structured fashion
x = pi/3.0
for i=1:16
h = 10.0^(-i)
adf = forward_diff(x, h)
err = abs(adf - df(x))
@printf("%.8e  %.16f  %.5e\n", h, adf, err)
end

1.00000000e-01  0.8655031711119876  2.14573e-02
1.00000000e-02  0.8461595334843253  2.11364e-03
1.00000000e-03  0.8442569441295955  2.11047e-04
1.00000000e-04  0.8440669989817629  2.11015e-05
1.00000000e-05  0.8440480075977418  2.11012e-06
1.00000000e-06  0.8440461083392137  2.10857e-07
1.00000000e-07  0.8440459198233441  2.23411e-08
1.00000000e-08  0.8440458998393296  2.35710e-09
1.00000000e-09  0.8440459442482506  4.67660e-08
1.00000000e-10  0.8440470544712753  1.15699e-06
1.00000000e-11  0.8440581567015215  1.22592e-05
1.00000000e-12  0.8439915433200440  5.43542e-05
1.00000000e-13  0.8437694987151190  2.76399e-04
1.00000000e-14  0.8437694987151190  2.76399e-04
1.00000000e-15  0.8881784197001251  4.41325e-02
1.00000000e-16  0.0000000000000000  8.44046e-01

In [15]:
central_diff = (x, h) -> (f(x+h) - f(x-h))/(2.0*h)
@show central_diff(pi/3, 0.01)
@show df(pi/3)

central_diff(pi / 3,0.01) = 0.8440494143445387
df(pi / 3) = 0.8440458974822342

Out[15]:
0.8440458974822342
In [16]:
x = pi/3.0
for i=1:16
h = 10.0^(-i)
adf = central_diff(x, h)
err = abs(adf - df(x))
@printf("%.8e  %.16f  %.5e\n", h, adf, err)
end

1.00000000e-01  0.8443976272361930  3.51730e-04
1.00000000e-02  0.8440494143445387  3.51686e-06
1.00000000e-03  0.8440459326507499  3.51685e-08
1.00000000e-04  0.8440458978342669  3.52033e-10
1.00000000e-05  0.8440458974856567  3.42248e-12
1.00000000e-06  0.8440458973968390  8.53952e-11
1.00000000e-07  0.8440458987291066  1.24687e-09
1.00000000e-08  0.8440458998393296  2.35710e-09
1.00000000e-09  0.8440459442482506  4.67660e-08
1.00000000e-10  0.8440459442482506  4.67660e-08
1.00000000e-11  0.8440581567015215  1.22592e-05
1.00000000e-12  0.8439915433200440  5.43542e-05
1.00000000e-13  0.8437694987151190  2.76399e-04
1.00000000e-14  0.8437694987151190  2.76399e-04
1.00000000e-15  0.8881784197001251  4.41325e-02
1.00000000e-16  0.0000000000000000  8.44046e-01