<p>If you're running your own N-body dynamics,
you can get initial Cartesian positions and velocities for each body in your system (including the Sun) by asking
<a href="http://ssd.jpl.nasa.gov/horizons.cgi" rel="nofollow">JPL HORIZONS</a>
for Ephemeris Type: Vectors.
I would choose Coordinate Origin: Solar System Barycenter so that the Sun does not drift away from the origin,
and Target Body: [Planet] Barycenter (or Sol) unless you are modeling the Earth and Moon as separate bodies.</p>

<p>If you still want to compute initial Cartesian positions and velocities from Keplerian elements, first do a sanity check: Earth should cross the X axis at the equinoxes and the Y axis at the solstices.
Second, those approximate elements for the major planets may not be precise enough for your purpose.
Instead you might ask HORIZONS for Ephemeris Type: Elements, and Time Span beginning with the epoch of your elements for Apophis.
You could also get elements for Apophis in a more convenient epoch this way.</p>

<p>Note that Keplerian elements are subject to dynamic evolution and are only valid for a limited time near their epoch.
If you ask HORIZONS for Apophis's orbital elements one week before and after its close approach to Earth, you can see that they are quite different. Of course the latter are also highly uncertain.</p>

<p>For the gory post-Newtonian details of the dynamical model the Apophis prediction article discussed extending, see JPL's
<a href="http://ilrs.gsfc.nasa.gov/docs/2014/196C.pdf" rel="nofollow">notes on DE430 and DE431</a>, part III.</p>

