Monomial Interpolation

In [30]:
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline

Data

Here are our data points.

In [43]:
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.
In [44]:
plt.plot(x, y, 'o')
plt.axis((x[0], x[-1], -1, 2));

Compute the coefficients

In [45]:
N = len(x)
V = np.vander(x, increasing=True)

c = np.linalg.solve(V, y)
In [46]:
c
Out[46]:
array([  -2.98538423,   33.92541972, -100.00865813,  103.34975722,
        -33.39752279])

Plot the interpolant over the points

In [48]:
# 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');
In [ ]: