# Mars Circular Intercept Orbit

I've been trying to simulate a spacecraft entering a circular capture orbit about Mars using Mathematica, but am having a little trouble. The simulation starts when the spacecraft enters Mars' sphere of influence. In order to find the correct positions and velocities for a circular orbit, I calculated the point of closest approach between the spacecraft and Mars, and then found their positions and velocities at that point. I also calculated the angle that the spacecraft's velocity vector makes with the positive x-axis at closest approach. Once I had these bits, I used the WhenEvent function to give the spacecraft the same velocity as Mars + the velocity required for a circular orbit about Mars at closest approach (looking at the WhenEvent code will hopefully make things clearer), using the following formulae:

$$v_{sx}=v_{mx}+ \sqrt{\frac{GM_{mars}}{r}} \sin{\theta},$$ $$v_{sy}=v_{my}+\sqrt{\frac{GM_{mars}}{r}} \cos{\theta},$$

where $v_{sx}$ and $v_{sy}$ are the x- and y-components of the spacecraft's velocity, $v_{mx}$ and $v_{my}$ are the x- and y-components of Mars' velocity, $r$ is the radial separation between the spacecraft and Mars and $\theta$ is the angle made between the positive x-axis and the spacecraft's velocity vector (all at closest approach).

The following is the Mathematica code, which should work if you just plonk it into a new notebook:

