Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Homework Help: Yet-another RK4 n-body problem

  1. Jun 5, 2010 #1
    1. The problem statement, all variables and given/known data

    Implementing the 4th order Runge-Kutta method for an n-body problem

    2. Relevant equations

    The RK4 method for the IVP y' = f(t,y), y(t0) = y0, is given by
    y_(n+1) = y_n + (1/6)(k1 + 2 k2 +2 k3 + k4)
    t_(n+1) = t_n + h


    k1 = h f(t_n,y_n)
    k2 = h f(t_n+ h/2, y_n + k1/2)
    k3 = h f(t_n+ h/2, y_n + k2/2)
    k4 = h f(t_n+ h/2, y_n + k3)

    3. The attempt at a solution

    I'm trying to simulate some n-body problems using the Runge-Kutta 4th Order method on Mathematica. I have something that works, for just the most simple problem of one body orbiting about a fixed mass.

    step[data_] := (
    t = data[[1]]; kx1 = data[[2]]; kx2 = data[[3]]; kx3 = data[[4]]; kx4 = data[[5]]; kv1 = data[[6]]; kv2 = data[[7]]; kv3 = data[[8]]; kv4 = data[[9]]; v = data[[10]]; x = data[[11]];

    {t + h,

    h ((1/m2) GravForce[m1, {0, 0}, m2, x]),
    h ((1/m2) GravForce[m1, {0, 0}, m2,
    x + k1/2]),
    ht ((1/m2) GravForce[m1, {0, 0}, m2,
    x + k2/2]),
    h ((1/m2) GravForce[m1, {0, 0}, m2,
    x + k3]),

    h v,
    h (v + k1 /2),
    h (v + k2/2),
    h (v + k3),

    v + (1/6) (k1 + 2 k2 + 2 k3 + k4),
    x + (1/6) (vk1 + 2 vk2 + 2 vk3 + vk4)


    But I'm fairly sure that this is entirely wrong despite the fact that it gives me a "correct" result - for one thing, it's not significantly more accurate with the same value of h than Euler's Method. The other is that the functions themselves look completely and utterly wrong.

    Could someone help me out here? This is really spinning my head around.

    I'm not really looking for help with Mathematica, just more of a general guide on how Runge-Kutta is supposed to work for a system like this.
    Last edited: Jun 5, 2010
  2. jcsd
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook

Can you offer guidance or do you also need help?
Draft saved Draft deleted