# How to simulate planets orbit curce around the sun ?

1. Oct 28, 2011

### kjensen

How to simulate planets orbit curve around the sun ?

Hi

I'm about to write a java program where I wish to simulate planets orbit curve around the sun, so that I can set the speed of a planet and mass of planet and sun and simulate the curves around the sun (just basic simulations). I only know high-school level physics but I know university level mathematics. I want to build my programs on newtons laws. I know that there are plenty of others programs that can do such simulations, but I wish to know the theory and math behind it so that I can build my program from ground up. I know newton laws of gravity, but I do not understand how to apply them to a simulation, and when I fool around with the formulas I manage to get different results depending on how I interpreted applying the formulas to a simulation. I hope that someone here will be so kind to explain me the theory of doing such a simulation or tell me where I can find books, documents, or websites about it. I have tried searching on the net, but have just got more confused. Thanks.

Last edited: Oct 28, 2011
2. Oct 28, 2011

### dacruick

Hi there,

Keplers laws of planetary motion will explain to you the physics behind the orbit, which I think is probably all you need to get a Johandle on the situation. excuse the cheesy pun.

http://en.wikipedia.org/wiki/Kepler's_laws_of_planetary_motion

One thing I notice is that you say you can set the speed of the planet and the mass of the planet, but since planets travel in elliptical orbits, the speed of the planet will be a function of the distance from the sun. Setting the speed of the planet will not suffice for a stable model. There is a value called eccentricity which depicts how elliptical an orbit is. an eccentricity of 0 is a circular orbit, but you will come across all of this if you read through the link I posted.

3. Oct 28, 2011

### DaveC426913

In terms of simulating:
Decide on your accuracy (some algorthms are harder than others but more accurate).
Decide if you want it 2D or 3D (recommended: 2D)
Decide if the orbiting bodies have significant mass compared to the parent (if no then your job is easer. If yes, you can simulate star clusters).

What you do is make nested iterative loops.

Plot all points in orbits, putting their xy coords in an array
Calc the force vector pulling on them from the parent (prop to mass, inverse prop to sqaure of dist)
From the force, calc the change in velocity.
From that, calc the new position.
Do this for all point, then update the display and repeat.

4. Oct 28, 2011

### kjensen

