TypeError:'numpy.ndarray'对象不可调用,它从哪里获取数组?[关闭]

jjjwad0x  于 2023-05-17  发布在  其他
关注(0)|答案(2)|浏览(101)

**关闭。**此题需要debugging details。目前不接受答复。

编辑问题以包含desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem。这将帮助其他人回答这个问题。
4天前关闭。
Improve this question

def draw_PF(g, x0, nb, colors):    
    plt.plot([x0], [x0], color = colors, marker='o')
    for i in range(nb):
        x1 = g(x0)
        plt.plot([x0,x0], [x0,x1], color = colors)
        plt.plot([x0,x1], [x1,x1], color = colors)
        x0 = x1

x = np.linspace(0,1,999)
y1 = 1-np.sin(𝑥)
y2 = np.exp(-4*x)
y3 = 0.4 * np.exp(𝑥) - 0.25
y4 = 1/(1 + ((1-x)/x)**2)
y = x
y1_dev = -np.cos(x)
y2_dev = -4 * y2
y3_dev = 0.4*np.exp(x)
y4_dev = 2*((1 + ((1-x)/x)**2)**-2)*((1-x)/x**3)
functions = [y1,y2,y3,y4]
derivatives = [y1_dev,y2_dev,y3_dev,y4_dev]
iterations = [10,10,10,10]
colors = ["r","g","c","m"]
fixed_points = [0.5,0.3,0.3,0.5]
for i in range(len(functions)):
    plt.plot(x,functions[i])
    plt.plot(x,y)
    plt.show()
for j in range(len(derivatives)):
    plt.plot(x,derivatives[j])
    plt.show()

for k in range(len(functions)):
    dessiner_PF(functions[k],(fixed_points[k] - 0.1), iterations[k], couleurs[k])
    plt.show()
TypeError                                 Traceback (most recent call last)
Cell In[28], line 25
     22     plt.show()
     24 for k in range(len(functions)):
---> 25     draw_PF(functions[k],(fixed_points[k] - 0.1), iterations[k], colors[k])
     26     plt.show()

Cell In[27], line 15, in draw_PF(g, x0, nb, colors)
     13 plt.plot([x0], [x0], color = colors, marker='o')
     14 for i in range(nb):
---> 15     x1 = g(x0)
     16     plt.plot([x0,x0], [x0,x1], color = colors)
     17     plt.plot([x0,x1], [x1,x1], color = colors)

TypeError: 'numpy.ndarray' object is not callable``

'你好,我一直收到这个错误消息,但我不明白为什么...我试图实现一个Picard算法来找到固定点,但我一直有一个问题与我的调用,我特别不明白它从哪里采取的numpy数组...先谢谢你。”

dohp0rv5

dohp0rv51#

错误来自这个函数:

def draw_PF(g, x0, nb, colors):    
    plt.plot([x0], [x0], color = colors, marker='o')
    for i in range(nb):
        x1 = g(x0)  # error is here
        plt.plot([x0,x0], [x0,x1], color = colors)
        plt.plot([x0,x1], [x1,x1], color = colors)
        x0 = x1

g(x0)不起作用,因为g本身在代码中没有定义为函数。如果你做print(g),你会得到一个形状为(999,)numpy.ndarray。这是因为您定义了y1,它是在draw_PF中表示g的第一个对象,如下所示:

x = np.linspace(0,1,999)
y1 = 1-np.sin(x)

由于y1是一个numpy数组,它是不可调用的(也就是说,它的行为不像一个函数,因为它是一个数组),因此python返回错误numpy.ndarray' object is not callable。这同样适用于y2y3y4
让我知道这是否有帮助!

rhfm7lfc

rhfm7lfc2#

没有足够的信息来确定,但似乎g是一个np.ndarray,并且您正在尝试访问元素x0。在这种情况下,您希望g[x0]访问列表中的第x0个元素。

相关问题