CC BY-SA 3.0
2016-08-16T03:07:03.130Z
I tried to follow closely your answer, but could not fix my code to achieve the first sanity check (very nice test indeed, thanks for that). Can you provide a link to an article or book with the calculation/explanation?
hawaii
CC BY-SA 3.0
2016-08-16T21:11:54.963Z
My sanity check is based on the usual definition of [rectangular ecliptic coordinates](https://en.wikipedia.org/wiki/Ecliptic_coordinate_system#Rectangular_coordinates).
Mike G
CC BY-SA 3.0
2016-08-16T22:14:16.077Z
Your answer helped me a lot to debug my code. It is working now. Strangely, the orbital elements (planets) on the NASA page seems to be acting weird. I'm implementing a way to query horizons through telnet and respond to the browser as a restful application. After that I believe I will be able to answer my own question on what was going wrong, since I will have all the tools I need to check everything. Thanks again!
hawaii
CC BY-SA 3.0
2016-08-17T13:45:13.027Z
The approximate elements for the planets are approximately correct. Earth may have wildly different $\Omega$ and $\omega$ due to near-zero $I$, but the longitude of perihelion should be about the same. Good luck!
Mike G
CC BY-SA 3.0
2016-08-15T19:50:35.903Z
2016-08-15T20:34:32.127Z
2016-08-15T20:34:32.127Z
Mike G
<p>I would like to build a gravity model (with 3D graphics) that would build the orbit of the planets based on orbital elements input and the gravity calculation for each element. </p>

<p><a href="http://daed-dev.on.br/~csiqueira/on-physics/src/examples/framework-example-kepler.html" rel="nofollow">My initial code is live here (Click the 0.005 to turn it into 0.05 and then click on restart time to run the orbits).</a></p>

<p>The example above has all planets on Solar System (up to Neptune), the Sun and the asteroid Apophis. For clarity, the Sun and planets other than earth have been hidden, but are still considered for gravity calculations.</p>

<p>However, the orbits do not match the correct estimate intersection date. <a href="http://neo.jpl.nasa.gov/apophis/" rel="nofollow">In this article</a>, it says that Earth-Apophis close-encounters will happen on 13 April (meaning that the earth should be in the orbit's intersection point every 13 April, or very close to it).</p>

<p>The data from the program is taken from Nasa JPL.</p>

<ul>
<li><a href="http://ssd.jpl.nasa.gov/sbdb.cgi?sstr=99942;orb=1" rel="nofollow">Apophis Data</a></li>
<li><a href="http://ssd.jpl.nasa.gov/txt/aprx_pos_planets.pdf" rel="nofollow">Planets Data</a></li>
<li>The sun is positioned at the origin</li>
</ul>

<p>Start date is 2454441.5, as given in JPL Apophis data. Planets orbital elements have been corrected to this date, with the calculations in the above PDF.</p>

<p><strong>Why the orbits intersection date is wrong? Should I be doing something else? Am I doing something wrong?</strong></p>

<p><strong>The above Apophis link also cites a "Standard Dynamical Model". If anyone knows how to correctly create one, I believe this would answer my question as well.</strong></p>

<p>TLDR: How can I correctly create planetary orbits from kepler elements?</p>

<p>Thank you in advance if anyone can help me!</p>

CC BY-SA 3.0
2016-08-15T15:43:20.127Z
Answer to a deleted comment: I'm converting keplerian orbital elements into cartesian coordinates and then solving it numerically (n^2 total gravity calculations, n = number of bodies). What I meant by the date is that Earth should be at the intersect position on April 13 every year. Apophis position only matters on 2029 and 2036 like you said.
hawaii
CC BY-SA 3.0
2016-08-15T18:41:13.103Z
I don't know how to do that, but for all 8 planets and Apophis, the calculation is enormously complicated.   Once you have 3 bodies, simple equations no longer work and you need to run constant corrections.    There's no longer a simple formula.  (look up 3 body problem).   I don't think what you ask can be programmed without a PHD in perturbation theory and pages and pages of code and perhaps a supercomputer.
userLTK
CC BY-SA 3.0
2016-08-15T19:32:22.890Z
@userLTK No, straight (numerical) integration of the system should be accurate enough Eg Verlet integration. I'd guess there is some mistake in the initialisation of the system.
James K
CC BY-SA 3.0
2016-08-16T11:02:45.827Z
Thanks @JamesK  I didn't know that.   I remember the 3 body problem being unsolvable when I studied math, but I'm guessing there's work-arounds with a stable system.
userLTK
<gravity><orbital-elements><software>
How to correctly create planetary orbits from kepler elements
hawaii
# How to correctly create planetary orbits from kepler elements

(Asked by: username_0 on 2016-08-15)

I would like to build a gravity model (with 3D graphics) that would build the orbit of the planets based on orbital elements input and the gravity calculation for each element.

[My initial code is live here (Click the 0.005 to turn it into 0.05 and then click on restart time to run the orbits).](http://daed-dev.on.br/~csiqueira/on-physics/src/examples/framework-example-kepler.html)

The example above has all planets on Solar System (up to Neptune), the Sun and the asteroid Apophis. For clarity, the Sun and planets other than earth have been hidden, but are still considered for gravity calculations.

However, the orbits do not match the correct estimate intersection date. [In this article](http://neo.jpl.nasa.gov/apophis/), it says that Earth-Apophis close-encounters will happen on 13 April (meaning that the earth should be in the orbit's intersection point every 13 April, or very close to it).

The data from the program is taken from Nasa JPL.

* [Apophis Data](http://ssd.jpl.nasa.gov/sbdb.cgi?sstr=99942;orb=1)
* [Planets Data](http://ssd.jpl.nasa.gov/txt/aprx_pos_planets.pdf)
* The sun is positioned at the origin

Start date is 2454441.5, as given in JPL Apophis data. Planets orbital elements have been corrected to this date, with the calculations in the above PDF.

**Why the orbits intersection date is wrong? Should I be doing something else? Am I doing something wrong?**

**The above Apophis link also cites a "Standard Dynamical Model". If anyone knows how to correctly create one, I believe this would answer my question as well.**

TLDR: How can I correctly create planetary orbits from kepler elements?

Thank you in advance if anyone can help me!

**Tags:** gravity, orbital-elements, software
> **Comments:**
> **username_0**: Answer to a deleted comment: I'm converting keplerian orbital elements into cartesian coordinates and then solving it numerically (n^2 total gravity calculations, n = number of bodies). What I meant by the date is that Earth should be at the intersect position on April 13 every year. Apophis position only matters on 2029 and 2036 like you said.  
> **username_1**: I don't know how to do that, but for all 8 planets and Apophis, the calculation is enormously complicated.   Once you have 3 bodies, simple equations no longer work and you need to run constant corrections.    There's no longer a simple formula.  (look up 3 body problem).   I don't think what you ask can be programmed without a PHD in perturbation theory and pages and pages of code and perhaps a supercomputer.  
> **username_2**: @userLTK No, straight (numerical) integration of the system should be accurate enough Eg Verlet integration. I'd guess there is some mistake in the initialisation of the system.  
> **username_1**: Thanks @JamesK  I didn't know that.   I remember the 3 body problem being unsolvable when I studied math, but I'm guessing there's work-arounds with a stable system.  

---

## 1 answers

### Answer by username_3 on 2016-08-15. Score: 2

If you're running your own N-body dynamics,
you can get initial Cartesian positions and velocities for each body in your system (including the Sun) by asking
[JPL HORIZONS](http://ssd.jpl.nasa.gov/horizons.cgi)
for Ephemeris Type: Vectors.
I would choose Coordinate Origin: Solar System Barycenter so that the Sun does not drift away from the origin,
and Target Body: [Planet] Barycenter (or Sol) unless you are modeling the Earth and Moon as separate bodies.

If you still want to compute initial Cartesian positions and velocities from Keplerian elements, first do a sanity check: Earth should cross the X axis at the equinoxes and the Y axis at the solstices.
Second, those approximate elements for the major planets may not be precise enough for your purpose.
Instead you might ask HORIZONS for Ephemeris Type: Elements, and Time Span beginning with the epoch of your elements for Apophis.
You could also get elements for Apophis in a more convenient epoch this way.

Note that Keplerian elements are subject to dynamic evolution and are only valid for a limited time near their epoch.
If you ask HORIZONS for Apophis's orbital elements one week before and after its close approach to Earth, you can see that they are quite different. Of course the latter are also highly uncertain.

For the gory post-Newtonian details of the dynamical model the Apophis prediction article discussed extending, see JPL's
[notes on DE430 and DE431](http://ilrs.gsfc.nasa.gov/docs/2014/196C.pdf), part III.

> **Comments:**
> **username_0**: I tried to follow closely your answer, but could not fix my code to achieve the first sanity check (very nice test indeed, thanks for that). Can you provide a link to an article or book with the calculation/explanation?  
> **username_3**: My sanity check is based on the usual definition of [rectangular ecliptic coordinates](https://en.wikipedia.org/wiki/Ecliptic_coordinate_system#Rectangular_coordinates).  
> **username_0**: Your answer helped me a lot to debug my code. It is working now. Strangely, the orbital elements (planets) on the NASA page seems to be acting weird. I'm implementing a way to query horizons through telnet and respond to the browser as a restful application. After that I believe I will be able to answer my own question on what was going wrong, since I will have all the tools I need to check everything. Thanks again!  
> **username_3**: The approximate elements for the planets are approximately correct. Earth may have wildly different $\Omega$ and $\omega$ due to near-zero $I$, but the longitude of perihelion should be about the same. Good luck!  

---

