# How to predict the location of a planet at a given time? [closed]

I am trying to create a simple two-dimensional computer simulation of a planetary system.

While I've been able to understand Kepler's Laws of Planetary Motion, I do not know enough about the formulas to apply them.

I understand the first law. I understand the second law, although I have no idea how to create a formula that uses it. If I understand it correctly, the third formula is only useful when comparing the orbits or orbiting periods of two planets.

What I am trying to do, is to determine, at a time $T$, what the location/position of a planet $P_1$ is, given its orbit and a starting position.

For that matter, I do not fully understand how to properly describe a certain elliptical orbit either. I often see ellipses defined with a center, a radius and an eccentricity, but then you do not know the location of the foci?

## closed as off-topic by ACuriousMind♦, user36790, Norbert Schuch, Gert, Qmechanic♦Jan 8 '16 at 20:38

This question appears to be off-topic. The users who voted to close gave this specific reason:

• "Homework-like questions should ask about a specific physics concept and show some effort to work through the problem. We want our questions to be useful to the broader community, and to future users. See our meta site for more guidance on how to edit your question to make it better" – ACuriousMind, Community, Norbert Schuch, Gert, Qmechanic
If this question can be reworded to fit the rules in the help center, please edit the question.

• You might consider asking this question on astronomy.stackexchange.com. The formulas for elliptical orbits aren't exact, but they do exist. – barrycarter Jan 8 '16 at 14:47

Your questions shows some confusions about how orbits work. Kepler's three laws are useful in describing planetary orbits, but they don't quite get you the entire way into a direct way to calculate the position $\mathbf r(t)$ given a specified time $t$.

Instead, you require a solution of Newton's equations of motion for the planet. There are two ways to do this:

• One is numerically: take a starting position (like a fixed starting ephemeris) and then propagate it numerically using the numerical ODE solver of your choice. This could be based on Euler's method, some level of the Runge-Kutta method, Verlet's algorithm as described in Rutger's answer. In general, it takes slightly more work to implement a higher-order method such as Runge-Kutta, but once you do it can result in surprisingly accurate results even for moderate time steps.

• Alternatively, you can solve it analytically - or, of course, rely on the analytical solution that's been known since the time of Newton. The problem here is that the analytical method will only give you a closed-form, elementary expression for the distance $r(t)$ from the focus in terms of the angular variable $\theta(t)$, defined such that $\theta(t)=0$ at periapsis, in the form $$r(t)=\frac{p}{1+e \cos(\theta(t))}, \tag1$$ where $p$ describes the size of the orbit and $e$ is the eccentricity. If you start with $t$ and you want the position, though, there is no closed-form elementary expression that you can use; instead, you're forced to solve a trascendental equation known as Kepler's equation, which reads $$t=\kappa (E-e\sin(E)), \tag{2}$$ where $\kappa$ is a constant and $E$ is the eccentric anomaly, which gives you the angular coordinate via $$\cos(\theta)=\frac{\cos(E)-e}{1-e\cos(E)}.$$

The problem is that Kepler's equation $(2)$ is a transcendental equation, which means that it doesn't have closed-form solutions and you will need to do it numerically, using e.g. Newton's method. This isn't terrible but it does mean that even the analytical solution has a nonzero numerical weight to it, and if you want to be doing this repeatedly and accurately you will end up spending a lot of time numerically solving $(2)$.

For more details, see the Wikipedia articles on Kepler orbits and the position as a function of time under Kepler's laws of motion.

If you just want one position at a single time, it is probably cheaper to implement the exact solution, which will have a nonzero but fairly trivial numerical cost. It is unlikely, however, that you do in fact want a single position, and more likely you want to plot a stretch of the orbit for a variety of times, in which case the numerical cost of the exact solution could be higher than a properly-implemented Runge-Kutta solver (which will give you all the positions you need in a single go). The choice between the two is a tradeoff between numerical cost and ease of implementation, and there is no clear recommendation in general.

One thing you should keep in mind is that it's not a terrible thing to be solving differential equations for numerical evaluation. In fact, this is very often done under the hood when you numerically evaluate e.g. Bessel, Airy or Legendre functions. Additionally, it is much more useful as practice for the future to implement a sturdy ODE solver than to embed a Newton's-method solution inside a big algebraic kludge. But again, this comes down to taste.

