Floating Point vs Finite Differences

Copyright (C) 2020 Andreas Kloeckner

MIT License Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
In [5]:
import numpy as np
import numpy.linalg as la
import matplotlib.pyplot as pt

Define a function and its derivative:

In [17]:
c = 20*2*np.pi

def f(x):
    return np.sin(c*x)

def df(x):
    return c*np.cos(c*x)

n = 2000
x = np.linspace(0, 1, n, endpoint=False).astype(np.float32)

pt.plot(x, f(x))
Out[17]:
[<matplotlib.lines.Line2D at 0x7f0d292a0cc0>]

Now compute the relative $l^\infty$ norm of the error in the finite differences, for a bunch of mesh sizes:

In [16]:
h_values = []
err_values = []

for n_exp in range(5, 24):
    n = 2**n_exp
    h = (1/n)

    x = np.linspace(0, 1, n, endpoint=False).astype(np.float32)

    fx = f(x)
    dfx = df(x)

    dfx_num = (np.roll(fx, -1) - np.roll(fx, 1)) / (2*h)

    err = np.max(np.abs((dfx - dfx_num))) / np.max(np.abs(fx))

    print(h, err)

    h_values.append(h)
    err_values.append(err)

pt.rc("font", size=16)
pt.title(r"Single precision FD error on $\sin(20\cdot 2\pi)$")
pt.xlabel(r"$h$")
pt.ylabel(r"Rel. Error")
pt.loglog(h_values, err_values)
0.03125 4.8089
0.015625 1.24653
0.0078125 0.314495
0.00390625 0.0789223
0.001953125 0.0200939
0.0009765625 0.00580978
0.00048828125 0.003088
0.000244140625 0.00217628
0.0001220703125 0.00588608
6.103515625e-05 0.0104866
3.0517578125e-05 0.0216255
1.52587890625e-05 0.0410156
7.62939453125e-06 0.0843182
3.814697265625e-06 0.166426
1.9073486328125e-06 0.416862
9.5367431640625e-07 0.586523
4.76837158203125e-07 1.42031
2.384185791015625e-07 3.42323
1.1920928955078125e-07 7.42658
Out[16]:
[<matplotlib.lines.Line2D at 0x7f0d295ce908>]
In [12]: