[英]“'numpy.ndarray' object is not callable”
我的代码有问题。 我想为一个模型的参考解决方案,该模型将描述熊和鳟鱼的种群及其相互联系。
我找不到我做错了什么,但是python调用了三行有问题的代码,并以错误“ numpy.ndarray”对象不可调用为结尾。 我究竟做错了什么?
import matplotlib.pyplot as plt
import numpy as np
from scipy.interpolate import interp1d
def referenz_loesung(f, ref_loeser, x0, t0, T):
print "Berechne Referenzloesung..."
h_ref = 0.0001
#it mentions the following line as problematic
t_punkte, x_punkte = ref_loeser(f, x0, t0, T, h_ref) #it mentions this line as problematic
print "Erstelle Funktionen aus Referenzloesungen mit Hilfe von Splines."
a = interp1d(t_punkte, x_punkte[: , 0])
b = interp1d(t_punkte, x_punkte[: , 1])
return a, b
if __name__ == "__main__":
def f(x, t): # Funktion aus vorherigen Uebungen der DGL: x'(t) = f(x(t), t)
y = np.zeros(2)
y[0] = -2*x[0] + 10 * x[1]
y[1] = -x[0]
return y
x0 = np.array([1., 0.])
t0 = 0
T = 10
h = 0.5
from unsersolver import integriere
#it mentions the following line as problematic
t_punkte, x_werte = integriere (f, x0, t0, T, h)
from implEuler import integriere as ref_loeser
#it mentions the following line as problematic
x1_ref, x2_ref = referenz_loesung(f,ref_loeser, x0, t0, T)
ref_punkte = np.linspace(t0, T, 1000)
from woche1_1_m import auswerten_nx1
ref_werte1 = auswerten_nx1(x1_ref, ref_punkte)
ref_werte2 = auswerten_nx1(x2_ref, ref_punkte)
我导入的内容来自前几周的其他文件,这些文件实际上都能正常工作。 知道我能做什么吗? 我刚开始学习python,所以我真的没有任何线索。 谢谢!
如果可以将代码发布给integriere()或ref_loeser(),则将更为有用。 有了这些信息,我就请您尝试追溯到问题的根源:
问题的开始是t_punkte, x_werte = integriere(f, x0, t0, T, h)
,您说这会产生一个numpy错误,并且由于您要传递给此的唯一numpy参数是x0,因此建议您检查x在integriere()
代码中的用法。
接下来,我建议不要将函数作为参数传递,我认为这不是标准约定。 您可以在refrenz_loesung()
函数调用之外定义def ref_loeser(parameters..)
,然后像您一样调用它。
并且由于referenz_loesung()
调用ref_loeser()
与integriere()
相同, integriere()
我强烈建议您检查此函数中的代码。
祝好运!
x_lsg = berechne_nullstelle(x_werte(n-1))
<---错误
x_lsg = berechne_nullstelle(x_werte[n-1])
有效吗?
x_werte是一个ND数组,您需要访问n-1个元素,而应使用x_werte [n-1]。 ()用于函数调用,因此python解释器假定您正在调用numpy.ndarry,因此抛出该错误
如果将x0()
放入代码中,则会出现此错误。 没有其他模块的来历,我们不可能说出真正的错误是什么。
我最好的猜测:您是否有可能以错误的顺序integriere
了要进行参数integriere
的参数? 如果是这样,则该函数可能会尝试调用该参数,并期望它是一个函数,但是由于您已在该参数中放置了一个numpy数组,因此它会被“调用”?
因此,这是implEuler文件的代码。 “解题者”应该是正确的,因为它是老师给我的。
import matplotlib.pyplot as plt
import numpy as np
from newton import berechne_nullstelle
def integriere(f, x0, t0, T, h):
d = len(x0)
t_punkte = np.arange(t0, T, h)
N = len(t_punkte)
x_werte = np.zeros ((N, d))
x_werte[0] = x0
for n in range(1, N):
def f_implEuler(x):
x_werte^(n-1) + h * f(x_werte, t_punkte^(n)) - x_werte
x_lsg = berechne_nullstelle(x_werte(n-1))
x_werte[n] = x_lsg
print integriere
这有什么帮助吗?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.