Python 通过RK45方法指定时间求值将覆盖timestep select吗?(scipy.integrate.solve_ivp)

Python 通过RK45方法指定时间求值将覆盖timestep select吗?(scipy.integrate.solve_ivp),python,scipy,runge-kutta,Python,Scipy,Runge Kutta,从 与 t_eval对于指定时间内的存储解决方案是可选的。 此选项是否会通过RK45覆盖时间步选择?它不会覆盖时间步。验证这一点的一种方法是使用densite\u output=True,它在每个时间步保存数据,以便以后进行插值 sol属性包含有关ts属性中的时间步长的附加信息。在这里,您可以看到使用t_eval会更改sol3.t的返回,但不会影响时间步长 import numpy as np from scipy.integrate import solve_ivp # To make r

t_eval
对于指定时间内的存储解决方案是可选的。
此选项是否会通过
RK45
覆盖时间步选择?

它不会覆盖时间步。验证这一点的一种方法是使用
densite\u output=True
,它在每个时间步保存数据,以便以后进行插值

sol
属性包含有关
ts
属性中的时间步长的附加信息。在这里,您可以看到使用
t_eval
会更改
sol3.t
的返回,但不会影响时间步长

import numpy as np
from scipy.integrate import solve_ivp

# To make readable
np.set_printoptions(precision=2)

method = 'RK45'

def dy(t, y):
    return y

sol = solve_ivp(dy, (0, 10), [1], method=method)
print(f"No options    : {sol.t}")
sol2 = solve_ivp(dy, (0, 10), [1], method=method, dense_output=True)
print(f"Dense output  : {sol2.t}")
print(f"  Interpolants: {sol2.sol.ts}")
t_eval = [5]
sol3 = solve_ivp(dy, (0, 10), [1], method=method, t_eval=t_eval, dense_output=True)
print(f"t_eval return : {sol3.t}")
print(f"  Interpolants: {sol3.sol.ts}")

返回

No options    : [ 0.    0.1   1.07  2.3   3.65  5.03  6.43  7.83  9.24 10.  ]
Dense output  : [ 0.    0.1   1.07  2.3   3.65  5.03  6.43  7.83  9.24 10.  ]
  Interpolants: [ 0.    0.1   1.07  2.3   3.65  5.03  6.43  7.83  9.24 10.  ]
t_eval return : [5]
  Interpolants: [ 0.    0.1   1.07  2.3   3.65  5.03  6.43  7.83  9.24 10.  ]
我温和地建议,不要使用
t\u eval
,而应该只使用
densite\u output=True
,然后在事后构造y\u eval。这是一种更加灵活和透明的用法

sol = solve_ivp(dy, (0, 10), [1], method=method, dense_output=True)
y_eval = sol.sol(t_eval)
简而言之 不!它不会覆盖RK45的时间步长,因为函数
scipy.integrate.solve_ivp
将为
t_eval
中的每个
t
使用计算值。RK45仍然使用自己的时间步长


很长的回答。 经过调查,我发现。 根据

在函数
的第156行,求解ivp
。第477行

solver = method(fun, t0, y0, tf, vectorized=vectorized, **options)
解算器不将
t\u eval
作为参数。 在第511行

t = solver.t
解算器返回它自己的
t

第545行

if solver.direction > 0:
            t_eval_i_new = np.searchsorted(t_eval, t, side='right')
            t_eval_step = t_eval[t_eval_i:t_eval_i_new]
其中
t\u eval\u i\u new
是使用
np在
t
之间的
t\u eval
的新索引。searchsorted
t\u eval\u step
是ode解算器步骤之间的
t\u eval
的时间步长

if t_eval_step.size > 0:
            if sol is None:
                sol = solver.dense_output()
            ts.append(t_eval_step)
            ys.append(sol(t_eval_step))
            t_eval_i = t_eval_i_new
这意味着我们将
t\u eval\u步骤
附加到
ts
并使用
solver.densite\u output()
插入步骤,并给出
t\u eval\u步骤中每个特定时间的近似值
在完成集成后的第585行,程序返回输出

 return OdeResult(t=ts, y=ys, sol=sol, t_events=t_events, nfev=solver.nfev,
                 njev=solver.njev, nlu=solver.nlu, status=status,
                 message=message, success=status >= 0)
 return OdeResult(t=ts, y=ys, sol=sol, t_events=t_events, nfev=solver.nfev,
                 njev=solver.njev, nlu=solver.nlu, status=status,
                 message=message, success=status >= 0)