已知5个互异的节点xi=0.40、0.55、0.65、0.8、0.9,以及相应的函数值yi=0.537、0.685、0.789、0.919、1.26,试根据差商型的牛顿插值公式,编程计算由用户所输入的某插值点x所对应的函数值。
时间: 2023-12-28 07:52:25 浏览: 258
根据牛顿插值公式,我们可以先计算出各阶差商:
f[x0,x1] = (f[x1]-f[x0])/(x1-x0) = (0.685-0.537)/(0.55-0.40) = 1.480
f[x1,x2] = (f[x2]-f[x1])/(x2-x1) = (0.789-0.685)/(0.65-0.55) = 1.040
f[x2,x3] = (f[x3]-f[x2])/(x3-x2) = (0.919-0.789)/(0.8-0.65) = 1.450
f[x3,x4] = (f[x4]-f[x3])/(x4-x3) = (1.26-0.919)/(0.9-0.8) = 3.410
然后再计算出插值点对应的函数值:
f[x0,x1,x2] = (f[x1,x2]-f[x0,x1])/(x2-x0) = (1.040-1.480)/(0.65-0.40) = -0.960
f[x1,x2,x3] = (f[x2,x3]-f[x1,x2])/(x3-x1) = (1.450-1.040)/(0.8-0.55) = 1.125
f[x2,x3,x4] = (f[x3,x4]-f[x2,x3])/(x4-x2) = (3.410-1.450)/(0.9-0.65) = 5.727
最终的插值公式为:
P(x) = 0.537 - 0.960(x-0.40) + 1.125(x-0.40)(x-0.55) + 5.727(x-0.40)(x-0.55)(x-0.65)
输入某插值点x,即可计算出对应的函数值P(x)。以下是Python代码实现:
```python
def newton_interpolation(x, nodes, values):
n = len(nodes)
# 计算各阶差商
diffs = [values]
for i in range(1, n):
diffs.append([])
for j in range(n-i):
diffs[i].append((diffs[i-1][j+1] - diffs[i-1][j]) / (nodes[j+i] - nodes[j]))
# 计算插值多项式
p = values[0]
for i in range(1, n):
term = diffs[i][0]
for j in range(i):
term *= (x - nodes[j])
p += term
return p
# 测试
nodes = [0.40, 0.55, 0.65, 0.8, 0.9]
values = [0.537, 0.685, 0.789, 0.919, 1.26]
x = float(input("请输入插值点x的值:"))
print("插值点对应的函数值为:", newton_interpolation(x, nodes, values))
```
阅读全文