【问题标题】:Error: index 9 is out of bounds for axis 0 with size 9错误:索引 9 超出轴 0 的范围,大小为 9
【发布时间】: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


【解决方案1】:

因为第一个索引是零,所以您只能转到索引 8,然后 9 超出范围。您的 9 个索引是 0、1、2、3、4、5、6、7、8。
所以你不应该循环 d + 1。只使用 d。

【讨论】:

    猜你喜欢
    • 2018-01-11
    • 2021-07-17
    • 2021-11-18
    • 2018-02-12
    • 2021-11-03
    • 2021-01-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多