TypeError: fun() missing 1 required positional argument: 'a'

TypeError: fun() missing 1 required positional argument: 'a'

我知道有很多关于此错误的主题,我一直在研究其中的许多主题,试图了解 sush 一个简单系统的运行情况。这是我的代码,求解一个非常简单的方程来测试 solve_ivp 与 odeint 的效率。

import numpy as np 
import numpy as np 
import matplotlib.pyplot as plt
from scipy.integrate import solve_ivp


def fun(t,x,a) : 
    return -x/a


t = np.linspace(0,10,1000)
tspan = [t[0], t[-1]]


x0 = [10]

sol = solve_ivp(fun, tspan, x0, t_eval = t, args = 1)

plt.plot(t,sol.y.T)
plt.show()

这是完整的错误报告:

/home/anthony/.local/lib/python3.8/site-packages/scipy/integrate/_ivp/common.py:40:         
/home/anthony/.local/lib/python3.8/site-packages/scipy/integrate/_ivp/common.py:40:     
UserWarning: The following arguments have no effect for a chosen solver: `args`.
warn("The following arguments have no effect for a chosen solver: {}."
Traceback (most recent call last):
File "test.py", line 25, in <module>
sol = solve_ivp(fun, tspan, x0, t_eval = t, args = 1)
File "/home/anthony/.local/lib/python3.8/site-packages/scipy/integrate/_ivp/ivp.py", line 
477, in solve_ivp
solver = method(fun, t0, y0, tf, vectorized=vectorized, **options)
File "/home/anthony/.local/lib/python3.8/site-packages/scipy/integrate/_ivp/rk.py", line 
100, in __init__
self.f = self.fun(self.t, self.y)
File "/home/anthony/.local/lib/python3.8/site-packages/scipy/integrate/_ivp/base.py", line 
139, in fun
return self.fun_single(t, y)
File "/home/anthony/.local/lib/python3.8/site-packages/scipy/integrate/_ivp/base.py", line 
21, in fun_wrapped
return np.asarray(fun(t, y), dtype=dtype)
TypeError: fun() missing 1 required positional argument: 'a'

对我来说,错误很明显,但也很明显我根据这个求解器的文档把我的论点放在了正确的地方doc scipy.integrate.solve_ivp 我也升级到最新的 scipy 版本,任何建议都会很有帮助。

args 应该是一个元组

In [281]: sol = solve_ivp(fun, tspan, x0, t_eval = t, args = (1,))                                     
In [282]: sol                                                                                          
Out[282]: 
  message: 'The solver successfully reached the end of the integration interval.'
     nfev: 80
     njev: 0
      nlu: 0
      sol: None
   status: 0
  success: True
   ....

搞乱 args 是最常见的 scipy.integrate(和 optimize)SO 错误之一。