Python 是否可以让scipy.integrate.odeint与一对向量(或向量向量)一起工作 使用scipy.integrate.odeint解决自由落体问题 进口 常数和积分间隔 速度矢量解

Python 是否可以让scipy.integrate.odeint与一对向量(或向量向量)一起工作 使用scipy.integrate.odeint解决自由落体问题 进口 常数和积分间隔 速度矢量解,python,scipy,ipython,vectorization,Python,Scipy,Ipython,Vectorization,在这里,我们返回一个速度向量,设置初始V0,积分1秒。我们期望Vx和Vy是常数,Vz以-G的斜率线性减小 def velocity_vector (x, t, params): # x = (Vx, Vy, Vz) # Ordinary differential equation - velocity of an object in frictionless free-fall. g = params acceleration = np.array ([0, 0,

在这里,我们返回一个速度向量,设置初始V0,积分1秒。我们期望Vx和Vy是常数,Vz以-G的斜率线性减小

def velocity_vector (x, t, params):
    # x = (Vx, Vy, Vz)
    # Ordinary differential equation - velocity of an object in frictionless free-fall.
    g = params
    acceleration = np.array ([0, 0, -g])
    return acceleration

v0 = np.array ([1, 2, 0])
soln = odeint (velocity_vector, v0, t, args = (G,))

fig = plt.figure (1, figsize = (8,8))

ax1 = fig.add_subplot(311)
ax1.plot(t, soln [:,0])
ax1.set_xlabel ('time')
ax1.set_ylabel ('Vx')

ax1 = fig.add_subplot(312)
ax1.plot(t, soln [:,1])
ax1.set_xlabel ('time')
ax1.set_ylabel ('Vy')

ax1 = fig.add_subplot(313)
ax1.plot(t, soln [:,2])
ax1.set_xlabel ('time')
ax1.set_ylabel ('Vz')

plt.show ()
def motion (x, t, params):
    # x = (Sx, Vx)
    # Ordinary differential equation - velocity of an object in frictionless free-fall.
    g = params
    acceleration = np.array ([-g * t, -g])
    return acceleration

v0 = np.array ([5.0, 0])
soln = odeint (motion, v0, t, args = (G,))

fig,axes = plt.subplots(1, 2) # one row, two columns
fig.subplots_adjust(wspace=0.6)

axes[0].plot(t, soln [:,0])
axes[0].set_xlabel ('time')
axes[0].set_ylabel ('Sz')

axes[1].plot(t, soln [:,1])
axes[1].set_xlabel ('time')
axes[1].set_ylabel ('Vz')

plt.show ()

一维位置和速度的矢量解 在这里,我们返回运动中z维的加速度和速度向量(),设置初始Z0和V0,并积分1秒。我们期望Xz是二次的,并且Vz以-G的斜率线性递减

def velocity_vector (x, t, params):
    # x = (Vx, Vy, Vz)
    # Ordinary differential equation - velocity of an object in frictionless free-fall.
    g = params
    acceleration = np.array ([0, 0, -g])
    return acceleration

v0 = np.array ([1, 2, 0])
soln = odeint (velocity_vector, v0, t, args = (G,))

fig = plt.figure (1, figsize = (8,8))

ax1 = fig.add_subplot(311)
ax1.plot(t, soln [:,0])
ax1.set_xlabel ('time')
ax1.set_ylabel ('Vx')

ax1 = fig.add_subplot(312)
ax1.plot(t, soln [:,1])
ax1.set_xlabel ('time')
ax1.set_ylabel ('Vy')

ax1 = fig.add_subplot(313)
ax1.plot(t, soln [:,2])
ax1.set_xlabel ('time')
ax1.set_ylabel ('Vz')

plt.show ()
def motion (x, t, params):
    # x = (Sx, Vx)
    # Ordinary differential equation - velocity of an object in frictionless free-fall.
    g = params
    acceleration = np.array ([-g * t, -g])
    return acceleration

v0 = np.array ([5.0, 0])
soln = odeint (motion, v0, t, args = (G,))

fig,axes = plt.subplots(1, 2) # one row, two columns
fig.subplots_adjust(wspace=0.6)

axes[0].plot(t, soln [:,0])
axes[0].set_xlabel ('time')
axes[0].set_ylabel ('Sz')

axes[1].plot(t, soln [:,1])
axes[1].set_xlabel ('time')
axes[1].set_ylabel ('Vz')

plt.show ()

一维位置和速度的矢量解 让我们看看是否可以通过提供向量列表并返回向量列表来组合这两种方法

def position_and_velocity (x, t, params):
    # x = (S, V) as vectors
    # Ordinary differential equation - velocity of an object in frictionless free-fall.
    g = params
    acceleration = np.array ([-g * t, -g])
    return acceleration

s = np.array ([0, 0, 5])
v = np.array ([1, 2, 0])
SV0 = np.array ([s, v])
soln = odeint (position_and_velocity, SV0, t, args = (G,))

