我正在嘗試制作一個拉格朗日插值函式,但是在構建之后我得到錯誤索引 9 超出了軸 0 的范圍,大小為 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))
uj5u.com熱心網友回復:
因為第一個索引是零,所以你只能轉到索引 8,然后 9 越界。你的 9 個索引是 0, 1, 2, 3, 4, 5, 6, 7, 8。
所以你不應該遍歷 d 1。只使用 d。
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/342955.html
