111
This commit is contained in:
21
69-3.py
21
69-3.py
@@ -1,8 +1,5 @@
|
|||||||
import math
|
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):
|
def FxDiff_n(x,n):
|
||||||
return math.exp(x)
|
return math.exp(x)
|
||||||
@@ -22,15 +19,16 @@ def GetDyList(list_y):
|
|||||||
def NewtonForwardRegression(x,n,list_x):
|
def NewtonForwardRegression(x,n,list_x):
|
||||||
h = list_x[1] - list_x[0]
|
h = list_x[1] - list_x[0]
|
||||||
t = (x - list_x[0]) / h
|
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
|
result = 1
|
||||||
for i in range(n+1):
|
for i in range(n):
|
||||||
result *= (t-i)*h/(i+1)
|
result *= (t-i)*h/(i+1)
|
||||||
return result * ks
|
return result * ks
|
||||||
|
|
||||||
# 牛顿前插
|
# 牛顿前插
|
||||||
def NewtonForwardInterpolation(x,n,list_x,list_y):
|
def NewtonForwardInterpolation(x,n,list_x,list_y):
|
||||||
list_dyk = GetDyList(list_y)
|
list_dyk = GetDyList(list_y)
|
||||||
|
print(list_dyk)
|
||||||
h = list_x[1] - list_x[0]
|
h = list_x[1] - list_x[0]
|
||||||
t = (x - list_x[0]) / h
|
t = (x - list_x[0]) / h
|
||||||
result = list_y[0]
|
result = list_y[0]
|
||||||
@@ -57,6 +55,7 @@ def GetDQyList(list_x,list_y):
|
|||||||
# 牛顿基本插值
|
# 牛顿基本插值
|
||||||
def NewtonBaseInterpolation(x,n,list_x,list_y):
|
def NewtonBaseInterpolation(x,n,list_x,list_y):
|
||||||
list_dqy = GetDQyList(list_x,list_y)
|
list_dqy = GetDQyList(list_x,list_y)
|
||||||
|
print(list_dqy)
|
||||||
result = list_dqy[0][0]
|
result = list_dqy[0][0]
|
||||||
mul = 1
|
mul = 1
|
||||||
for i in range(1,n):
|
for i in range(1,n):
|
||||||
@@ -65,7 +64,11 @@ def NewtonBaseInterpolation(x,n,list_x,list_y):
|
|||||||
return result
|
return result
|
||||||
|
|
||||||
if __name__ == '__main__':
|
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))
|
list_x = [0.0,0.2,0.4,0.6,0.8] #改成题干的数值##############################3
|
||||||
print("三点牛顿基本插值 e^0.12 结果为%f" % NewtonBaseInterpolation(0.12, 2, list_x, list_y))
|
list_y = [1.0,1.2214,1.4918,1.8221,2.2255] #改成题干的数值##############################3
|
||||||
print("四点牛顿基本插值 e^0.12 结果为%f" % NewtonBaseInterpolation(0.12, 3, list_x, list_y))
|
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是点数################################
|
||||||
Reference in New Issue
Block a user