Error: index 9 is out of bounds for axis 0 with size 9
Error: index 9 is out of bounds for axis 0 with size 9
我正在尝试制作拉格朗日插值函数,但在构建后我收到错误消息 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))
因为第一个索引是零,所以你只能转到索引 8,然后 9 就超出了范围。你的 9 个索引是 0, 1, 2, 3, 4, 5, 6, 7, 8.
所以你不应该循环 d + 1。只使用 d.
我正在尝试制作拉格朗日插值函数,但在构建后我收到错误消息 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))
因为第一个索引是零,所以你只能转到索引 8,然后 9 就超出了范围。你的 9 个索引是 0, 1, 2, 3, 4, 5, 6, 7, 8.
所以你不应该循环 d + 1。只使用 d.