如何为 'Nonetype' 函数分配一个值?
How do I assign a a value to a 'Nonetype' function?
我一直在尝试使用 Runge-Kutta45 积分方法更新 space 中的一组粒子位置和速度,以获得某个时间步长的新状态。
最初,我创建了一个包含所有这些元素的数组并将它们组合 (y):
r_vec_1 = np.array([0, 0])
v_vec_1 = np.array([-np.sqrt(2), -np.sqrt(2)])
r_vec_2 = np.array([-1, 0])
v_vec_2 = np.array([np.sqrt(2) / 2, np.sqrt(2) / 2])
r_vec_3 = np.array([1, 0])
v_vec_3 = np.array([np.sqrt(2) / 2, np.sqrt(2) / 2])
y_0 = np.concatenate((r_vec_1, v_vec_1, r_vec_2, v_vec_2, r_vec_3, v_vec_3))
y = y_0
现在,我使用这个数组作为我的初始条件并创建了一个函数,该函数为我提供了一个名为 F(y) 的新函数,它是我的函数 y 的导数,用一组一阶 ODE 表示:
def fun(t,y):
np.array([y[2], y[3], x1_double_dot(y, mass_vector), y1_double_dot(y, mass_vector),
y[6], y[7], x2_double_dot(y, mass_vector), y2_double_dot(y, mass_vector),
y[10], y[11], x3_double_dot(y, mass_vector), y3_double_dot(y, mass_vector)])
获得新函数文件后,我使用了初始时间和最终时间以及 scipy.integrate.RK45 子例程所需的时间步长,得到以下代码:
#Time start, step, and finish point
t_0 = 0
t = 0
t_step = 0.01
t_final = 200
nsteps = int((t_final - t)/t_step)
#The loop for running the Runge-Kutta method over some time period.
for step in np.linspace(t, t_final, num = nsteps):
y_new = sp.integrate.RK45(fun(t,y), t_0, y_0, t_final,vectorized=True)
history.append(y_new)
y_new = y
t += dt
history = np.array(history)
问题是,一旦我 运行 代码,我希望函数 y 更新到新状态并在一段时间内保持积分,直到经过。但是,在 运行 之后,我收到以下错误消息:
Traceback (most recent call last):
File "C:\Users\RSlat\PycharmProjects\pythonProject\PracticeBP Calculator.py", line 68, in <module>
y_new = sp.integrate.RK45(fun(t,y), t_0, y_0, t_final,vectorized=True)
File "C:\Users\RSlat\PycharmProjects\pythonProject\Practice\lib\site-packages\scipy\integrate\_ivp\rk.py", line 94, in __init__
self.f = self.fun(self.t, self.y)
File "C:\Users\RSlat\PycharmProjects\pythonProject\Practice\lib\site-packages\scipy\integrate\_ivp\base.py", line 138, in fun
return self.fun_single(t, y)
File "C:\Users\RSlat\PycharmProjects\pythonProject\Practice\lib\site-packages\scipy\integrate\_ivp\base.py", line 125, in fun_single
return self._fun(t, y[:, None]).ravel()
File "C:\Users\RSlat\PycharmProjects\pythonProject\Practice\lib\site-packages\scipy\integrate\_ivp\base.py", line 20, in fun_wrapped
return np.asarray(fun(t, y), dtype=dtype)
TypeError: 'NoneType' object is not callable
如有任何帮助,我们将不胜感激。谢谢,祝你有美好的一天!
显然(根据 https://docs.scipy.org/doc/scipy/reference/generated/scipy.integrate.RK45.html)sp.integrate.RK45() 需要在第一个位置调用。
因此当你这样写时它应该可以工作:
sp.integrate.RK45(fun, t_0, y_0, t_final,vectorized=True)
如您所见,我只将函数(可调用)“fun”(无参数)赋予RK45。
我一直在尝试使用 Runge-Kutta45 积分方法更新 space 中的一组粒子位置和速度,以获得某个时间步长的新状态。
最初,我创建了一个包含所有这些元素的数组并将它们组合 (y):
r_vec_1 = np.array([0, 0])
v_vec_1 = np.array([-np.sqrt(2), -np.sqrt(2)])
r_vec_2 = np.array([-1, 0])
v_vec_2 = np.array([np.sqrt(2) / 2, np.sqrt(2) / 2])
r_vec_3 = np.array([1, 0])
v_vec_3 = np.array([np.sqrt(2) / 2, np.sqrt(2) / 2])
y_0 = np.concatenate((r_vec_1, v_vec_1, r_vec_2, v_vec_2, r_vec_3, v_vec_3))
y = y_0
现在,我使用这个数组作为我的初始条件并创建了一个函数,该函数为我提供了一个名为 F(y) 的新函数,它是我的函数 y 的导数,用一组一阶 ODE 表示:
def fun(t,y):
np.array([y[2], y[3], x1_double_dot(y, mass_vector), y1_double_dot(y, mass_vector),
y[6], y[7], x2_double_dot(y, mass_vector), y2_double_dot(y, mass_vector),
y[10], y[11], x3_double_dot(y, mass_vector), y3_double_dot(y, mass_vector)])
获得新函数文件后,我使用了初始时间和最终时间以及 scipy.integrate.RK45 子例程所需的时间步长,得到以下代码:
#Time start, step, and finish point
t_0 = 0
t = 0
t_step = 0.01
t_final = 200
nsteps = int((t_final - t)/t_step)
#The loop for running the Runge-Kutta method over some time period.
for step in np.linspace(t, t_final, num = nsteps):
y_new = sp.integrate.RK45(fun(t,y), t_0, y_0, t_final,vectorized=True)
history.append(y_new)
y_new = y
t += dt
history = np.array(history)
问题是,一旦我 运行 代码,我希望函数 y 更新到新状态并在一段时间内保持积分,直到经过。但是,在 运行 之后,我收到以下错误消息:
Traceback (most recent call last):
File "C:\Users\RSlat\PycharmProjects\pythonProject\PracticeBP Calculator.py", line 68, in <module>
y_new = sp.integrate.RK45(fun(t,y), t_0, y_0, t_final,vectorized=True)
File "C:\Users\RSlat\PycharmProjects\pythonProject\Practice\lib\site-packages\scipy\integrate\_ivp\rk.py", line 94, in __init__
self.f = self.fun(self.t, self.y)
File "C:\Users\RSlat\PycharmProjects\pythonProject\Practice\lib\site-packages\scipy\integrate\_ivp\base.py", line 138, in fun
return self.fun_single(t, y)
File "C:\Users\RSlat\PycharmProjects\pythonProject\Practice\lib\site-packages\scipy\integrate\_ivp\base.py", line 125, in fun_single
return self._fun(t, y[:, None]).ravel()
File "C:\Users\RSlat\PycharmProjects\pythonProject\Practice\lib\site-packages\scipy\integrate\_ivp\base.py", line 20, in fun_wrapped
return np.asarray(fun(t, y), dtype=dtype)
TypeError: 'NoneType' object is not callable
如有任何帮助,我们将不胜感激。谢谢,祝你有美好的一天!
显然(根据 https://docs.scipy.org/doc/scipy/reference/generated/scipy.integrate.RK45.html)sp.integrate.RK45() 需要在第一个位置调用。
因此当你这样写时它应该可以工作:
sp.integrate.RK45(fun, t_0, y_0, t_final,vectorized=True)
如您所见,我只将函数(可调用)“fun”(无参数)赋予RK45。