Python中的拉格朗日插值
作者:互联网
我想用Lagrange方法插值多项式,但这段代码不起作用:
def interpolate(x_values, y_values):
def _basis(j):
p = [(x - x_values[m])/(x_values[j] - x_values[m]) for m in xrange(k + 1) if m != j]
return reduce(operator.mul, p)
assert len(x_values) != 0 and (len(x_values) == len(y_values)), 'x and y cannot be empty and must have the same length'
k = len(x_values)
return sum(_basis(j) for j in xrange(k))
我跟着Wikipedia,但是当我运行它时,我在第3行收到一个IndexError!
谢谢
最佳答案:
检查索引,维基百科说“k 1数据点”,但你设置k = len(x_values),它应该是k = len(x_values) – 如果你完全遵循公式,则为1.
标签:python,interpolation,polynomial-math 来源: https://codeday.me/bug/20190515/1110292.html