Thanks a lot for your reply. But I thought that the shape of the curve is depending on the total mechanical energy of the system E_mek which is equal to E_kin + E_pot (E_mek = E_kin + E_pot = 1/2*G*(M*m)/r

Therefore I thought that the curve form is dependent on the speed and/or the mass of planet (and/or the mass of the sun)

My plan was to be apple to simulate all sort of curves (comets and so on) including hyperbolic and parabolic curves with the speed and mass as starting conditions. But maybe that is possible with Keplers laws, even though I found it hard to see how to make a simulation out of it!

5. Oct 28, 2011

### Grizzled

... and if you stop at that, you will soon discover that your simulated planets have wandered off, or crashed into the Sun, or did something else equally bizzare. The worst possibility (as regards consequences of the error) is that results may look credible enough to pass cursory examination but be nevertheless wrong.

The reason for the above is accumulated systemic error. Increments (distances between the points on your graph) are by necessity finite. That means that the force, acceleration etc you calculated at the start of next step is wrong for the rest of that step. Even if you are crafty and use a central spot or several of them - that's still not enough. It will only reduce the error but not eliminate it.

To make sure your planets behave, you need to ensure that (at every step of your calculations!) they comply with conservation laws of energy and momentum. Force-adjust the results at every step to make sure your model at least doesn't break the natural laws. Not too much anyway :-)

6. Oct 28, 2011

### kjensen

Thanks a lot for your reply. But does that mean that some algorithms might give different shapes? I would to make it as precise as possible (only 2D for now).

So that should give the same result no matter the step size? If so then I'm not sure why that is, but then I have enough to make the program.

7. Oct 28, 2011

### kjensen

Thanks a lot for your reply. That was also what I have been thinking according to that the step size will influence the result. Do You know anywhere (documents, books or something) where it is described how to make the best possible model? (I'm a computer programmer and would like the very best model possible. My plan is to start in 2D (with a single planet) and then move to 3D and then up with a complete solar system simulator)

Last edited: Oct 28, 2011
8. Oct 28, 2011

### DaveC426913

I've built these several times and they are stable over long periods.

However, what I'm building are not models of solar systems, they're simply generic orbital paths of up to 20 or so stars in a cluster. There's no accuracy to any real system, but they are stable.
[/QUOTE]

9. Oct 28, 2011

### DaveC426913

Oh. In your OP, you said "basic simulations", which I interpreted as "rough".

10. Oct 28, 2011

### kjensen

Actually I meant with a single planet orbiting around the sun, and when I get that working then with several planets in 2D and then hopefully ending up with a 3D solar system simulator (a solar system space travel program). I just haven't been able to find books and/or documents about how to make such simulations (also I would like to make a simulation where it is possible to add new objects).

Last edited: Oct 28, 2011
11. Oct 28, 2011

### kjensen

I know that there exist ephemerides but I would like to make it as a simulation as I am very interested in astronomy and would like to understand the mechanisms in the solar system.

Last edited: Oct 28, 2011
12. Oct 30, 2011

### kjensen

I already knew from experiments that the step size would affect the curve form. Can you (or someone) say more about how to force-adjust at every step (formulas, code fragments or so)?

Also what do you mean with not breaking the natural laws too much anyway? Is it not possible to make a perfect simulation, and if not then why not? And if it is possible to make a perfect simulation where can I find informations about it? I have tried to search for documents and at Amazon for books about it, but I haven't been able to find any documents or books about high quality computer simulations of planets orbiting the sun!

Also my questions is not based on passing any exams, but is the starting point in making a perfect planet simulation program (I will start in 2D with one planet), and therefore I would like to know everything about the physics related to it. I have a degree in computer science (and also I have taken a few courses in physics), so I know how to make graphics etc., but I need to understand the physics and mathematics of planetary computer simulations a lot better.

13. Oct 30, 2011

### DrStupid

Re: How to simulate planets orbit curve around the sun ?

As DaveC426913 already wrote you have to decide which numerical method you want to use. This may strongly influence the structure of your program. The best choice would be a symplectic multi-step integrator for second order IDEs with variable step width. But there are a lot of simple methods that give sufficient results. I prefer a Runge-Kutta-Nyström method.

Than you should think about the representation of the data. There are several different possibilities such as putting the data into different arrays or encapsulating them into objects. I prefer objects for each body and an array for all bodies. That makes it very easy to remove or add bodies even during a running simulation.

I would not recommend to limit the program to 2D first because it might be very difficult to extend it to 3D later. As you have to use vector algebra in both cases there is actually no reason for such a limitation.

As you want to extend your simulation to a space travel program you have to implement a full simulation of Newtons gravitational law with individual mass for different bodies including the possibility of external forces (e.g. from rocket motors). But you may use a hybrid simulator with Kepler for Sun and planets and Newton for everything else.

It is not possible because there is no global solution for n-body problems with n>2. Thats why such systems can only be approximated numerical and such methods have a limited accuracy.

Last edited: Oct 30, 2011
14. Oct 30, 2011

### TurtleMeister

Hi kjensen. I think http://www.cs.princeton.edu/courses/archive/spring11/cos126/assignments/nbody.html" [Broken] may be what you're looking for. I used it to write a java simulation which I later ported over to VB with added bells and whistles. I learned a lot and had a lot of fun with it.

Last edited by a moderator: May 5, 2017
15. Oct 30, 2011

### kjensen

Re: How to simulate planets orbit curve around the sun ?

Thanks a lot for your reply. If I google "symplectic multi-step integrator" then I get a lot of interesting stuff (documents, books and so on). It might be what I am looking for! Do you know any books or documents where it is related to computer programming (any programming language). Thanks.

16. Oct 30, 2011

### kjensen

Thanks a lot for your reply. The link looks really great and even though I will build my program from ground up with a model as precise as possible then it might be worth figuring out the exercise (but it looks rather similar to another programming experiment I have been doing). But I think that the curve form will depend on the step size doesn't it?

Last edited by a moderator: May 5, 2017
17. Oct 30, 2011

### tony873004

If you want to learn to walk before you learn to run, I'd start with Euler's method. It's the method DaveC described in post 3. It will give a very intiutive understanding of what you're doing. Leapfrog and RK4 are fine, but more difficult to code. At this point high accuracy is not an issure. You're creating a single planet moving around a stationary star, just to get a feel as to how to do it. So who cares if your simulation isn't accurate enough to predict solar eclipses decades into the future?

Actually Euler's method can be accurate enough to predict solar elipses decades into the future if you take a small enough time step. (delta t). The higher order integrators allow you to achieve good accuracy at larger timesteps, meaning it will take your computer less time to simulate your system decades into the future.

Later, you can upgrade to a higher order integrator. Since an Euler step is performed in the RK4 method, you'll be building upon what you've already done.

18. Oct 30, 2011

### kjensen

Thanks a lot for your reply. Of course you are right, and I will design my program so that I easily can change the 'calculation engines' along the way. But now when I have started a thread about it then I would like to know what the best possible method is, so that I don't have to start another thread later on.

19. Oct 30, 2011

### DrStupid

Re: How to simulate planets orbit curve around the sun ?

Unfortunately not. Maybe you will find corresponding sources in the references of your google-results. But you should also be able to write your program without literature. The program code will mainly result from the selected integrator and the representation the physical parameters. For example http://www.drstupid.de/Newton.html" is based on this Runge-Kutta-Nytröm integrator:

$\begin{array}{l} a_1 = a\left( {x_n } \right) \cdot \Delta t \\ a_2 = a\left( {x_n + {\textstyle{{\Delta t} \over 2}}v_n + {\textstyle{{\Delta t} \over 8}}a_1 ,v_n + {\textstyle{1 \over 2}}a_1 } \right) \cdot \Delta t \\ a_3 = a\left( {x_n + {\textstyle{{\Delta t} \over 2}}v_n + {\textstyle{{\Delta t} \over 8}}a_1 ,v_n + {\textstyle{1 \over 2}}a_2 } \right) \cdot \Delta t \\ a_4 = a\left( {x_n + \Delta t \cdot v_n + {\textstyle{{\Delta t} \over 2}}a_3 ,v_n + a_3 } \right) \cdot \Delta t \\ x_{n + 1} = x_n + v_n \cdot \Delta t + {\textstyle{{\Delta t} \over 6}}\left( {a_1 + a_2 + a_3 } \right) \\ v_{n + 1} = v_n + {\textstyle{1 \over 6}}\left( {a_1 + 2a_2 + 2a_3 + a_4 } \right) \\ \end{array}$

[http://theory.gsi.de/~vanhees/faq/gravitation/node62.html" [Broken]]

As it is a 4-step algorithm intermediate data must be handled. For every step 4 accelerations (for different positions) must be calculated for every body and kept in memory. I solved this problem by an array of 4 accelerations for each body:

Code (Text):

function Body(Mass,sx,sy,sz,vx,vy,vz,Name) {
this.m = Mass ; this.s = new Vector3D(sx,sy,sz) ; this.v = new Vector3D(vx,vy,vz) ;
this.F = new Vector3D(0,0,0) ; this.a = new Array ; for (var k=0;k<4;k++) this.a.push(new Vector3D(0,0,0)) ;
[...]
}

Within each time step the accelerations a[0] - a[3] are calculated using the first four equations of the algorithms. Than the velocities s and v are updated according to the last two equations using the accelerations calculated before. Each step of the algorithm must be calculated for all bodies at once. In doing so the force F is accumulated for each pair of bodies (in order to calculate each force only once).

With other algorithms and other data management you will get different code. If you select a numerical method and your favorite data management you may already have an idea about the structure of your program.

Last edited by a moderator: May 5, 2017
20. Oct 30, 2011

### DrStupid

You are right. But as a compromise I would suggest http://en.wikipedia.org/wiki/Verlet_integration#Velocity_Verlet". It is almost as simple to understand and to implement as Euler but it conserves the energy.

Last edited by a moderator: Apr 26, 2017