计算方法 | 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)
本文来自博客园,作者:Mz1,转载请注明原文链接:https://www.cnblogs.com/Mz1-rc/p/13886502.html
如果有问题可以在下方评论或者email:mzi_mzi@163.com