Newtons forward Difference formula
#Newtons forward Difference formula
xpoints=[30.1,30.2,30.3,30.4,30.5,30.6]
ypoints=[.5015107,.5030199,.5045276,0.5060388,0.5075384,0.5090414]
n=len(xpoints)
z=[0]
d1=z*n
d2=z*n
d3=z*n
d4=z*n
d5=z*n
x=float(input('enter data point for interpolation'))
h=xpoints[1]-xpoints[0]
for i in range(0,n-1):
d1[i]=ypoints[i+1]-ypoints[i]
for i in range(0,n-2):
d2[i]=d1[i+1]-d1[i]
for i in range(0,n-3):
d3[i]=d2[i+1]-d2[i]
for i in range(0,n-4):
d4[i]=d3[i+1]-d3[i]
for i in range(0,n-5):
d5[i]=d4[i+1]-d4[i]
print('Table of first order differences \n', d1)
print('\n Table of second order differences \n',d2)
print('\n Table of third order differences \n',d3)
print('\n Table of fourth order differences \n',d4)
print('\n Table of fifth order differences \n',d5)
p=(x-xpoints[0])/h
print ('p=',p)
print ('h=',h)
yvalue=ypoints[0]+p*d1[0]+(p*(p-1)*d2[0])/2+(p*(p-1)*(p-2)*d3[0])/6
print ('value of y corresponding to',x,'is',yvalue)
Comments
Post a Comment