Copyright (C) 2020 Andreas Kloeckner
import numpy as np
import numpy.linalg as la
import matplotlib.pyplot as pt
plot_x = np.linspace(-3, 3, 200)
np.random.seed(20)
centers = np.random.randn(10)*0.05 + np.linspace(-1.5, 1.5, 10)
centers = np.sort(centers)
centers
array([-1.45580534, -1.15687342, -0.81545651, -0.6171631 , -0.2209083 , 0.19465148, 0.54697347, 0.78440928, 1.19182151, 1.52032072])
radius = 0.3
def radial_basis_function(x, i):
return np.exp(-(x-centers[i])**2/radius**2)
pt.plot(plot_x, radial_basis_function(plot_x, 3))
[<matplotlib.lines.Line2D at 0x7feae0471ef0>]
def f(x): return x**3 - 3*x
pt.plot(plot_x, f(plot_x))
[<matplotlib.lines.Line2D at 0x7feae0412f28>]
Let's build a Vandermonde matrix at the centers:
nodes = centers
V = np.array([
radial_basis_function(nodes, i)
for i in range(len(centers))
]).T
Find the coefficients:
coeffs = la.solve(V, f(nodes))
Find the interpolant:
interpolant = 0
for i in range(len(centers)):
interpolant += coeffs[i] * radial_basis_function(plot_x, i)
pt.figure(figsize=(8,8))
pt.ylim([-5,5])
pt.plot(plot_x, interpolant, label="Interpolant")
pt.plot(plot_x, f(plot_x), label="$f$")
pt.plot(centers, f(centers), "o")
pt.legend(loc="best")
<matplotlib.legend.Legend at 0x7feae03853c8>