# Implementing Runge-Kutta(RK4) into my n-body simulation

This topic is 3968 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hey guys, I know there are a lot of information and discussions out there regarding using the RK4 method but I am having the hardest time truly understanding them to the point where I can actually implement them in my simulation. Anyway, I am programming an n-body simulation of a solar system (upto 100 planets orbiting around a central star with upto 5 moons orbiting around each planet). These numbers are pretty arbitrary but I need some place to start. I have coded the simulation up using Euler Integration and have taken care of the graphical interface but, lets just say, the paths the planets are traveling as they orbit their sun are less then spectacularly precise. Thus, I definitely need to implement a more precise approximation method and whats more precise than the Runge-Kutta (RK4) method, right! Anyway, I understand that the Runge-Kutta method is basically just calculating the Euler integration of a variable multiple times over a given timestep but I just can't seem to understand how to handle the acceleration part of it. My equations are as follows: A(acceleration_ = F(total force) / m(mass of body); F = Summationof(G(gravitational constant)*m1(mass of some body)*m2(mass of another body) / d(distance between two bodies)^2;.........the summing is done for all bodies over all bodies V(velocity) = v_previous + A*t(timestep); X(position) = x_previous + v*t + (0.5*A*t^2); *Note A,F,d,v,and x are vectors..... Can SOMEONE!!!ANYONE!!! please help me understand this..I've been googling and reading discussions from forums for so long I'm daydreaming in text....code would be greatly appreciated!!!

##### Share on other sites
Damn't, come on guys.....no one has any ideas on this? I'm getting desperate......

##### Share on other sites
Use Verlet intergtation- much more accurate than Euler and faster than RK4 with comparable accuracy.

##### Share on other sites
I don't have time to write much, but one thing you have to understand is that you need to integrate the system as a whole when using RK4. This means you can't implement the integration as a function that operates on individual bodies one at a time, even though you can get away with that with Euler. The reason for this is that the forces at the "intermediate times" depend upon the "intermediate positions" of not only the body you're trying to integrate, but also of all the other bodies in the system.

I think the Baraff SIGGRAPH notes explain things reasonably well, IIRC.

##### Share on other sites
check out:

Gaffer on Games

it has a great explanation of how to implement RK4 as well as some other game-physics related articles (top corner of menu bar) that might help you.

##### Share on other sites
Quote:
 Original post by STEVIE GUse Verlet intergtation- much more accurate than Euler and faster than RK4 with comparable accuracy.

Thanks for all the responses guys. I think STEVIE G is right though. I probably should give up on RK4 and just use Verlet Integration. Verlet Integration was my first choice but I wanted to do it big so I decided to give RK4 a try. The problem is that I didn't know that RK4 would require me to have to calculate the force within timesteps and thus have to iterate over all the bodies in the system multiple times within a give timestep(not sure I have the CPU cycles for all that or that I even want to sacrifice speed for a bit more accuracy). Anyway, can anyone comment on the O(delta-t^4) in the equation. I understand that this is big-O notation but I'm not quite sure what I am supposed to do with it with respect to the rest of the equation......Thanks.

##### Share on other sites
You shouldn't ever calculate force in a n-body problem. You just need accelerations, and these could be computed without bothering with computation of "force".

You should use sixth order integrator (because it's painless to implement), to avoid problems. You would be probably unable to create some "stable" simulations even with sixth order integrator without an energy dampening.

Have you calculated accuracy requirements? 64 bit floating point number has finite accuracy, and square root would kill it quite a bit. Also summing too low and too high number would result in original high number, the small one is simply thrown out.

You might look at bob jenkins page, he has few quite easy to write integrators, and he provided requirements for accuracy. (steps per orbit, divergence of the integrators...)

BTW this integrator is symplectic.

velocity = prevVelocity + acceleration*delta
Position = prevPosition + velocity*delta

(spirals out)

1. 1
2. 2
Rutin
19
3. 3
khawk
18
4. 4
5. 5
A4L
11

• 12
• 16
• 26
• 10
• 44
• ### Forum Statistics

• Total Topics
633768
• Total Posts
3013747
×