IndexError: tuple index out of range, error when trying to integrate the fucntion

IndexError: tuple index out of range, error when trying to integrate the fucntion

我正在尝试计算函数 f_integ 的值,它是函数 f 从 0 积分到 x_v 的结果。

   f = lambda x : x + 1

    def f_integ(x_array):
        int_result = np.zeros_like(x_array)
        indexes = np.shape(x_array)[0]
        for ind in range(indexes):
            x_v = x_array[ind]
            int_result[ind] = integ.quad(f, 0.0, x_v)[0]
        return int_result

    C = f_integ(1)
    print "C", C

当我 运行 这样做时,出现以下错误:

Traceback (most recent call last):
  File "untitled.py", line 117, in <module>
    C = f_integ(1)
  File "scr1.py", line 110, in f_integ
    indexes = np.shape(x_array)[0]
IndexError: tuple index out of range

我知道 quad() returns 是一个元组,但我不知道如何在积分结果中将数字作为参数。我是 Python 的新手,请帮忙。

像这样调用函数:

C = f_integ(np.array([1]))
print "C", C

目前您正在将数字传递给 f_integ(),而不是数组。当它遇到 np.shape(x_array)[0] 时,数字的形状只是 (),因此对于空元组,它不能 return 索引 0 处的任何内容。