In this lab, we implement three solutions to the interpolation problem.
import numpy as np
from matplotlib import pyplot as plt # For plotting data
np.set_printoptions(suppress=True, precision=2)
### The data is given in this cell for you
X = np.arange(1, 5, 0.5)
Y = np.random.random(X.shape[0]) * 10
# Let us plot instead of printing the data points
plt.grid(True)
plt.plot(X, Y, 'bo')
plt.show()
### Write your interpolation code here
def linear_interpolation(X, Y, x_u) :
return a, b
### Test your code here first by calculating and then plotting
### (x_u, y_u)
a, b = linear_interpolation(X, Y, x_u)
x_u = 2.15
y_u = a * x_u + b
### Let us plot the data and see if it looks good!
plt.grid(True)
plt.plot(X, Y, 'b.')
plt.plot(X, Y, 'xkcd:lime')
plt.plot(x_u, y_u, 'ro')
plt.show()
### Write your code here
def vandermonde_interpolation(X, Y) :
C = np.zeros(Y.shape[0])
return C
At an unknown value $x_u$, the polynomial is calculated as $$ y_u = \frac{(x-x_1)(x-x_2)\ldots(x-x_K)} {(x_0-x_1)(x_0-x_2)\ldots(x_0-x_K)}y_0 + \frac{(x-x_0)(x-x_2)\ldots(x-x_K)} {(x_1-x_0)(x_1-x_2)\ldots(x_1-x_K)}y_1 + \ldots + \frac{(x-x_1)(x-x_2)\ldots(x-x_{K-1})} {(x_{K}-x_1)(x_{K}-x_2)\ldots(x_K-x_{K-1})} $$
### Implement Lagrange Polynomial code here
def lagrange_interpolation(X, Y) :
lagrange = np.zeros(Y.shape[0])
return lagrange