Web VPython 3.2
# create and initialize variables for
# position and speed of all objects
# at time that asteroid hits Earth
dSun = 1.47e11
posSun = vec(0,-dSun,0)
posEarth = vec( 0, 0, 0)
vE = 2.978e4
velEarth = vec(-vE, 0, 0)
omegaE = vE / dSun
REarth = 6.378137e6
posAst = vec(0, REarth, 0 )
vA = 1e4
velAst = vec(0, -vA, 0)
dMoon = 3.84e8
posMoon = vec( dMoon, 0, 0) # at collision time
vMoonEarth = 1e3
velMoon = vec( -vE, vMoonEarth, 0 ) # half-moon at collision
omegaM = vMoonEarth / dMoon
# create and initialize variables for masses and G
mAst = 5e9
mEarth = 5.97e24
mSun = 1.99e30
mMoon = 7.35e22
G = 6.67e-11
# set time 0 at collision, run backwards
t = 0
dt = -10
# choose amount of time to run backwards
tf = 2e6
# set up the loop to run time backwards
# use Euler-Cromer method
while t >= -(tf):
# Motion of Earth
# use function
posEarth = posSun + dSun*vec(-sin(omegaE*t),
cos(omegaE*t),0)
# do the same for Moon
posMoon = posEarth + dMoon*vec(cos(omegaM*t),
sin(omegaM*t),0)
# asteroid motion using gravity and Euler-Cromer
Sun2Ast = posAst - posSun
gSunAtAst = -G*mSun*norm(Sun2Ast)/(mag(Sun2Ast))**2
Earth2Ast = posAst - posEarth
gEarthAtAst = -G*mEarth*norm(Earth2Ast)/(mag(Earth2Ast))**2
Moon2Ast = posAst - posMoon
gMoonAtAst = -G*mMoon*norm(Moon2Ast)/(mag(Moon2Ast))**2
velAst = velAst + (gSunAtAst + gEarthAtAst + gMoonAtAst) * dt
posAst = posAst + velAst*dt
t = t + dt
# print the result, which will be the initial
# conditions for asteroid approaching Earth
print("At time", t, "s")
print("Asteroid position", posAst, "m \n and velocity", velAst, "m/s")
print("Earth position", posEarth, "m \n and velocity", velEarth, "m/s")
print("Moon position", posMoon, "m \n and velocity",velMoon,"m/s")