【问题标题】:How do I assign a a value to a 'Nonetype' function?如何为“Nonetype”函数赋值?
【发布时间】:2021-05-10 13:12:35
【问题描述】:

我一直在尝试使用 Runge-Kutta45 积分方法来更新一组粒子在空间中的位置和速度,以便在某个时间步获得新的状态。

最初,我创建了一个包含所有这些元素的数组并将它们组合在一起 (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\Practice\3BP 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

任何帮助都将不胜感激。谢谢,祝您有美好的一天!

【问题讨论】:

  • 不应该fun(t,y)返回什么吗?如果它没有返回任何东西,它将返回None
  • 为什么使用步进类而不是实现时间循环的例程,res=solve_ivp(fun,[t0,tf],y0)。然后res.t 包含调整步骤的时间,res.y 包含这些时间的值。

标签: python arrays numpy scipy runge-kutta


【解决方案1】:

显然(根据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)

如您所见,我只将函数(可调用)“有趣”(不带参数)赋予 RK45。

【讨论】:

  • 效果很好,非常感谢!!
  • 太棒了 :) 我很高兴能帮上忙 :)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-02-07
  • 1970-01-01
  • 2018-05-10
  • 2011-08-13
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多