#fig,axes = plt.subplots(1, 2) # one row, two columns
#fig.subplots_adjust(wspace=0.6)

#axes[0].plot(t, soln [:,0])
#axes[0].set_xlabel ('time')
#axes[0].set_ylabel ('Sz')

#axes[1].plot(t, soln [:,1])
#axes[1].set_xlabel ('time')
#axes[1].set_ylabel ('Vz')

#plt.show ()


---------------------------------------------------------------------------

ValueError                                Traceback (most recent call last)

<ipython-input-29-a12f336fc4fc> in <module>()
      9 v = np.array ([1, 2, 0])
     10 SV0 = np.array ([s, v])
---> 11 soln = odeint (position_and_velocity, SV0, t, args = (G,))
     12 
     13 #fig,axes = plt.subplots(1, 2) # one row, two columns


/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/scipy/integrate/odepack.py in odeint(func, y0, t, args, Dfun, col_deriv, full_output, ml, mu, rtol, atol, tcrit, h0, hmax, hmin, ixpr, mxstep, mxhnil, mxordn, mxords, printmessg)
    146     output = _odepack.odeint(func, y0, t, args, Dfun, col_deriv, ml, mu,
    147                              full_output, rtol, atol, tcrit, h0, hmax, hmin,
--> 148                              ixpr, mxstep, mxhnil, mxordn, mxords)
    149     if output[-1] < 0:
    150         print(_msgs[output[-1]])


ValueError: object too deep for desired array
def位置和速度(x、t、参数):
#x=(S,V)作为向量
#常微分方程-无摩擦自由落体中物体的速度。
g=参数
加速度=np.array([-g*t,-g])
返回加速度
s=np.array([0,0,5])
v=np.array([1,2,0])
SV0=np.array([s,v])
soln=odeint(位置和速度,SV0,t,args=(G,)
#图,轴=plt.子批次(1,2)#一行两列
#图子批次调整(wspace=0.6)
#轴[0]。绘图(t,soln[:,0])
#轴[0]。设置标签(“时间”)
#轴[0]。设置_ylabel('Sz'))
#轴[1]。绘图(t,soln[:,1])
#轴[1]。设置标签(“时间”)
#轴[1]。设置_ylabel('Vz')
#plt.show()
---------------------------------------------------------------------------
ValueError回溯(最近一次调用上次)
在()
9 v=np.array([1,2,0])
10 SV0=np.array([s,v])
--->11 soln=odeint(位置和速度,SV0,t,args=(G,)
12
13#图,轴=plt.子图(1,2)#一行两列
/odeint中的Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/scipy/integrate/odepack.py(func、y0、t、args、Dfun、col_deriv、full_输出、ml、mu、rtol、atol、tcrit、h0、hmax、hmin、ixpr、mxstep、mxhnil、mxordn、mxwords、printmessg)
146输出=_odepack.odeint(func、y0、t、args、Dfun、col_deriv、ml、mu、,
147全输出,rtol,atol,tcrit,h0,hmax,hmin,
-->148 ixpr、mxstep、mxhnil、mxordn、mxords)
149如果输出[-1]<0:
150个打印(_msgs[输出[-1]])
ValueError:对象对于所需数组太深

似乎scipy.integrate.odeint()可以处理向量并使用向量求解方程,但不能像本例中那样处理向量向量向量。这里有没有办法避免返回六个不同的函数,而不是两个?

只处理一维数组。要在组合系统上使用
odeint
,必须将两个三维向量连接成一个6维向量。如果您试图使用两个现有函数来计算两个三维系统方程的右侧,则必须创建一个新函数,该函数接受一个6-d向量,将其拆分为相应的3-d子向量,调用两个现有函数,然后将结果连接为6-d向量返回。

正如Warren提到的,答案是odeint确实需要1-d数组。诀窍是设置传递给odeint的函数,以便将传递的一维数组转换为所需的向量形式的第二个数组-在本例中为2个三维向量,以向量形式执行计算,然后将结果重塑为一维数组

答案演示了使用方便的numpy整形功能的技术

def position_and_velocity (x, t, params):
    # x = (S, V) as vectors
    # Ordinary differential equation - velocity of an object in frictionless free-fall.
    G = params
    g = np.array ([0, 0, -G])
    # convert the 6 element vector to 2 3 element vectors of displacement and velocity
    # to use vector formulation of the math
    s,v = x.reshape (2,3)
    acceleration = np.array ([v * t, g])
    # reshape the two vector results back into one for odeint
    return np.reshape (acceleration, 6)

s = np.array ([0, 0, 5])
v = np.array ([40, 10, 0])
SV0 = np.array ([s, v])
# pass reshaped displacement and velocity vector to odeint
soln = odeint (position_and_velocity, np.reshape (SV0, 6), t, args = (G,))

如果有人知道如何添加图形,我可以尝试添加两个.png文件。