如何修复 scipy 的 odeint 函数中的 np.float64 not callable 错误?
How do I fix np.float64 not callable error in scipy's odeint function?
所以我试图用 scipy odeint 函数求解一组耦合的 ODE-s,并且错误:'numpy.float64' object is not callable 不断弹出。我已经对这个功能有了一些经验,但我无法弄清楚哪一部分是错误的。这是我的代码:
import numpy as np
import matplotlib.pyplot as plt
from scipy.integrate import odeint
def ODE(f, t):
"""Function containing the ODE-s, to be used in odeint"""
x, vx, y, vy = f
ax = -2.0*y**2*x(1 - x**2)*np.exp(-(x**2 + y**2))
ay = -2.0*x**2*y(1 - y**2)*np.exp(-(x**2 + y**2))
return np.array([vx, ax, vy, ay])
def trajectory(impactpar, speed):
"""Using odeint to calculate values"""
x0 = impactpar
vy = speed
y0 = -2
vx = 0
initvals = [x0, vx, y0, vy]
maxtime = 10 / speed
time = np.linspace(0, maxtime, 300)
result = odeint(ODE, initvals, time)
return result
"""setting initial conditions for trajectory()"""
a_x0 = np.random.randint(-9000, 9001, 1)[0]/1e4
a_vy = np.random.randint(1, 5000, 1)[0]/1e4
xt = trajectory(a_x0, a_vy)[0]
yt = trajectory(a_x0, a_vy)[2]
这是我收到的完整错误消息:
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-55-40f25e7b2ba0> in <module>
28 a_x0 = np.random.randint(-9000, 9001, 1)[0]/1e4
29 a_vy = np.random.randint(1, 5000, 1)[0]/1e4
---> 30 xt = trajectory(a_x0, a_vy)[0]
31 yt = trajectory(a_x0, a_vy)[2]
<ipython-input-55-40f25e7b2ba0> in trajectory(impactpar, speed)
22 maxtime = 10 / speed
23 time = np.linspace(0, maxtime, 300)
---> 24 result = odeint(ODE, initvals, time)
25 return result
26
E:\Coding\Anaconda\lib\site-packages\scipy\integrate\odepack.py in odeint(func, y0, t, args, Dfun, col_deriv, full_output, ml, mu, rtol, atol, tcrit, h0, hmax, hmin, ixpr, mxstep, mxhnil, mxordn, mxords, printmessg, tfirst)
242 full_output, rtol, atol, tcrit, h0, hmax, hmin,
243 ixpr, mxstep, mxhnil, mxordn, mxords,
--> 244 int(bool(tfirst)))
245 if output[-1] < 0:
246 warning_msg = _msgs[output[-1]] + " Run with full_output = 1 to get quantitative information."
<ipython-input-55-40f25e7b2ba0> in ODE(f, t)
9 """Function containing the ODE-s, to be used in odeint"""
10 x, vx, y, vy = f
---> 11 ax = -2.0*y**2*x(1 - x**2)*np.exp(-(x**2 + y**2))
12 ay = -2.0*x**2*y(1 - y**2)*np.exp(-(x**2 + y**2))
13 return np.array([vx, ax, vy, ay])
TypeError: 'numpy.float64' object is not callable
如果有人能帮助我,我将不胜感激
正如它所说的那样,浮点变量不是函数,因此不能被调用。您需要将 x(...)
替换为 x*(...)
。
所以我试图用 scipy odeint 函数求解一组耦合的 ODE-s,并且错误:'numpy.float64' object is not callable 不断弹出。我已经对这个功能有了一些经验,但我无法弄清楚哪一部分是错误的。这是我的代码:
import numpy as np
import matplotlib.pyplot as plt
from scipy.integrate import odeint
def ODE(f, t):
"""Function containing the ODE-s, to be used in odeint"""
x, vx, y, vy = f
ax = -2.0*y**2*x(1 - x**2)*np.exp(-(x**2 + y**2))
ay = -2.0*x**2*y(1 - y**2)*np.exp(-(x**2 + y**2))
return np.array([vx, ax, vy, ay])
def trajectory(impactpar, speed):
"""Using odeint to calculate values"""
x0 = impactpar
vy = speed
y0 = -2
vx = 0
initvals = [x0, vx, y0, vy]
maxtime = 10 / speed
time = np.linspace(0, maxtime, 300)
result = odeint(ODE, initvals, time)
return result
"""setting initial conditions for trajectory()"""
a_x0 = np.random.randint(-9000, 9001, 1)[0]/1e4
a_vy = np.random.randint(1, 5000, 1)[0]/1e4
xt = trajectory(a_x0, a_vy)[0]
yt = trajectory(a_x0, a_vy)[2]
这是我收到的完整错误消息:
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-55-40f25e7b2ba0> in <module>
28 a_x0 = np.random.randint(-9000, 9001, 1)[0]/1e4
29 a_vy = np.random.randint(1, 5000, 1)[0]/1e4
---> 30 xt = trajectory(a_x0, a_vy)[0]
31 yt = trajectory(a_x0, a_vy)[2]
<ipython-input-55-40f25e7b2ba0> in trajectory(impactpar, speed)
22 maxtime = 10 / speed
23 time = np.linspace(0, maxtime, 300)
---> 24 result = odeint(ODE, initvals, time)
25 return result
26
E:\Coding\Anaconda\lib\site-packages\scipy\integrate\odepack.py in odeint(func, y0, t, args, Dfun, col_deriv, full_output, ml, mu, rtol, atol, tcrit, h0, hmax, hmin, ixpr, mxstep, mxhnil, mxordn, mxords, printmessg, tfirst)
242 full_output, rtol, atol, tcrit, h0, hmax, hmin,
243 ixpr, mxstep, mxhnil, mxordn, mxords,
--> 244 int(bool(tfirst)))
245 if output[-1] < 0:
246 warning_msg = _msgs[output[-1]] + " Run with full_output = 1 to get quantitative information."
<ipython-input-55-40f25e7b2ba0> in ODE(f, t)
9 """Function containing the ODE-s, to be used in odeint"""
10 x, vx, y, vy = f
---> 11 ax = -2.0*y**2*x(1 - x**2)*np.exp(-(x**2 + y**2))
12 ay = -2.0*x**2*y(1 - y**2)*np.exp(-(x**2 + y**2))
13 return np.array([vx, ax, vy, ay])
TypeError: 'numpy.float64' object is not callable
如果有人能帮助我,我将不胜感激
正如它所说的那样,浮点变量不是函数,因此不能被调用。您需要将 x(...)
替换为 x*(...)
。