<p>The equations of motion are just second order ordinary differential equations. They can be solved numerically by any of the usual methods, However, for two bodies an exact solution can be found, that solution was known to Kepler. To model the trajectory you need to know the orbital period and the eccentricty (e) of the orbit.</p>

<p>If you know the  period of orbit of a body, then the  "Mean anomaly (M)" is the angle time/period *2*pi radians (it increases uniformly from zero to 2pi in one orbital period.</p>

<p>The find the Eccentric anomaly ("E", the angle made by the body, the center of the elliptical orbit and the point of periapsis when the body is closest to the sun)  you solve $M=E-e\sin(E)$ (it can be solved by newton's method quickly, though convergence is fastest for roughly circular orbits.</p>

<p>You then know the body is on a ellipse, with the sun at one focus and you have calculated the ray on which the body is found at a given time. This gives you the position of the body.</p>

<p>There is a rough implementation of this in a <a href="https://gist.github.com/zeimusu/7a2e71de2301df893731775398df7fe5" rel="nofollow">python gist</a></p>

CC BY-SA 3.0
2016-03-29T17:25:14.413Z
2016-03-29T17:25:14.413Z
James K
<p>I IT developer, and I would code a small program that simulates the orbit of two bodies, the problem of two bodies eventually. 
I have no problem on the part programming but I have a little trouble with the calculations to be done to retrieve the position of the second body that is in orbit around the first. 
Could someone expliqer calculations me or give me a site with a concrete example, such calculations to do to have the orbit of the moon around the earth or the earth around the sun?</p>

CC BY-SA 3.0
2016-03-29T16:35:37.203Z
How about this? The only bit you really need is the one-line equation for (Newtonian) gravity between two objects, after that it's just modelling moving bodies. http://stackoverflow.com/questions/22068178/implementing-newtons-law-of-universal-gravity-in-c-sharp
Andy
CC BY-SA 3.0
2016-03-29T18:04:25.193Z
An easy way to implement this is the Leapfrog Integration. Have a look at [the code in this answer](http://physics.stackexchange.com/a/230887/70207).
pela
<orbit><astrophysics>
Simulates Orbit
simon
# Simulates Orbit

(Asked by: username_0 on 2016-03-29)

I IT developer, and I would code a small program that simulates the orbit of two bodies, the problem of two bodies eventually.
I have no problem on the part programming but I have a little trouble with the calculations to be done to retrieve the position of the second body that is in orbit around the first.
Could someone expliqer calculations me or give me a site with a concrete example, such calculations to do to have the orbit of the moon around the earth or the earth around the sun?

**Tags:** orbit, astrophysics
> **Comments:**
> **username_1**: How about this? The only bit you really need is the one-line equation for (Newtonian) gravity between two objects, after that it's just modelling moving bodies. http://stackoverflow.com/questions/22068178/implementing-newtons-law-of-universal-gravity-in-c-sharp  
> **username_2**: An easy way to implement this is the Leapfrog Integration. Have a look at [the code in this answer](http://physics.stackexchange.com/a/230887/70207).  

---

## 1 answers

### Answer by username_3 on 2016-03-29. Score: 0

The equations of motion are just second order ordinary differential equations. They can be solved numerically by any of the usual methods, However, for two bodies an exact solution can be found, that solution was known to Kepler. To model the trajectory you need to know the orbital period and the eccentricty (e) of the orbit.

If you know the period of orbit of a body, then the "Mean anomaly (M)" is the angle time/period \*2\*pi radians (it increases uniformly from zero to 2pi in one orbital period.

The find the Eccentric anomaly ("E", the angle made by the body, the center of the elliptical orbit and the point of periapsis when the body is closest to the sun) you solve $M=E-e\sin(E)$ (it can be solved by newton's method quickly, though convergence is fastest for roughly circular orbits.

You then know the body is on a ellipse, with the sun at one focus and you have calculated the ray on which the body is found at a given time. This gives you the position of the body.

There is a rough implementation of this in a [python gist](https://gist.github.com/zeimusu/7a2e71de2301df893731775398df7fe5)


---

