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:


*

*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.
 A: 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.
A: 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.
