【问题标题】:Converting odeint system to solve_ivp, dimensions problem将odeint系统转换为solve_ivp,维度问题
【发布时间】:2019-02-23 15:44:22
【问题描述】:

我使用 solve_ivp 求解微分方程组 (6 x 6)。系统读取 4 个数组(形状为 (8000, ))作为输入,并将结果保存在具有相同形状 (8000, ) 的数组中。我想重复同一个系统的一部分(只有最后两个方程)。问题是,当我对odeint 做同样的事情时,我的最终结果(theta_i 和 theta_deg_i)的长度是 8000。现在,因为在solve_ivp 中参数的顺序相反,所以我的结果长度是6 . 我该如何解决?

t = np.linspace(0,100,8000) # s

xdot = np.array(.....) # shape (8000, )
ydot = np.array(.....)
xdotdot = np.array(.....)
ydotdot = np.array(.....)

interp = interp1d(t,(xdot,ydot,xdotdot,ydotdot))


def inverse(t,k):
    vcx_i = k[0]
    vcy_i = k[1]
    psi_i = k[2]
    wz_i = k[3]
    theta_i = k[4]
    theta_deg_i = k[5]

    # equations of the system...

    return [vcxdot_i, vcydot_i, psidot_i, wzdot_i, theta_i, theta_deg_i]


k0 = [0.1257, 0, 0, 0, 0, 0]

steps = 1
method = 'RK23'
atol = 1e-3
k = solve_ivp(inverse, (0, 100), k0, method=method, t_eval=t, atol=atol, vectorized=True)


vcx_i = k.y[0,:]
vcy_i = k.y[1,:]
psi_i = k.y[2,:]
wz_i = k.y[3,:]
theta_i = k.y[4,:]
theta_deg_i = k.y[5,:]

theta_i = [inverse(t_i, k_i)[4] for t_i, k_i in zip(t, k.y)]
theta_deg_i = [inverse(t_i, k_i)[5] for t_i, k_i in zip(t, k.y)]

最后两行,在 odeint 版本中是:

theta_i = [inverse(k_i, t_i)[4] for t_i, k_i in zip(t, k)]
theta_deg_i = [inverse(k_i, t_i)[5] for t_i, k_i in zip(t, k)]

solve_ivp解中k.y的形状是(6, 8000),而odeint解中k的形状是(8000, 6)。我是 python 新手,我在 Ubuntu 16.04 LTS 上使用 python 2.7.12。先感谢您。

【问题讨论】:

    标签: python-2.7 scipy dimensions odeint


    【解决方案1】:

    我将问题定位在每个函数保存结果的数组维度中。使用 solve_ivp 解决方案,k.y 数组的形状为 (6,8000),而 odeint 解决方案中的数组 k 的形状为 (8000,6)。在重复系统之前,我只是添加了一些行以便转置数组。

    k_new = np.transpose(k.y) # antistrofi diastasewn k.y apo (6,8000) se (8000,6) 
    theta_i = [inverse(t_i, k_i)[4] for t_i, k_i in zip(t, k_new)]
    theta_deg_i = [inverse(t_i, k_i)[5] for k_i, t_i in zip(k_new, t)]
    

    注意:转置函数像这样改变数组的维度:

    ([[1,  2,  3,  4,  5]              ([[1,10,100]
      [10, 20, 30, 40, 50]     --->      [2,20,200]
      [100,200,300,400,500]])            [3,30,300]
                                         [4,40,400]
                                         [5,50,500]])
      # with shape (3,5)             # with shape(5,3)
    

    【讨论】:

      猜你喜欢
      • 2019-04-24
      • 2018-06-18
      • 2021-02-15
      • 2019-09-30
      • 2013-05-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多