Lagrange interpolation

#Lagrange interpolation
n=input(" No of data points = ")
xd=[]
yd=[]
print "Enter data points"
for i in range(n):
    xi,yi=input()
    xd.append(xi)
    yd.append(yi)

x=input(" Enter the x value for which y is to be found ")
l=0
for i in range(n):
    lg=1
    for j in range(n):
        if i!= j:
            lg=lg*(x-xd[j])/float(xd[i]-xd[j])
          
    l=l+yd[i]*lg
  

print " Y value = ",l

Output
No of data points = 4
Enter data points
-2,5
1,7
3,11
7,34
 Enter the x value for which y is to be found 0
 Y value =  6.03888888889




note:1087/180=6.038888889



Comments