Check out the Pastebin Gadgets Shop. We have thousands of fun, geeky & affordable gadgets on sale :-)Want more features on Pastebin? Sign Up, it's FREE!
tweet

# Langrange polynomial interpolation

By: a guest on May 15th, 2014  |  syntax: Python  |  size: 0.93 KB  |  views: 701  |  expires: Never
This paste has a previous version, view the difference. Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
1. #!/usr/bin/env python
2.
3. import numpy as np
4. from matplotlib import pyplot as plt
5.
6. data_fname = 'knot_points.csv'
7. # x1,y1
8. # x2,y2
9. # ...
10.
12.     X = []
13.     Y = []
14.     with open(fname, 'r') as f:
15.         for line in f:
16.             (x, y) = line.split(',')
17.             X.append(float(x))
18.             Y.append(float(y))
19.     return (X, Y)
20.
21. def langrange_polynomial(X, Y):
22.     def L(i):
23.         return lambda x: np.prod([(x-X[j])/(X[i]-X[j]) for j in range(len(X)) if i != j]) * Y[i]
24.     Sx = [L(i) for i in range(len(X))]  # summands
25.     return lambda x: np.sum([s(x) for s in Sx])
26.