Interpolation is the technique of estimating the value of a function for any intermediate value of the independent variable, while the process of computing the value of the function outside the given range is called extrapolation.
Forward Differences: The differences y1 – y0, y2 – y1, y3 – y2, ……, yn – yn–1 when denoted by dy0, dy1, dy2, ……, dyn–1 are respectively, called the first forward differences. Thus, the first forward differences are :
Δ
Y
r
=
Y
r
+
1
−
Y
r
\Delta Y_{r}=Y_{r+1}-Y_{r}
ΔYr=Yr+1−Yr
# Python3 Program to interpolate using
# newton forward interpolation
# calculating u mentioned in the formula
def u_cal(u, n):
temp = u;
for i in range(1, n):
temp = temp * (u - i);
return temp;
# calculating factorial of given number n
def fact(n):
f = 1;
for i in range(2, n + 1):
f *= i;
return f;
# Driver Code
# Number of values given
n = 4;
x = [ 45, 50, 55, 60 ];
# y[][] is used for difference table
# with y[][0] used for input
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;
# Calculating the forward difference
# table
for i in range(1, n):
for j in range(n - i):
y[j][i] = y[j + 1][i - 1] - y[j][i - 1];
# Displaying the forward difference table
for i in range(n):
print(x[i], end = "\t");
for j in range(n - i):
print(y[i][j], end = "\t");
print("");
# Value to interpolate at
value = 52;
# initializing u and sum
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("\nValue at", value,
"is", round(sum, 6));
# This code is contributed by mits
NEWTON’S GREGORY FORWARD INTERPOLATION FORMULA :
f ( a + h u ) = f ( a ) + u Δ f ( a ) + u ( u − 1 ) 2 ! Δ 2 f ( a ) + . . . + u ( u − 1 ) ( u − 2 ) . . . ( u − n + 1 ) n ! Δ n f ( a ) f(a+hu)=f(a)+u\Delta f(a)+\frac{u\left ( u-1 \right )}{2!}\Delta ^{2}f(a)+...+\frac{u\left ( u-1 \right )\left ( u-2 \right )...\left ( u-n+1 \right )}{n!}\Delta ^{n}f(a) f(a+hu)=f(a)+uΔf(a)+2!u(u−1)Δ2f(a)+...+n!u(u−1)(u−2)...(u−n+1)Δnf(a)
This formula is particularly useful for interpolating the values of f(x) near the beginning of the set of values given. h is called the interval of difference and u = ( x – a ) / h, Here a is the first term.
NEWTON’S GREGORY BACKWARD INTERPOLATION FORMULA :
f
(
a
+
n
h
+
u
h
)
=
f
(
a
+
n
h
)
+
u
∇
f
(
a
+
n
h
)
+
u
(
u
+
1
)
2
!
∇
2
f
(
a
+
n
h
)
+
.
.
.
+
u
(
u
+
1
)
.
.
.
(
u
+
n
−
1
‾
)
n
!
∇
n
f
(
a
+
n
h
)
f(a+nh+uh)=f(a+nh)+u\nabla f(a+nh)+\frac{u\left ( u+1 \right )}{2!}\nabla ^{2}f(a+nh)+...+\frac{u\left ( u+1 \right )...\left ( u+\overline{n-1} \right )}{n!}\nabla ^{n}f(a+nh)
f(a+nh+uh)=f(a+nh)+u∇f(a+nh)+2!u(u+1)∇2f(a+nh)+...+n!u(u+1)...(u+n−1)∇nf(a+nh)
This formula is useful when the value of f(x) is required near the end of the table. h is called the interval of difference and u = ( x – an ) / h, Here an is last term.
# Python3 Program to interpolate using
# newton backward interpolation
# Calculation of u mentioned in formula
def u_cal(u, n):
temp = u
for i in range(n):
temp = temp * (u + i)
return temp
# Calculating factorial of given n
def fact(n):
f = 1
for i in range(2, n + 1):
f *= i
return f
# Driver code
# number of values given
n = 5
x = [1891, 1901, 1911, 1921, 1931]
# y is used for difference
# table and y[0] used for input
y = [[0.0 for _ in range(n)] for __ in range(n)]
y[0][0] = 46
y[1][0] = 66
y[2][0] = 81
y[3][0] = 93
y[4][0] = 101
# Calculating the backward difference table
for i in range(1, n):
for j in range(n - 1, i - 1, -1):
y[j][i] = y[j][i - 1] - y[j - 1][i - 1]
# Displaying the backward difference table
for i in range(n):
for j in range(i + 1):
print(y[i][j], end="\t")
print()
# Value to interpolate at
value = 1925
# Initializing u and sum
sum = y[n - 1][0]
u = (value - x[n - 1]) / (x[1] - x[0])
for i in range(1, n):
sum = sum + (u_cal(u, i) * y[n - 1][i]) / fact(i)
print("\n Value at", value, "is", sum)
# This code is contributed by phasing17
Detailed see: https://www.geeksforgeeks.org/newton-forward-backward-interpolation/