我正在尝试对一个被月球环绕的行星进行简单的模拟。到目前为止,我有一个解决行星和月球轨道的 2 体问题。现在我想给行星添加一个固定的旋转轴,看看它是如何受到月球的影响的。知道如何通过使用 python 来完成吗?
可以使用以下代码运行两个主体问题:
import pylab
import math
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
# Set Constants
G = 6.67e-11
AU = 1.5e11
daysec = 24.0*60*60
Ma =5.972e24 # Planet mass in Kg
Mb = 7.348e22 # Moon mass in Kg
gravconst = G*Ma*Mb
# Set up starting conditions
# Planet
xa = 0.0
ya = 0.0
za = 0.0
xva = 0.0
yva = 0.0
zva = 0.0
# Moon
xb = 384400000
yb = 0.0
zb = 0.0
xvb = 0.0
yvb = 1000.0
zvb = 0.0
# Time steps
t = 0.0
dt = 0.01*daysec
# Coordinate lists
xalist = []
yalist = []
xblist = []
yblist = []
zalist = []
zblist = []
# Loop
while t < 100.0*daysec:
# Compute Force
rx = xb-xa
ry = yb-ya
rz = zb-za
modr3 = (rx**2+ry**2+rz**2)**1.5
fx = -gravconst*rx/modr3
fy = -gravconst*ry/modr3
fz = -gravconst*rz/modr3
# Update quantities
# Moon
xvb += fx*dt/Mb
yvb += fy*dt/Mb
zvb += fz*dt/Mb
xb += xvb*dt
yb += yvb*dt
zb += zvb*dt
# Planet
xva += -fx*dt/Ma
yva += -fy*dt/Ma
zva += -fz*dt/Ma
xa += xva*dt
ya += yva*dt
za += zva*dt
t += dt
# Saving them in lists
xalist.append(xa)
yalist.append(ya)
zalist.append(za)
xblist.append(xb)
yblist.append(yb)
zblist.append(zb)
xalist[:] = [x / 1e6 for x in xalist]
yalist[:] = [x / 1e6 for x in yalist]
zalist[:] = [x / 1e6 for x in zalist]
xblist[:] = [x / 1e6 for x in xblist]
yblist[:] = [x / 1e6 for x in yblist]
zblist[:] = [x / 1e6 for x in zblist]
#Creating the point to represent the planet at the origin (not to scale),
plt.scatter(0,0,s=200,color='blue')
plt.annotate('Planet', xy=(-45,-50))
plt.scatter(xblist[0],0,s=100,color='grey')
plt.annotate('Mond', xy=(xblist[0]-45,-50))
# Plotting
pylab.plot(xalist, yalist, "-g")
pylab.plot(xblist, yblist, "-r")
plt.axhline(0, color='black')
plt.axvline(0, color='black')
pylab.axis("equal")
pylab.xlabel("X (Mio. Meter)")
pylab.ylabel("Y (Mio. Meter)")
pylab.show()