Files
CalWay_Python/按方法整理/插值-牛顿前插-牛顿基本插值.py
2025-06-16 20:44:29 +08:00

93 lines
3.4 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import math
# 求差分表
def GetDyList(list_y):
result = []
result.append(list_y)
for i in range(len(list_y)-1,0,-1):
tmp = []
for j in range(i):
tmp.append(result[len(list_y)-1-i][j+1] - result[len(list_y)-1-i][j])
result.append(tmp)
return result
# 牛顿前插余项计算
def NewtonForwardRegression(x,n,list_x,FxDiff_n):
h = list_x[1] - list_x[0]
t = (x - list_x[0]) / h
ks = max([abs(FxDiff_n(list_x[i],n)) for i in range(n)])
result = 1
for i in range(n):
result *= (t-i)*h/(i+1)
return result * ks
# 牛顿前插
def NewtonForwardInterpolation(x,n,list_x,list_y,FxDiff_n):
list_dyk = GetDyList(list_y)
print("\n差分表")
print("--------------------------------------------------")
for i in range(len(list_dyk)):
print(list_dyk[i])
print("--------------------------------------------------")
h = list_x[1] - list_x[0]
t = (x - list_x[0]) / h
result = list_y[0]
mul = 1
result = list_y[0]
for i in range(1, n):
mul *= ((t-i+1)/i)
result += list_dyk[i][0] * mul
r = abs(NewtonForwardRegression(x,n,list_x,FxDiff_n))
return (result,r)
# 求差商表
def GetDQyList(list_x,list_y):
result = []
result.append(list_y)
for i in range(len(list_y)-1,0,-1):
tmp = []
for j in range(i):
tmp.append((result[len(list_y)-1-i][j+1] - result[len(list_y)-1-i][j])/(list_x[j+len(list_y)-i] - list_x[j]))
result.append(tmp)
return result
# 牛顿基本插值
def NewtonBaseInterpolation(x,n,list_x,list_y):
list_dqy = GetDQyList(list_x,list_y)
print("\n差商表")
print("--------------------------------------------------")
for i in range(len(list_dqy)):
print(list_dqy[i])
print("--------------------------------------------------")
result = list_dqy[0][0]
mul = 1
for i in range(1,n):
mul *= (x - list_x[i-1])
result += list_dqy[i][0] * mul
return result
# 定义原函数和其导函数计算结果,用于计算牛顿前插的截断误差,如果不需要则不用管
def FxDiff_n1(x,n):
result = 0
if n == 0:
# 下面改成原函数 ############################################################
result = math.exp(x)
else:
# 下面改成n阶导数 ##############################################################################
result = math.exp(x)
return result
if __name__ == '__main__':
##############################################################################################################
list_x = [0.0,0.2,0.4,0.6,0.8] # 已给出的x数值与y数值对应
list_y = [1.0,1.2214,1.4918,1.8221,2.2255] # 已给出的y数值与x数值对应
x_to_predict = 0.12 # 要预测的x值
# 记得改下面的点数(第二个参数)n是点数阶数为n-1 ################################
print("三点牛顿前插结果为%f, 截断误差%f" % NewtonForwardInterpolation(x_to_predict, 3, list_x, list_y,FxDiff_n1))
print("四点牛顿前插结果为%f, 截断误差%f" % NewtonForwardInterpolation(x_to_predict, 4, list_x, list_y,FxDiff_n1))
print("三点牛顿基本插值结果为%f" % NewtonBaseInterpolation(x_to_predict, 3, list_x, list_y))
print("四点牛顿基本插值结果为%f" % NewtonBaseInterpolation(x_to_predict, 4, list_x, list_y))