Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/361.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Python中绘制轨道,但当x<;0,轨迹突然变为直线_Python_Trigonometry_Orbit - Fatal编程技术网

在Python中绘制轨道,但当x<;0,轨迹突然变为直线

在Python中绘制轨道,但当x<;0,轨迹突然变为直线,python,trigonometry,orbit,Python,Trigonometry,Orbit,我在一个简单的二维平面上画出了一个运动物体经过另一个引力吸引物体的路径 每次循环,时间增加一秒,计算并打印出身体的新位置。 然后我将结果粘贴到电子表格中,并将其绘制成图表 直到身体的x分量变为负值,然后轨迹变为线性,并快速移动到左上角,一切看起来都正常 这一切都是为了我自己的个人娱乐,我不是学生。所以,在挠头了一段时间后,我终于把向别人寻求帮助的事搞砸了。 我可能错过了一些显而易见的事情。我怀疑我的三角学缺少什么东西 我正在使用Python 2.7.10 import sys import o

我在一个简单的二维平面上画出了一个运动物体经过另一个引力吸引物体的路径

每次循环,时间增加一秒,计算并打印出身体的新位置。 然后我将结果粘贴到电子表格中,并将其绘制成图表

直到身体的x分量变为负值,然后轨迹变为线性,并快速移动到左上角,一切看起来都正常

这一切都是为了我自己的个人娱乐,我不是学生。所以,在挠头了一段时间后,我终于把向别人寻求帮助的事搞砸了。 我可能错过了一些显而易见的事情。我怀疑我的三角学缺少什么东西

我正在使用Python 2.7.10

import sys
import os
import math

mu = 4.0*(10**14)
massAst = 1
earthRadius = 6371000.
alt = 100000.
r = earthRadius+ alt
rTheta = 270.
rAngtoX = math.radians(rTheta)
tInc = 1 ## increment time by 1 seconds - one update of pos&velocity per second of travel
calcTime = 1100 ## simulation runtime (86400 seconds = 1 day) 10 mins
t = 1 ## integral of time t to use in the calcs in the loop.
printbell = 120 ## print results now
printclock = 0
hourCount = 0

## Initialise velocity vectors for Asteroid:
uAstX = 1500.
uAstY = 0.
vAstX = 0.
vAstY = 0.
## Displacement
dAstX = r*math.cos(rAngtoX)
dAstY = r*math.sin(rAngtoX)

for i in range(0, calcTime):
    acc = -1*(mu/r**2)

    accX = acc*math.cos(rAngtoX)
    accY = acc*math.sin(rAngtoX)

    vAstX = uAstX + accX*t ## new value for velocity in X direction
    vAstY = uAstY + accY*t ## and in Y

    deltaDAstX = uAstX*t + 0.5*accX*(t**2) ## change in position over this time interval
    deltaDAstY = uAstY*t + 0.5*accY*(t**2)

    dAstX = dAstX + deltaDAstX
    dAstY = dAstY + deltaDAstY

    uAstX = vAstX
    uAstY = vAstY
    ## Now calculate new angle and range
    ## tan(theta) = dAstY/dAstX, so:
    rAngtoX = math.atan(dAstY/dAstX) ##+(2*3.141592654)
    ##print 'theta:', math.degrees(rAngtoX)
    r = dAstY/math.sin(rAngtoX)
    ## if i == print
    print dAstX, '  ', dAstY

dAstX
接近零时,
dAstY/dAstX
将接近一个被零除的数。。。这将导致各种各样的问题(至少是取舍问题)


我建议保持距离/速度/加速度的x/y分量分开。当然,物体之间的距离很重要,但这可以通过
r=sqrt(dAstX**2+dAstY**2)
来计算。我不熟悉数学,但我把你的代码修改为在我的机器上运行,使用
seaborn
库绘制数据,我得出了以下结论:

这是我使用的代码:

import math
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt


def calc(calcTime):
    mu = 4.0*(10**14)
    earthRadius = 6371000.0
    alt = 100000.0
    r = earthRadius+ alt
    rTheta = 270.0
    rAngtoX = math.radians(rTheta)
    t = 1               # integral of time t to use in the calcs in the loop.

    # Initialise velocity vectors for Asteroid:
    uAstX = 1500.0
    uAstY = 0.0
    # Displacement
    dAstX = r*math.cos(rAngtoX)
    dAstY = r*math.sin(rAngtoX)

    point_list = []
    for i in range(0, calcTime):
        acc = -1*(mu/r**2)

        accX = acc*math.cos(rAngtoX)
        accY = acc*math.sin(rAngtoX)

        vAstX = uAstX + accX*t                  # new value for velocity in X direction
        vAstY = uAstY + accY*t                  # and in Y

        deltaDAstX = uAstX*t + 0.5*accX*(t**2)      # change in pos over time interval
        deltaDAstY = uAstY*t + 0.5*accY*(t**2)

        dAstX = dAstX + deltaDAstX
        dAstY = dAstY + deltaDAstY

        uAstX = vAstX
        uAstY = vAstY
        # Now calculate new angle and range
        # tan(theta) = dAstY/dAstX, so:
        rAngtoX = math.atan(dAstY/dAstX)            #+(2*3.141592654)
        # print 'theta:', math.degrees(rAngtoX)
        r = dAstY/math.sin(rAngtoX)
        # if i == print
        if i % 5 == 0:
            print('{:05d} | {:15.2f} | {:15.2f}'.format(i, dAstX, dAstY))
            point_list.append((i, dAstX, dAstY))

    df = pd.DataFrame(data=point_list, columns=['i', 'x', 'y'])
    return df


if __name__ == '__main__':
    df = calc(950)
    sns.scatterplot(data=df, x='x', y='y')
    plt.show()

我的分析:点与点之间的间距每次都变大(注:我认为我只会每隔5点绘制一次,以使图像更可读)。从物理学的角度来看,这表明物体正在加速

难道你的计算不可能是正确的,物体离开轨道是因为它获得了足够的速度(即能量)离开中心质量(即地球)的引力场吗


正如我所说,我不熟悉具体的数学,但对我来说,对象可以以半圈内的速度从轨道上挣脱出来是有道理的。

您正在运行什么版本的Python?要正确格式化代码,请将其复制粘贴到问题框中,然后选择粘贴的所有内容(包括预览中看起来不错的部分)并点击Ctrl-K或带大括号的按钮。这对于Python代码非常重要,因为我们需要看到缩进与实际运行的代码完全相同。