计算方法 | Lagrange 插值方法

很简单,先求l(x),再乘y加起来求个和。

 1 # data = eval(input("请输入数据:"))
 2 data = [(0.4,-0.916291), (0.5,-0.693147), (0.6,-0.510826), (0.7,-0.356675)]
 3 m = len(data)
 4 l = [0 for i in range(m)]
 5 x = 0.54
 6 _sum = 0
 7 # 求l表然后乘y的值
 8 for i in range(m):
 9     up = 1
10     down = 1
11     for j in range(m):
12         if j != i:
13             up = up * (x - data[j][0])
14             down = down * (data[i][0] - data[j][0])
15     l[i] = up / down
16     _sum += data[i][1] * l[i]
17     
18 print(_sum)

posted @ 2020-10-27 18:38  Mz1  阅读(86)  评论(0编辑  收藏  举报