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:
- Compute the Mean Motion of a satellite.
- Use this to compute the Mean Anomaly
- Calculate the Eccentric Anomaly from this (that was a doozy)
- Calculate the True Anomaly from the Eccentric Anomaly
- Calculate the Heliocentric Distance
- 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.