繁体   English   中英

用scipy odeint解决python中的向量常微分方程

[英]Solving vector ordinary differential equations in python with scipy odeint

我正试图解决和涉及向量的颂歌,并且不能提出可行的答案。 所以我将它分成6个分量,一个用于组件的每个时间导数,一个用于速度分量的每个时间导数。 第一个值似乎是合理的,然后它跳到数百万的数字,我不知道为什么。 老实说,我真的不确定如何做到这一点,我现在只是试一试。 我似乎无法在网上找到任何信息,如果有这类问题的例子,可以使用一些帮助或一些链接。 任何信息将非常感谢如何解决这个问题。

def dr_dt(y, t):
"""Integration of the governing vector differential equation.
d2r_dt2 = -(mu/R^3)*r with d2r_dt2 and r as vecotrs.
Initial position and velocity are given.
y[0:2] = position components
y[3:] = velocity components"""

    G = 6.672*(10**-11)
    M = 5.972*(10**24)
    mu = G*M
    r = np.sqrt(y[0]**2 + y[1]**2 + y[2]**2) 

    dy0 = y[3]
    dy1 = y[4]
    dy2 = y[5]
    dy3 = -(mu / (r**3)) * y[0]
    dy4 = -(mu / (r**3)) * y[1]
    dy5 = -(mu / (r**3)) * y[2]
    return [dy0, dy3, dy1, dy4, dy2, dy5]

解决这个问题后,我想绘制它。 它应该是一个椭圆形,但说实话,我也不确定如何做到这一点。 我正考虑采取位置的大小,然后随着时间绘制它。 如果有更好的方法,请随时告诉我。

谢谢。

首先,如果你的y[:3]是位置而y[3:]是速度,那么dr_dt函数应该按照这个顺序返回组件。 其次,为了绘制轨迹,我们可以使用优秀的matplotlib mplot3d模块,或者省略位置和速度的第z个分量(因此我们的运动在XY平面上),并绘制yx 示例代码(具有更正的返回值顺序)如下:

import numpy as np
import matplotlib.pyplot as plt
from scipy.integrate import odeint

def dr_dt(y, t):
    """Integration of the governing vector differential equation.
    d2r_dt2 = -(mu/R^3)*r with d2r_dt2 and r as vecotrs.
    Initial position and velocity are given.
    y[0:2] = position components
    y[3:] = velocity components"""

    G = 6.672*(10**-11)
    M = 5.972*(10**24)
    mu = G*M
    r = np.sqrt(y[0]**2 + y[1]**2 + y[2]**2).

    dy0 = y[3]
    dy1 = y[4]
    dy2 = y[5]
    dy3 = -(mu / (r**3)) * y[0]
    dy4 = -(mu / (r**3)) * y[1]
    dy5 = -(mu / (r**3)) * y[2]
    return [dy0, dy1, dy2, dy3, dy4, dy5]

t = np.arange(0, 100000, 0.1)
y0 = [7.e6, 0., 0., 0., 1.e3, 0.]
y = odeint(dr_dt, y0, t)
plt.plot(y[:,0], y[:,1])
plt.show()

这会产生一个很好的椭圆:

在此输入图像描述

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM