import numpy as np
import numpy.linalg as la
import matplotlib.pyplot as pt
degree = 2
h = 0.25
# Assume even degree so that there's a well-defined middle node.
assert degree % 2 == 0
nodes = np.linspace(-h/2, h/2, degree+1)
nodes
Now construct V
(the generalized Vandermonde) and Vprime
(the generalized Vandermonde for the derivatives):
V = np.array([
nodes**i
for i in range(degree+1)
]).T
def monomial_deriv(i, x):
if i == 0:
return 0*x
else:
return i*nodes**(i-1)
Vprime = np.array([
monomial_deriv(i, nodes)
for i in range(degree+1)
]).T
Combine them to form the derivative matrix:
diff_mat = Vprime.dot(la.inv(V))
Let's say we only care about the derivative at the middle node:
finite_difference_weights = diff_mat[degree//2]
finite_difference_weights
# * We could have left the middle point out. :)
# * -4*f(x-0.25) + 4*f(x+0.25)
# * They scale with 1/h, as you might expect.
# * (f(x-h/2) + f(x+h/2))/h
# * We get a more complicated (but more accurate) formula.
# * Nothing.
# * We get a different formula (that's valid for those nodes).
# * See below.
def f(x):
return np.sin(4*x)
def df(x):
return 4*np.cos(4*x)
x = np.arange(10) * 0.125
pt.plot(x, f(x), "o-")
Now use the weights to compute the finite difference derivative as deriv
:
fdw = finite_difference_weights
fx = f(x)
deriv = np.zeros(len(x)-2)
for i in range(1, 1+len(deriv)):
deriv[i-1] = fx[i-1]*fdw[0] + fx[i]*fdw[1] + fx[i+1]*fdw[2]
Now plot the finite difference derivative:
pt.plot(x[1:-1], df(x[1:-1]), label="true")
pt.plot(x[1:-1], deriv, label="FD")
pt.legend()