Python Matplotlib:将轨道程序转换为3D

2024-06-25 06:04:12 发布

您现在位置:Python中文网/ 问答频道 /正文

我已经完成了我的二维轨道程序,用的是真正的物理,这是相当酷的。我现在想做的一件事,就是在方程中加上“z”力,加速度和速度。我已经做过了,但现在程序只是一条围绕垂直线的垂直线:

Planets are apparently vertical lines, unlike previously thought

因为没有像circle那样创建球体的代码,所以我需要使用trig:

代码如下:

import numpy as np
import matplotlib.pyplot as plt
import math
import matplotlib.animation as animation
import pdb
from mpl_toolkits.mplot3d import Axes3D

er = 6378100*10#m
mr = 1737400*10#m
em = 5.97219*10**24#kg
mm = 7.34767309*10**22#kg
G = 6.67384*10**(-11)
mv = -1023#m/s
nts = 10000
ts = 10000

def simData():
    tmax = ts*nts
    jx = 0.0
    t = 0.0
    d = 384400000
    mx = d
    my = 0
    mz = 0
    vx = 0
    vy = -mv
    vz = 0

    while t < tmax:
        Fg = G*(em*mm)/d**2
        Fgx = -Fg*mx/d
        Fgy = -Fg*my/d
        Fgz = -Fg*mz/d
        mAx = Fgx/mm
        mAy = Fgy/mm
        mAz = Fgz/mm
        vx = vx + mAx*ts
        vy = vy + mAy*ts
        vz = vz + mAz*ts
        mx = mx + vx*ts
        my = my + vy*ts
        mz = mz + vz*ts

        u, v = np.mgrid[0:2*np.pi:20j, 0:np.pi:10j]
        x=np.cos(u)*np.sin(v)+mx
        y=np.sin(u)*np.sin(v)+my
        z=np.cos(v)+mz
        ax.plot_wireframe(x, y, z, color="grey")

        d = math.sqrt(mx**2+my**2+mz**2)
        t = t + ts
        yield jx, t

def simPoints(simData):
    jx, t = simData[0], simData[1]
    time_text.set_text(time_template%(t))
    line.set_data(t, jx)
    return line, time_text



fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.set_aspect("equal")
line, = ax.plot([], [], 'bo', ms=10)

eu, ev = np.mgrid[0:2*np.pi:20j, 0:np.pi:10j]
eax=np.cos(eu)*np.sin(ev)
eay=np.sin(eu)*np.sin(ev)
eaz=np.cos(ev)
ax.plot_wireframe(eax, eay, eaz, color="b")

time_template = 'Time = %.1f s'    # prints running simulation time
time_text = ax.text(0.05, 0.9, 0.9,'', transform=ax.transAxes)

ani = animation.FuncAnimation(fig, simPoints, simData, blit=False,\
     interval=10, repeat=True)
plt.show()

我已经测试了没有这个程序的圆三角,它工作。另外,有什么建议来制作表面球体而不是线框球体?在


Tags: textimporttimemynpsinaxmm