Skip to content

Instantly share code, notes, and snippets.

@gonzula
Created November 8, 2018 13:18
Show Gist options
  • Save gonzula/268942b621f6366e07594fbf49499e09 to your computer and use it in GitHub Desktop.
Save gonzula/268942b621f6366e07594fbf49499e09 to your computer and use it in GitHub Desktop.
La Grange Polynomial
#!/usr/bin/env python3
import numpy as np
from matplotlib import pyplot as plt
from numpy.polynomial import polynomial as poly
x = np.array([1,2, 3])
y = np.array([-1, 12, 39])
L = []
for i, x_i in enumerate(x):
L_i = poly.Polynomial(poly.polyfromroots(np.delete(x, i)))
L_i /= L_i(x_i)
L.append(L_i)
P = np.sum(y * l for y, l in zip(y, L))
print(P)
exit()
t = np.linspace(0, 1, 5000)
plt.plot(t, f(t), 'blue')
plt.plot(t, P(t), 'green')
plt.plot(t, (P(t) - f(t)), 'red')
plt.plot([t[0], t[-1]], [0, 0], 'purple')
plt.show()
print(np.linalg.norm(P(t) - f(t), np.inf))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment