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

Conservation of energy is universal across all mechanics, including orbital mechanics. The orbital energy will be constant as long as there are no non-conservative external forces acting on the body (gravity is a conservative force). Semi-major axis is directly related to orbital energy as you have shown, therefore it will also remain constant. The semi-major axis defines the size of the orbit and therefore it also makes sense that the orbit doesn't change without any external forces.

If you are only working on a two-body problem, then you can simply choose an orbit based on the set of orbital elements (semi-major axis, eccentricity, and argument of perigee, as well as inclination and right ascension of the ascending node in full 3D cases). With those elements you don't have to simulate the orbit with gravity forces and equations of motion, you can just simply map out the position of the body at any time you wish.

However, let's assume you still want to simulate gravity for the two-body case since it can be useful and is easily extended to more than two bodies. Looking at your code, the biggest thing that stands out to me is: why are you dividing the distance by 3000? Remove that scaling from your function and only apply it for plotting.

• The reason I applied the 3000 scale is because very very little happens without. after x amount of time, the planet has moved 50,000 units horizontally and only 1.956686e-06 vertically – Jacob Morris Dec 26 '15 at 21:11
• That scaling is a bad way to compensate for that, you should either be scaling the masses or playing with the integration time step size. Simply scaling the distance is not smart since you may be introducing errors you don't realize! – Brian Lynch Dec 26 '15 at 21:20
• I've made the star the heaviest allowed, and the planet the lightest, and sped the simulation up as fast as it will allow me, but nothing is happening because the calculations are so small. Even placing these as far away as possible only yields a very very minute number which doesn't even shift the planet a pixel, after running for a few minutes – Jacob Morris Dec 26 '15 at 21:28
• Well you need to revert to your original question: without the scaling, does your orbital energy change? If not then you have your answer! If so then you are doing something else incorrectly. If this is only a two-body problem then you should just use orbital mechanics instead of forces and equations of motion. – Brian Lynch Dec 26 '15 at 21:30
• Your two bodies don't move with respect to each other because you're computing for a circular orbit not an ellipse with constant velocity, and because you likely set your frame of reference wrong and all you'd see is change in distance between the two bodies (which stays the same in a circular orbit). Either case, you'll have to produce a whole lot more code than what you linked to so far, if you want us to see what you're doing wrong. – TildalWave Dec 26 '15 at 21:42