【发布时间】:2021-12-14 20:00:02
【问题描述】:
我正在尝试创建一个拉格朗日插值函数,但是在构造之后我得到错误 index 9 is out of bounds for axis 0 with size 9。为什么会收到此错误,如何修复它以执行插值?
import numpy as np
b = np.arange(3,12)
y = np.arange(9)
from sympy import Symbol
t = Symbol('t')
d = len(b)
def interpolation(x, z):
if len(x) != len(z):
print("Error: the length of x and z is different")
else:
L = 0
for i in range (d+1):
p = 1
for j in range (d+1):
if j != i:
p *= (t-x[j]/(x[i] - x[j]))
L += z[i]*p
print(interpolation(b, y))
【问题讨论】:
-
索引从零开始
标签: python function numpy interpolation