From 01e3e1732b5049a9877efb07a39bb8ec12ff4891 Mon Sep 17 00:00:00 2001 From: 123 <629825095@qq.com> Date: Fri, 13 Jun 2025 20:43:17 +0800 Subject: [PATCH] 111 --- 69-3.py | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/69-3.py b/69-3.py index 7e5b2ae..cafc013 100644 --- a/69-3.py +++ b/69-3.py @@ -1,8 +1,5 @@ import math -list_x = [0.0,0.2,0.4,0.6,0.8] -list_y = [1.0,1.2214,1.4918,1.8221,2.2255] - # 定义原函数和其导函数计算结果 def FxDiff_n(x,n): return math.exp(x) @@ -22,15 +19,16 @@ def GetDyList(list_y): def NewtonForwardRegression(x,n,list_x): h = list_x[1] - list_x[0] t = (x - list_x[0]) / h - ks = max([abs(FxDiff_n(list_x[i],n+1)) for i in range(n+1)]) + ks = max([abs(FxDiff_n(list_x[i],n)) for i in range(n)]) result = 1 - for i in range(n+1): + for i in range(n): result *= (t-i)*h/(i+1) return result * ks # 牛顿前插 def NewtonForwardInterpolation(x,n,list_x,list_y): list_dyk = GetDyList(list_y) + print(list_dyk) h = list_x[1] - list_x[0] t = (x - list_x[0]) / h result = list_y[0] @@ -57,6 +55,7 @@ def GetDQyList(list_x,list_y): # 牛顿基本插值 def NewtonBaseInterpolation(x,n,list_x,list_y): list_dqy = GetDQyList(list_x,list_y) + print(list_dqy) result = list_dqy[0][0] mul = 1 for i in range(1,n): @@ -65,7 +64,11 @@ def NewtonBaseInterpolation(x,n,list_x,list_y): return result if __name__ == '__main__': - print("三点牛顿前插 e^0.12 结果为%f, 截断误差%f" % NewtonForwardInterpolation(0.12, 2, list_x, list_y)) - print("四点牛顿前插 e^0.12 结果为%f, 截断误差%f" % NewtonForwardInterpolation(0.12, 3, list_x, list_y)) - print("三点牛顿基本插值 e^0.12 结果为%f" % NewtonBaseInterpolation(0.12, 2, list_x, list_y)) - print("四点牛顿基本插值 e^0.12 结果为%f" % NewtonBaseInterpolation(0.12, 3, list_x, list_y)) + + list_x = [0.0,0.2,0.4,0.6,0.8] #改成题干的数值##############################3 + list_y = [1.0,1.2214,1.4918,1.8221,2.2255] #改成题干的数值##############################3 + print("三点牛顿前插 e^0.12 结果为%f, 截断误差%f" % NewtonForwardInterpolation(0.12, 3, list_x, list_y)) + print("四点牛顿前插 e^0.12 结果为%f, 截断误差%f" % NewtonForwardInterpolation(0.12, 4, list_x, list_y)) + print("三点牛顿基本插值 e^0.12 结果为%f" % NewtonBaseInterpolation(0.12, 3, list_x, list_y)) + print("四点牛顿基本插值 e^0.12 结果为%f" % NewtonBaseInterpolation(0.12, 4, list_x, list_y)) + #70到73行,0.12改成题干的数值,n是点数################################ \ No newline at end of file