Newton Interpolation Method in Python
Newton Interpolation Method in Python
Interpolation is the estimation of the value of two known values in a range of values.
Newton's fractional difference interpolation formula is an interpolation technique used when the interval difference is not equal to all values.
The (n + 1) values of the y = f (x) function correspond to the arguments x = x0, x1, x2 are f (x0), f (x1), f (x2) ……… f (xn)… Xn, where interval differences Are not identical.
Newton Interpolation Formula
Write a Program to find value of x using Newton Interpolation Formula
value at 52
Input: x = 45, 50, 55, 60
y= f(x) = 0.7071, 0.7660, 0.8192, 0.8660
Python Code:
def u_cal(u,n):
temp = u
for i in range(1,n):
temp = temp*(u-i)
return temp
def fact(n):
f = 1
for i in range(2,n+1):
f*=i
return f
n = 4
x = [45,50,55,60]
y = [[0 for i in range(n)] for j in range(n)]
y[0][0]=0.7071
y[1][0]=0.7660
y[2][0]=0.8192
y[3][0]=0.8660
for i in range(1,n):
for j in range(n-i):
y[j][i]=y[j+1][i-1]-y[j][i-1]
for i in range (n):
print(x[i],end="\t")
for j in range(n-i):
print(y[i][j],end="\t")
print("")
value = 52
sum = y[0][0]
u =(value-x[0]/x[1]-x[0])
for i in range(1,n):
sum = sum+(u_cal(u,i)*y[0][i]/fact(i))
print("\n value at",value,"is",round(sum,6))
Output:
45 0.7071 0.05890000000000006 -0.005700000000000038 -0.0007000000000000339
50 0.766 0.053200000000000025 -0.006400000000000072
55 0.8192 0.04679999999999995
60 0.866
value at 52 is 0.962846
Example 2:
Write a program to find value of x using Newton Interpolation Formula
value at 1925
Input: x = 1891,1901,1911,1921,1931
y = f(x) = 46,66,81,93,101
Python Code
def u_cal(u,n):
temp = u
for i in range(1,n):
temp = temp*(u-i)
return temp
def fact(n):
f = 1
for i in range(2,n+1):
f*=i
return f
n = 5
x = [1891,1901,1911,1921,1931]
y = [[0 for i in range(n)] for j in range(n)]
y[0][0]=46
y[1][0]=66
y[2][0]=81
y[3][0]=93
y[4][0]=101
for i in range(1,n):
for j in range(n-i):
y[j][i]=y[j+1][i-1]-y[j][i-1]
for i in range (n):
print(x[i],end="\t")
for j in range(n-i):
print(y[i][j],end="\t")
print("")
value=1925
sum=y[0][0]
u =(value-x[0]/x[1]-x[0])
for i in range(1,n):
sum = sum+(u_cal(u,i)*y[0][i]/fact(i))
print("\n value at",value,"is",round(sum,6))Output:
1891 46 20 -5 2 -3 1901 66 15 -3 -1 1911 81 12 -4 1921 93 8 1931 101 value at 1925 is -113859.489961