;Edward Burin des Roziers
;ASEN5050

;THREE_BODY.PRO is a function that provides the inertial equations of
;motion and derivatives for the three-body problem, applied to a satellite of

;negligable mass orbiting the Earth and feeling the effects of the Moon.
;****************************************************************************

function three_body, t, X

;CONSTANTS
mu_E = 3.986d5                                ;km^3/sec^2
mu_M = 4.903d3                               ;km^3/sec^2

;Moon Position
posvel_M = moon(t)
r_EM = posvel_M[0:2]
r_ESat = X[0:2]
r_SatM = r_EM - r_ESat

rm_ESat = magn(r_ESat)
rm_SatM = magn(r_SatM)
rm_EM = magn(r_EM)

Xdot = [X[3],X[4],X[5],(-(mu_E+mu_M)*r_ESat[0]/rm_ESat^3 + mu_M*(r_SatM[0]/rm_SatM^3 - r_EM[0]/rm_EM^3)),$
                                      (-(mu_E+mu_M)*r_ESat[1]/rm_ESat^3 + mu_M*(r_SatM[1]/rm_SatM^3 - r_EM[1]/rm_EM^3)),$
                                      (-(mu_E+mu_M)*r_ESat[2]/rm_ESat^3 + mu_M*(r_SatM[2]/rm_SatM^3 - r_EM[2]/rm_EM^3))]

return, Xdot

end