将odeint系统转换为Solve

将odeint系统转换为Solve

本文介绍了将odeint系统转换为Solve_ivp,尺寸问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用solve_ivp求解微分方程组(6 x 6)。系统读取4个数组(形状为(8000,))作为输入,并将结果保存在形状相同的数组(8000,)中。我想重复同一系统的一部分(仅最后两个方程式)。问题是,当我对 odeint 进行相同操作时,最终结果的长度(theta_i和theta_deg_i)为8000。现在,因为参数是用相反的写法order insolve_ivp中,结果的长度为6。如何解决该问题?

I use solve_ivp to solve a system of differential equations (6 x 6). The system reads 4 arrays (with shape (8000, ) ) as inputs and saves the results in arrays with the same shape (8000, ). I want to repeat a part of the same system (only the last 2 equations). The problem is that, when I was doing the same with odeint the length of my final results (theta_i and theta_deg_i) was 8000. Now, because the arguments are written with the opposite order in solve_ivp, the length of my results is 6. How can I fix that?

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版本中,最后两行是:

the last two lines, in odeint version are:

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解决方案中ky的形状为(6,8000),而odeint解中的k的形状为(8000,6)。我是python的新手,在Ubuntu 16.04 LTS上使用python 2.7.12。先感谢您。

The shape of k.y in solve_ivp solution is (6, 8000), while the shape of k in odeint solution is (8000, 6). I am new to python and I use python 2.7.12 on Ubuntu 16.04 LTS. Thank you in advance.

推荐答案

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

I located the problem in the dimensions of the array in which each function saves the results. With the solve_ivp solution the k.y array has shape (6,8000), while the shape of the array k in odeint solution is (8000,6). I just added some lines in order to transpose the array before I repeated the system.

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)]

注意:转置函数会像这样更改数组的维数:

Note: The transpose function changes the dimension of an array like this:

([[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)

这篇关于将odeint系统转换为Solve_ivp,尺寸问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 22:25