# My orbiting body is orbiting about the wrong focus of it's elliptical orbit… why?

I am coding in c++ and am computing the position of an orbiting body as a function of time.

Everything is almost working. I have a nice elliptical orbit. Except, my orbiting body speeds up as it moves away from the "sun" and slows down as it approaches it.

The best way I can describe it is that it's like the sun is at the wrong focus of the ellipse.

I'm hoping someone can point me to what could cause this to happen? I've gone through all my code and I don't see any mistakes.

My steps are:

1. Compute the Mean Motion of a satellite.
2. Use this to compute the Mean Anomaly
3. Calculate the Eccentric Anomaly from this (that was a doozy)
4. Calculate the True Anomaly from the Eccentric Anomaly
5. Calculate the Heliocentric Distance
6. Finally I take the Polar Coordinates and convert to Cartesian Coordinates which I then position over the top of my "sun"

Somewhere in here I'm screwing up. I don't believe it's the final step as the calculated coordinates are derived from the angle and radius, which should be based on the correct focus.

EDIT for the equations I'm using:

Mean Motion

$$n = \sqrt{\frac{G(M+m)}{4\pi^2a^3}}$$

This is from Wikipedia.

Mean Anomaly

This is just $n \times t$ elapsed.

Eccentric Anomaly

I'm not really sure how to write this up for a non programmer. Basically the way I did this was based very much on some code that I found on the internet that I can no longer find. There's some recursion involved to gradually refine the answer. I'm convinced the issue isn't in here because I can enter my values into something like http://www.jgiesen.de/kepler/kepler.html and I have similar results.

True Anomaly

$$\nu = 2\ \arg\left(\sqrt{1-e} \ \cos \left(\frac{E}{2}\right), \sqrt{1+e} \ \sin \left(\frac{E}{2} \right)\right)$$

$e$ is Eccentricity $E$ is Eccentric Anomaly from above

Taken from here Wikipedia.

Heliocentric Distance

$$r = a \frac{1-e^2}{1+e \ \cos(\nu)}$$

$a$ is my semi-major axis $\nu$ is the True Anomaly from above

Taken from Wikipedia.

• What do you mean with the mean motion of the satellite? The mean anomaly will be linear in time, so its change will be a constant times the time step. – fibonatic Aug 8 '14 at 10:24
• @fibonatic yes you're right. However I need it to calculate the mean anomaly, so I included it as its part of my calculation. – NeomerArcana Aug 8 '14 at 11:06

I suspect that you made a mistake at step 5, since the only thing that is reversed is the radius with respect to the velocity, but the trajectory does look like an ellipse.

You probably switched a plus sign with a minus sign in the denominator of the following equation, which relates the true anomaly to the radius,

$$r = \frac{a(1-e^2)}{1 + e \cos \theta},$$

because a minus sign will give a half a period phase difference,

$$-\cos\theta = \cos(\theta + \pi).$$

After seeing the equations from your edit I think the source of the error lies with the argument function, since often it is defined as the argument of the vector $(x, y)$ like this: $\arg(y,x)$ instead of the definition on Wikepedia, which uses $\arg(x,y)$. So flipping the two inputs to the argument function should fix it. This will reverse the direction of the orbit, but that is because the direction you initially had was wrong.

• Thanks for taking the time to answer.I double checked and I didn't have a minus. In fact, I just used a minus to see what would happen and I get an orbit in the opposite direction but the speed of the orbiting body appears constant now. – NeomerArcana Aug 8 '14 at 13:02
• @DavidMurphy without knowing the equations that you are using it will be guessing. Could you try to add those to your question? By the way what happens if you add half a period to the true anomaly ($\pi$ if you work in radians or 180 if you work in degrees). – fibonatic Aug 8 '14 at 13:06
• I went and added my equations as best I could. I tried adding pi to the True Anomaly before calculating the distance. The result is an elliptical orbit in the opposite direction and the same steady speed that I saw when changing the plus to a minus sign above. – NeomerArcana Aug 8 '14 at 13:32
• I think you're right and it was the Arg. I'm now getting that same consistent speed though I'm starting to think that maybe the difference in speed closer and farther away are too slight to see. Later today I will plot the speed to make sure it is increasing slightly as it gets closer to the "sun". – NeomerArcana Aug 8 '14 at 23:12
• @DavidMurphy increasing the eccentricity will make the speed difference bigger. And another way to test if the code works properly is by seeing if the orbital energy and angular momentum are constant. – fibonatic Aug 8 '14 at 23:26

Is there a step in your code that takes an inverse trig function? Each inverse trig function has two answers, a positive and a negative. Calculators and presumably program functions probably just take the positive automatically. I made this mistake once in calculating a gravity assist trajectory for a class problem and your description reminded me of my wrong answer then.