My task here is to determine orbit parameters, using current values:
- $\mu=GM$ - standard gravitational parameter
- $r$ - distance to the object with Mass $M$
- $v$ - speed of the object in the point $r$
I have tried to do this task using Specific orbital energy:
$$ \epsilon=\frac{v^2}{2}-\frac{\mu}{r} =-\frac{\mu^2}{2h^2}(1-e^2)=-\frac{\mu}{2a};(1) $$ This gives me $a$ and $e$. $$ a = -\frac{\mu}{2\epsilon}; $$ $$ e = \sqrt{1+\frac{2h^2\epsilon}{\mu^2}}; h=\vec{r}\times \vec{v}; $$ In 2d cross-product of vectors are they's manitude;
At the same time, I've launched simulation to draw gravitation only movement with the same $\mu$ as in the given equation.
Simulation is simple: $$ \vec g=\frac{\mu}{\vec {r^2}};(2) $$ $$ \vec v=\vec gt+\vec {v_0};(3) $$ $$ \vec x=\vec gt^2 + \vec vt+\vec{x_0};(4) $$
The code in simulation is written in javascript:
var rv = Sub(o.p, sun.c); // o.p contains [x,y] coords of point, and sun.c contains coords of sun - gravity object
var rvn = Normal(rv); // position normal
var g = mu/ (r*r); // free-fall acceleration magnitude
var gv = Mul(rvn, -g); // frefall vector - negative to position
var dv = Mul(gv, dt); //velocity changes
var dx = Add(Mul(gv , dt*dt) , Mul(o.v, dt)); // position changes
o.v = Add(o.v, dv); // new speed
o.p = Add(o.p, dx); // new position
But, my code gives me wrong results. Ellipse, which is drawn on calculated from (1) parameters ($e$,$a$) is narrower than ellipse, which is drawn by my gravity simulation.
...And in the same way, when I took $r_a$ and $r_p$ parameters from my simulation and drawn an ellipse with $e=\frac{r_a-r_p}{r_a+r_p}$ and $a=\frac{r_a+r_p}{2}$, It was corresponding to calculated (not simulated) ellipse.

This brings me to make assumption, that there's something wrong with gravity simulator, but I'm running out of ideas, what's going on with my simulation.
Where could I make a mistake?
PS: Demo code is here. It uses mouse to shoot moving objects. After shooting all ellipses are drawn, but there's several options, when calculated ellipses having wrong orientation - reload page to remove them and start with blank canvas. When shooting objects with eccentricity more than 1, arrow showed in red.
Mul,Sub, etc. functions - are function over vectors. – Vasiliy Stavenko Jul 01 '14 at 12:23