Remove["Global*"]
G = 6.672*10^-11; (*Gravitational Constant*)
m = 1.988544*10^30 ;(*Mass of Sun*)
m = 6.4185*10^23; (*Mass of Mars*)
m = 1000; (*Mass of spacecraft*)
(*Mars' position and velocity at spacecraft's entrance to Mars' SOI*)
p = {-1.3528201165963936*^11, -1.8675833330580637*^11};
v = {20533.99477259318, -12116.993615214029} ;
r = 3.3899*10^6 ;(*Mean planetary radius of Mars*)
(*Spacecraft's position and velocity at entrance to Mars' SOI*)
p = {-1.3470234059931529*^11, -1.8670782689951355*^11};
v = {17250.213610967, -12349.519721984863};

(*Simulation running time*)
tmax = 86400*5

Soln = NDSolve[{

x''[t] == -((G m x[t])/((x[t])^2 + (y[t])^2)^(3/2)),
y''[t] == -((G m y[t])/((x[t])^2 + (y[t])^2)^(3/2)),
x''[t] == -((G m x[t])/((x[t])^2 + (y[t])^2)^(3/2)) - (G m (x[t]- x[t]))/((x[t] - x[t])^2 + (y[t] - y[t])^2)^(3/2),
y''[t] == -((G m y[t])/((x[t])^2 + (y[t])^2)^(3/2)) - (G m (y[t]- y[t]))/((x[t] - x[t])^2 + (y[t] - y[t])^2)^(3/2),
x == p[], y == p[], x == p[],
y == p[], x' == v[], y' == v[],
x' == v[], y' == v[],
WhenEvent[
t == 173379, {x'[t] -> 20785.020973205566 + Sqrt[(G m)/6.395400814228174*^6]Sin[-0.7053105626554602],
y'[t] ->(*v[]*)-11763.84750366211 -
Sqrt[(G m)/6.395400814228174*^6]
Cos[-0.7053105626554602]}]}, {x[t], y[t], x[t],
y[t]}, {t, 0, tmax}, StartingStepSize -> 0.001,
AccuracyGoal -> 17, PrecisionGoal -> 17,
Method -> "StiffnessSwitching", MaxSteps -> 10000000]

Show[ParametricPlot[
Evaluate[{{x[t], y[t]}, { x[t], y[t]}} /. Soln], {t, 0,
tmax}, AxesLabel -> {x, y}, PlotStyle -> Automatic,
PlotRange -> Full, ImageSize -> Large],
Graphics[{Red, Disk[{0, 0}, r]}]]

Animate[ParametricPlot[{{x[t], y[t]}, {x[t], y[t]}} /.
Soln /. t -> a, {t, Max[0, a - 20000], a}, AxesLabel -> {x, y},
Axes -> False, ImageSize -> Large], {a, 0, tmax},
AnimationRate -> 10000]


Also, for those who are interested in calculating the positions and velocities at closest approach, do the following: Comment out the WhenEvent part in NDSolve and then put the following code below the NDSolve ouput:

Spacecraft Minimum Approach Radius at Intercept Point
dt = 60;
MarsPosition =
Table[{x[t], y[t]} /. Soln, {t, 0, 86400*2.2, dt}] ;
SpaceCraftPosition =
Table[{x[t], y[t]} /. Soln, {t, 0, 86400*2.2, dt}] ;
dxy = Sqrt[(MarsPosition - SpaceCraftPosition)^2];
dr = Table[Norm[dxy[[i]]], {i, 1, Length[dxy]}];
(*Find closest approach of spacecraft to Mars*)
mindr = Min[dr]  (*Pretty accurate using forward difference for speed!*)
(*Finds index position of mindr in dr*)
mindrindex = Position[dr, mindr]
(*Finds time of closest approach*)
mindrtime = dt*2891(*mindrindex*)

Heliocentric Velocity and Position of Spacecraft and Mars at Minimum Approach Radius
xy2f = MarsPosition[];
xy2f2 = xy2f[];
x2f = xy2f2[]
y2f = xy2f2[]

xy2i = MarsPosition[];
xy2i2 = xy2i[];
x2i = xy2i2[];
y2i = xy2i2[];

v2x = (x2f - x2i)/dt
v2y = (y2f - y2i)/dt

xy3f = SpaceCraftPosition[];
xy3f2 = xy3f[];
x3f = xy3f2[]
y3f = xy3f2[]

xy3i = SpaceCraftPosition[];
xy3i2 = xy3i[];
x3i = xy3i2[];
y3i = xy3i2[];

v3x = (x3f - x3i)/dt
v3y = (y3f - y3i)/dt
theta = ArcTan[v3x, v3y]
`

This is the intercept orbit I got with the above values: As can be seen from the output, the spacecraft doesn't go into a circular orbit, but instead goes into a highly eccentric one. Messing around manually with the value of theta and setting it to -1.1 radians seems to get an orbit with quite a low eccentricity, but it is a massive difference from the value of -0.705 radians calculated using the spacecraft's closest approach velocity, so I feel like I must be doing something wrong. At first I thought the error might be because I was using a simple forward difference to calculate velocities, but surely this is adequate for this situation? Any help would be appreciated.

• Would Computational Science be a better home for this question? – Qmechanic Apr 7 '14 at 15:47
• What coordinate system are you using? What is your x axis? – HopDavid Apr 7 '14 at 20:58
• I'm using a standard Cartesian coordinate system. – InquisitiveInquirer Apr 7 '14 at 21:46
• Are you using the barycenter of the solar system for your origin? The ecliptic plane for your xy plane? Is θ the angle between the ship's velocity vector and Mars' velocity vector? I'm not familiar with Mathematica but have modeled orbits in other software. If I could better visualize the geometry of your scenario, I might be able to help. – HopDavid Apr 8 '14 at 1:03
• Sorry, I should have been more specific. Yes, the Solar System barycentre is the origin and the ecliptic is the xy plane. Though, $\theta$ is the angle that the spacecraft's velocity vector makes with the x-axis, not the angle between Mars and the spacecraft. That is, $\theta = \arctan{v_y/v_x}$, where $v_x$ and $v_y$ are the x and y component velocities of the spacecraft. – InquisitiveInquirer Apr 8 '14 at 6:15

$\theta$ is the angle made between the positive x-axis and the spacecraft's velocity vector (all at closest approach).
Don't you need $\theta$ to be the angle of the spacecraft's velocity relative to Mars? Try subtracting the Mars velocity vector from the spacecraft's velocity vector, calculating $\theta$ for the resultant vector and using that angle in the WhenEvent subroutine.
• After some testing it looks like I had to set $\theta$ equal to the angle that the spacecraft's radial vector makes relative to Mars, not its velocity vector. But since you got me thinking on those lines I'll give you the bounty, thank you! – InquisitiveInquirer Apr 10 '14 at 15:43