#Newton forward interpolation
from numpy import*
def fact(n):
if n<=1:
return(1)
else:
return(n*fact(n-1))
n=input("Enter no of elements ")
y=zeros([n,n])
x0,h=input("Enter the value of x[0] and finite difference ")
x=[x0+i*h for i in range(n)]
print "Enter y values"
for i in range(n):
print "Enter y value for x[%d]"%(i)
y[i,0]=input()
for i in range(1,n):
k=n-i
for j in range(k):
y[j,i]=y[j+1,i-1]-y[j,i-1]
print y
xp=input(" Enter the x value for which y is to be found")
u=(xp-x[0])/float(h)
#print "u=",u
yp=y[0,0]
for i in range(1,n):
t=1
for j in range(i):
t=t*(u-j)
yp=yp+t*y[0,i]/fact(i)
print "The value of y is %f "%(yp)
No comments:
Post a Comment