使用隐式方法时scipy.solve_ivp中的奇怪行为

时间:2019-02-21 15:27:32

标签: python python-3.x scipy

我最近遇到一个有关集成的问题,遇到了一个奇怪的错误。我尝试使用solve_ivp来解决一个非常简单的问题:

from scipy.integrate import solve_ivp
import numpy as np

def f(y, t):
    return y

y0 = [1,1,1,1]
method = 'RK23'
s = solve_ivp(f, (0,1), y0, method=method, t_eval=np.linspace(0,1))

它工作正常。当我更改为method='BDF'method='Radau'时收到错误消息:

Traceback (most recent call last):

  File "<ipython-input-222-f11c4406e92c>", line 10, in <module>
    s = solve_ivp(f, (0,1), y0, method=method, t_eval=np.linspace(0,1))

  File "C:\ProgramData\Anaconda3\lib\site-packages\scipy\integrate\_ivp\ivp.py", line 455, in solve_ivp
    solver = method(fun, t0, y0, tf, vectorized=vectorized, **options)

  File "C:\ProgramData\Anaconda3\lib\site-packages\scipy\integrate\_ivp\radau.py", line 299, in __init__
    self.jac, self.J = self._validate_jac(jac, jac_sparsity)

  File "C:\ProgramData\Anaconda3\lib\site-packages\scipy\integrate\_ivp\radau.py", line 345, in _validate_jac
    J = jac_wrapped(t0, y0, self.f)

  File "C:\ProgramData\Anaconda3\lib\site-packages\scipy\integrate\_ivp\radau.py", line 343, in jac_wrapped
    sparsity)

  File "C:\ProgramData\Anaconda3\lib\site-packages\scipy\integrate\_ivp\common.py", line 307, in num_jac
    return _dense_num_jac(fun, t, y, f, h, factor, y_scale)

  File "C:\ProgramData\Anaconda3\lib\site-packages\scipy\integrate\_ivp\common.py", line 318, in _dense_num_jac
    diff = f_new - f[:, None]

IndexError: too many indices for array

我也遇到method = 'LSODA'错误,尽管有所不同(即所有隐式积分器)。任何明确的集成商都没有出现错误。

我在scipy版本1.0.0和google colab(scipy版本1.1.0)的spyder中进行了尝试,结果相同。

这是一个错误还是我缺少一些隐式积分器需要的参数?

1 个答案:

答案 0 :(得分:0)

似乎Radau和BDF方法不处理单值RHS函数。使上面的函数git status输出一维列表可以解决您的问题。此外,如Weckesser在评论中所述,f期望RHS为solve_ivp,而不是f(t, y)

f(y, t)