# Is specific orbital energy a constant? How does this affect the semi-major axis?

I'm working on a simulation with 2 bodies - one a star and the other an orbiting planet.

I calculated the orbital energy from the first part of ${v^{2} \over {2}}-{\mu \over {r}}=-{\frac {\mu }{2a}}=\epsilon<0$.

For the simulation I have omitted $G$ from the $\mu$, so the numbers are nicer to deal with.

When I calculate the orbital energy at different points around the orbit, the value is different. Using the second part of the equation I can calculate the semi-major axis, however this will vary as the orbital energy changes...

I don't understand why, could somebody please explain?

• Then your simulation is wrong. Even with numerical error, the specific energy should be very close to constant throughout the orbit. If you are allowing both bodies to move, then make sure that $\mu$ is the sum of the masses, and that $r$ is the distance to the center of mass of the two bodies. – Mark Adler Dec 26 '15 at 19:54
• How are you calculating velocity at those points? Velocity is not constant in non-circular orbits. You can derive it yourself from Kepler's second law but you'll only arrive at the vis-viva equation ($v = \sqrt{\mu \left({ 2 \over r} - {1 \over a}\right)}$) and if you plug that into your specific orbital energy equation you'll see that it doesn't change. See proofs in linked Wiki pages. – TildalWave Dec 26 '15 at 19:55
• My code for calculating forces and the energy is here. I get the velocity from the built-in Rigidbodies in Unity. I can't add in that equation because I don't know a - I only know position and velocity vectors. – Jacob Morris Dec 26 '15 at 20:26
• Side note: The 3000 that I divide the distance by is to scale everything down – Jacob Morris Dec 26 '15 at 20:44
• You're using same velocity regardless of distance to primary, there's your problem. You'll have to find out what the velocity at distance is, see my previous comment. – TildalWave Dec 26 '15 at 21:30