我一直在跟tutorial一起跟踪,它模拟了地球,金星和太阳。代码运行正常。但是,我正在尝试对围绕地球的卫星轨道进行建模,其中地球是其“宇宙”在位置(0,0)的中心。我的卫星在切线上飞行时遇到了问题,但我不确定为什么。
import numpy as np
import numpy as np
class Planet():
def __init__(self,vx=0.0,vy=0.0,px=0.0,py=0.0,mass=0.0):
self.vx=vx
self.vy=vy
self.px=px
self.py=py
self.mass = mass
def attract(p1,p2,grav=6.67428e-11):
dx = p1.px - p2.px
dy = p1.py - p2.py
d = np.sqrt(dx**2 + dy**2)
force = (grav * p1.mass * p2.mass)/d**2
theta = math.atan2(dy,dx)
fx = math.cos(theta)*force
fy = math.sin(theta)*force
return fx,fy
def loop(bodies,epochs=10,timestep=10):
# compute forces
forces = {}
for i in range(0,len(bodies)):
total_fx = 0
total_fy = 0
for j in range(0,len(bodies)):
if i == j:
continue
fx,fy = attract(bodies[i],bodies[j])
total_fx += fx
total_fy += fy
forces[i] = (total_fx,total_fy)
#apply forces
sat_tups = []
for e in range(epochs):
for i in range(len(bodies)):
if bodies[i] == satellite:
sat_tups.append((bodies[i].px,bodies[i].py))
fx,fy = forces[i]
bodies[i].vx += (fx/bodies[i].mass*timestep)
bodies[i].vy += (fy/bodies[i].mass*timestep)
bodies[i].px += bodies[i].vx*timestep
bodies[i].py += bodies[i].vy*timestep
return sat_tups
earth = Planet(mass = 5.9742 * 10**24)
satellite = Planet(mass= 3000.0, px= 1414.0, py= 1414.0, vx=2300.0,vy=2300.0)
tups = loop(bodies=[earth,satellite])
tups
>>>
[(1414.0, 1414.0),
(7050856421.79636, 7050856421.79636),
(21152543437.38908, 21152543437.38908),
(42305062460.77817, 42305062460.77817),
(70508413491.96361, 70508413491.96361),
(105762596530.9454, 105762596530.9454),
(148067611577.72357, 148067611577.72357),
(197423458632.2981, 197423458632.2981),
(253830137694.66898, 253830137694.66898),
(317287648764.83624, 317287648764.83624)]
我不确定是什么问题。也许以(0,0)为中心的地球根本行不通。或者,也许这些物理定律仅适用于尺寸相对相似的物体(其他更复杂的规则正在发挥作用)。
要回答此问题,请解决导致卫星飞离的原因? (我猜想它会撞到地球上,而不是被弹开了。)