Newtons backward Difference formula -sastry 3.6
#Newtons backwardDiffernce formula -sastry 3.6
xpoints=[15,20,25,30,35,40]
ypoints=[.2588190,0.3420201,0.4226183,0.5,0.573576,0.6427876]
n=len(xpoints)
z=[0]
d1=z*(n-1)
d2=z*(n-2)
d3=z*(n-3)
d4=z*(n-4)
d5=z*(n-5)
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
yvalue1=yvalue+(p*(p-1)*(p-2)*(p-3)*d4[0])/24+(p*(p-1)*(p-2)*(p-3)*(p-4)*d5[0])/120
print ('value of y corresponding to',x,'is',yvalue1)
p=(x-xpoints[-1])/h
print ('p=',p)
yvalue2=ypoints[-1]+p*d1[-1]+(p*(p+1)*d2[-1])/2+(p*(p+1)*(p+2)*d3[-1])/6
yvalue3=yvalue2+(p*(p+1)*(p+2)*(p+3)*d4[-1])/24+(p*(p+1)*(p+2)*(p+3)*(p+4)*d5[-1])/120
print ('value of y corresponding to',x,'is',yvalue3)
Output Screen:
#Newtons forward Differnce formula -Grewal 7.1
xpoints=[100,150,200,250,300,350,400]
ypoints=[10.63,13.03,15.04,16.81,18.42,19.90,21.27]
n=len(xpoints)
z=[0]
d1=z*(n-1)
d2=z*(n-2)
d3=z*(n-3)
d4=z*(n-4)
d5=z*(n-5)
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
yvalue1=yvalue+(p*(p-1)*(p-2)*(p-3)*d4[0])/24+(p*(p-1)*(p-2)*(p-3)*(p-4)*d5[0])/120
print ('value of y corresponding to',x,'is',yvalue1)
p=(x-xpoints[-1])/h
print ('p=',p)
yvalue2=ypoints[-1]+p*d1[-1]+(p*(p+1)*d2[-1])/2+(p*(p+1)*(p+2)*d3[-1])/6
yvalue3=yvalue2+(p*(p+1)*(p+2)*(p+3)*d4[-1])/24+(p*(p+1)*(p+2)*(p+3)*(p+4)*d5[-1])/120
print ('value of y corresponding to',x,'is',yvalue3)
Comments
Post a Comment