Python:如何从 solve_ivp 解释 y
Python: how to interpret y from solve_ivp
这当然是一个微不足道的问题,但它阻止了我对 solve_ivp
和我目前正在培训的 scypy.integrate
的完整理解......
下图定义了我试图用 solve_ivp
:
解决的问题
因此,为了找到 y(t)
,我指定要积分的函数、初始值、时间跨度,然后我 运行 solve_ivp
,如代码如下:
# Function to integrate
def fun(t, u):
x1 = u[0] # "u": function to found / 4 components x1, x2, x3 and x4
x2 = u[1]
x3 = u[2]
x4 = u[3]
dx1_dt = 1 # "u'(t) = F(t,u(t))": derivatives of components
dx2_dt = x3
dx3_dt = x4
dx4_dt = np.exp(x1) + 5*x2 - x1*x3
return [dx1_dt, dx2_dt, dx3_dt, dx4_dt]
# Specify initial conditions
x1_0 = 0.0
x2_0 = 0.0
x3_0 = 0.0
x4_0 = 0.0
y_0 = np.array([x1_0, x2_0, x3_0, x4_0])
# Specify initial and final times
t0 = 0.0
tf = 10.0
t_span = np.array([t0, tf])
# Resolution
position = solve_ivp(fun, t_span, y_0, method='RK45', max_step=0.1)
现在,solve_ivp
returns 一个名为 y
的 ndarray
(在本例中,它将是 position.y
,形状为 (4, 104)
) 根据 scipy.integrate.solve_ivp
文档,给出了 "t 处解的值".
到目前为止,还不错。
我的问题只是:
在当前问题中,给出 y(t)
的值是什么:y[0], y[1], y[2]
或 y[3]
?据我了解solve_ivp
的工作原理,它应该是y[1]
,对应向量的第二行u(t)
。对吗?
是的,没错,position.y[1]
包含解函数 y(t)
的值。您还应该发现 position.y[0]
与 position.t
.
重合
如果您需要额外的值或更快的计算,请使用 teval
或 dense_output
选项。使用 max_step
参数,您将强制执行 100 多个内部步骤。如果没有那个和其他选项之一,内部步骤的数量会适应内部错误容限,而且通常会更少。然后从分段多项式插值函数中获得所需点的值,可以隐式地使用 teval=...
或显式地在 return 对象中包含“密集输出”插值函数作为 position.sol
.
这当然是一个微不足道的问题,但它阻止了我对 solve_ivp
和我目前正在培训的 scypy.integrate
的完整理解......
下图定义了我试图用 solve_ivp
:
因此,为了找到 y(t)
,我指定要积分的函数、初始值、时间跨度,然后我 运行 solve_ivp
,如代码如下:
# Function to integrate
def fun(t, u):
x1 = u[0] # "u": function to found / 4 components x1, x2, x3 and x4
x2 = u[1]
x3 = u[2]
x4 = u[3]
dx1_dt = 1 # "u'(t) = F(t,u(t))": derivatives of components
dx2_dt = x3
dx3_dt = x4
dx4_dt = np.exp(x1) + 5*x2 - x1*x3
return [dx1_dt, dx2_dt, dx3_dt, dx4_dt]
# Specify initial conditions
x1_0 = 0.0
x2_0 = 0.0
x3_0 = 0.0
x4_0 = 0.0
y_0 = np.array([x1_0, x2_0, x3_0, x4_0])
# Specify initial and final times
t0 = 0.0
tf = 10.0
t_span = np.array([t0, tf])
# Resolution
position = solve_ivp(fun, t_span, y_0, method='RK45', max_step=0.1)
现在,solve_ivp
returns 一个名为 y
的 ndarray
(在本例中,它将是 position.y
,形状为 (4, 104)
) 根据 scipy.integrate.solve_ivp
文档,给出了 "t 处解的值".
到目前为止,还不错。
我的问题只是:
在当前问题中,给出 y(t)
的值是什么:y[0], y[1], y[2]
或 y[3]
?据我了解solve_ivp
的工作原理,它应该是y[1]
,对应向量的第二行u(t)
。对吗?
是的,没错,position.y[1]
包含解函数 y(t)
的值。您还应该发现 position.y[0]
与 position.t
.
如果您需要额外的值或更快的计算,请使用 teval
或 dense_output
选项。使用 max_step
参数,您将强制执行 100 多个内部步骤。如果没有那个和其他选项之一,内部步骤的数量会适应内部错误容限,而且通常会更少。然后从分段多项式插值函数中获得所需点的值,可以隐式地使用 teval=...
或显式地在 return 对象中包含“密集输出”插值函数作为 position.sol
.