Python 如何将值分配给';非类型';功能?

Python 如何将值分配给';非类型';功能?,python,arrays,numpy,scipy,runge-kutta,Python,Arrays,Numpy,Scipy,Runge Kutta,我一直在尝试使用Runge-Kutta45积分方法来更新空间中粒子的一组位置和速度,以在某个时间步获得新的状态 最初,我创建了一个包含所有这些元素的数组,并将它们组合在一起(y): 现在,我使用这个数组作为初始条件,创建了一个函数,它给了我一个新函数,称为F(y),它是函数y的导数,用一组一阶常微分表示: def fun(t,y): np.array([y[2], y[3], x1_double_dot(y, mass_vector), y1_double_dot(y, mass_vec

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

最初,我创建了一个包含所有这些元素的数组,并将它们组合在一起(y):

现在,我使用这个数组作为初始条件,创建了一个函数,它给了我一个新函数,称为F(y),它是函数y的导数,用一组一阶常微分表示:

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
回溯(最近一次呼叫最后一次):
文件“C:\Users\RSlat\PycharmProjects\pythonProject\Practice\3BP Calculator.py”,第68行,在
y_new=sp.integrate.RK45(fun(t,y),t_0,y_0,t_final,向量化=True)
文件“C:\Users\RSlat\PycharmProjects\pythonProject\Practice\lib\site packages\scipy\integrate\\u ivp\rk.py”,第94行,在uu init中__
self.f=self.fun(self.t,self.y)
文件“C:\Users\RSlat\PycharmProjects\pythonProject\Practice\lib\site packages\scipy\integrate\u ivp\base.py”,第138行
回归自我。快乐单身(t,y)
文件“C:\Users\RSlat\PycharmProjects\pythonProject\Practice\lib\site packages\scipy\integrate\\u ivp\base.py”,第125行,在fun\u single中
返回自我。_-fun(t,y[:,无]).ravel()
文件“C:\Users\RSlat\PycharmProjects\pythonProject\Practice\lib\site packages\scipy\integrate\\u ivp\base.py”,第20行,用fun\u包装
返回np.asarray(fun(t,y),dtype=dtype)
TypeError:“非类型”对象不可调用
任何帮助都将不胜感激。谢谢,祝你有一个美好的一天

显然(并且根据)sp.integrate.RK45()需要在第一个位置调用

因此,当您以这种方式编写时,它应该会起作用:

sp.integrate.RK45(fun, t_0, y_0, t_final,vectorized=True)

如您所见,我只将函数(可调用的)“fun”(无参数)赋予RK45。

不应该
fun(t,y)
返回什么?如果不返回任何内容,它将返回
None
。为什么要使用stepper类而不是实现时间循环的例程,
res=solve\u ivp(fun,[t0,tf],y0)
。然后
res.t
包含调整步骤的时间和
res.y
这些时间的值。这非常有效,非常感谢!!太好了:)我很高兴能帮上忙:)
sp.integrate.RK45(fun, t_0, y_0, t_final,vectorized=True)