import numpy as np
import numpy.linalg as la
import matplotlib.pyplot as pt
Define a function and its derivative:
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))
[<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:
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
[<matplotlib.lines.Line2D at 0x7f0d295ce908>]