Newton Forward And Backward Interpolation

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+1Yr
在这里插入图片描述

# 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(u1)Δ2f(a)+...+n!u(u1)(u2)...(un+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)+uf(a+nh)+2!u(u+1)2f(a+nh)+...+n!u(u+1)...(u+n1)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/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值