Additionally, one important reason why the numerical solution is very often the one that's implemented is that it is much more flexible in terms of its growth. For one, it is naturally poised in terms of cartesian coordinates, which you will likely find easier to handle, and it expands more easily to accommodate additional particles or any extra forces you might want to put in in the future. Again, it's up to what you want to use your codebase for in the future. For a planetary system with multiple planets, though, at some point you will want to worry about interactions between the planets, and you may want to explore alternative scenarios with stronger coupling between them.

Finally, I'll note that if all you want is a plot of the orbit, without any time dependence, then you can simply implement equation $(1)$ directly and it will give you all the geometrical information.

As you are trying to simulate the movement of a planetary system you are making a dynamical simulation so you should not describe the orbits geometrically but strive to obtain them as a consequence of solving dynamical equations (numerically if necessary). If you solve the dynamical equations you will obtain the positions and velocities of the planets at each step and the elliptical orbits will emerge as a consequence. This is easier and much more interesting than using Kepler equations. For that purpose you can use Verlet algorithm which would be something like this:

1. Give an initial increment h, t=0, and initial positions and velocities.
2. Evaluate the accelerations a(t).
3. Evaluate r(t+h) and w = v(t)+[h/2] a(t).
4. Evaluate a(t+h) using new positions r(t+h).
5. Evaluate v(t+h) = w+[h/2] a(t+h).
6. t=t+h. Go to 3.
• Cool! I hadn't heard about Verlet before. Could you elaborate why the Verlet algorithm would be more interesting than Kepler? – Qqwy Jan 8 '16 at 14:34
• @Qqwy In general, if you want to describe planetary motion, you should really be solving Newton's equations of motion directly via some numerical ODE solver. (It doesn't need to be Verlet, by the way - you could use Euler or Runge-Kutta or whatever rocks your boat in terms of precision and speed.) Doing it this way is often faster (and in fact many evaluations of special functions internally solve their ODEs), and it generalizes better to e.g. multiple bodies. – Emilio Pisanty Jan 8 '16 at 15:58
• @EmilioPisanty is the following statement correct? Keplers Equation can be used if I want to find out one point on the trajectory of an elliptical orbit. Euler, Verlet or Runge-Kutta work well(and fast) when I have the current position, and I want to calculate the position at a certain $t$ in the near future ($\Delta t$ should be small in relation to the orbit period for the result to be accurate). This makes them very useful for iterating, but not so useful when wanting to predict a single point in time. – Qqwy Jan 8 '16 at 17:19
• @Qqwy Roughly yes, if you only want a single point (or do you want to graph a stretch of the orbit?). However, Kepler's equation is not really that useful, because there is no closed elementary result for finding the trajectory at a given time. If the procedure here seems simpler to implement accurately to you than implementing a DE solver, with the added flexibility that that brings, then by all means go ahead. Numerically solving Kepler's equation repeatedly can be expensive, though. – Emilio Pisanty Jan 8 '16 at 17:27

I understand the second law, although I have no idea how to create a formula that uses it.

The most general form of this law is the law of conservation of angular momentum $\frac{d}{dt}L_\text{tot} = 0$; Kepler's law is an approximation which holds because the Sun is more massive than the planets (so you can regard it as approximately being located at the barycenter of the system, in terms of the distances of lines to remote planets).

Since $L = m \vec r\times \vec v$ for a single particle, and the cross product is equal to the product of the magnitudes of the vectors times the sine of the angle between them, this means that $|r||v|\sin\theta$ is some constant of motion.

So let's say you choose four parameters: $L, E, r_0, m$ for an orbit: angular momentum, total energy, starting radius, object mass. With $E = \frac12 m |\vec v|^2 + GMm/|\vec r|$ you can figure out what $|\vec v_0|,$ the starting speed, is. Then $L = m|\vec r||\vec v|\sin\theta$ gives you two possible $\theta$ for the velocity vector. Following the equations out, this gives you only two possible ellipses which will fit the rules. (You can see this by imagining taking a little step in that direction, then using $E$ to compute a new $|\vec v|$ and $L$ to compute two new $\theta$, then only keep the one that's approximately what you already have. Then take another little step, etc. You will be simulating Newton's laws at a very small scale, creating the ellipse directly.)