import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
Here are our data points.
demo = 0
if demo==0:
N = 5
x = np.linspace(0, N-1, num=N)
x = sorted(np.random.rand(N))
y = np.random.rand(N)
elif demo==1:
x = np.arange(10, dtype=float)
y = np.zeros_like(x)
y[5:] = 1.
plt.plot(x, y, 'o')
plt.axis((x[0], x[-1], -1, 2));
N = len(x)
V = np.vander(x, increasing=True)
c = np.linalg.solve(V, y)
c
# Create a bunch of x-values
xx = np.linspace(x[0], x[-1], 100)
# Get N cols of vander(xx)
VV = np.vander(xx, N=N, increasing=True)
yy = VV @ c # eval polynomial at new x-values
# Plot original point, and polynomial
plt.plot(x,y,'o')
plt.plot(xx,yy)
plt.title('Polynomial Interpolant');