Yet-another RK4 n-body problem

In summary: Mathematica. Good luck! In summary, the conversation discusses the implementation of the 4th order Runge-Kutta method for solving an n-body problem. The RK4 method for an initial value problem is explained, along with the equations for calculating the next values of y and t. The challenge of coding and understanding the method for an n-body problem is also discussed. The general steps for implementing the method, including calculating k1, k2, k3, and k4 for each variable, are outlined.
  • #1
pretzsp
4
0

Homework Statement



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

Homework 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

where

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)

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:
Physics news on Phys.org
  • #2


Hi there,

I can understand your confusion with implementing the RK4 method for an n-body problem. It can be quite challenging to code and understand, but I'll try to break it down for you.

First, let's look at the general steps for the RK4 method:

1. Define an initial value problem (IVP) with an initial condition y(t0) = y0.
2. Choose a step size h.
3. Use the RK4 formula to calculate the next values of y and t.
4. Repeat until you reach the desired final value of t.

Now, for an n-body problem, we have multiple variables (position and velocity) for each body, so our IVP will have multiple equations. Let's say we have n bodies, then our IVP will look something like this:

y' = f(t,y), where y = [x1, y1, vx1, vy1, x2, y2, vx2, vy2, ..., xn, yn, vxn, vyn]

Here, x and y represent the position coordinates and vx and vy represent the velocity components for each body. Now, we can use the RK4 method to solve this IVP and get the updated values for all the variables at each step.

Let's say we have n bodies, then our RK4 formula will look like this:

y_(n+1) = y_n + (1/6)(k1 + 2 k2 +2 k3 + k4)

where y_n is the current value of the variables and k1, k2, k3, and k4 are calculated using the following equations:

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)

Here, h is the step size and f(t,y) represents the equations of motion for each body. So, for each step, we need to calculate the values of k1, k2, k3, and k4 for all the variables and use them to update the values of y at the next step.

I hope this helps in understanding the general concept of implementing the RK4 method for an n-body problem. You can use this as a guide for coding
 

1. What is an RK4 n-body problem?

An RK4 n-body problem is a type of mathematical problem that deals with the motion of multiple objects (such as planets, stars, or particles) interacting with each other through the force of gravity. The "RK4" refers to the fourth-order Runge-Kutta method, which is a numerical technique used to solve the differential equations that govern the motion of these objects.

2. How does the RK4 method work in solving n-body problems?

The RK4 method works by breaking down the problem into a series of smaller steps, where the positions and velocities of the objects are updated based on the forces acting on them. This process is repeated multiple times until the desired level of accuracy is achieved. The RK4 method is known for its efficiency and accuracy in solving n-body problems.

3. What are the applications of solving n-body problems using the RK4 method?

N-body problems are commonly used in various fields such as astrophysics, aerospace engineering, and computer graphics. The RK4 method allows for the simulation of complex systems, such as planetary orbits, galaxy interactions, and the motion of spacecraft and satellites.

4. Are there any limitations to using the RK4 method for n-body problems?

While the RK4 method is generally considered to be a reliable and efficient numerical technique, it does have some limitations. It may not accurately predict the behavior of systems with highly non-linear interactions, and it may also struggle with long-term simulations where small errors can accumulate over time.

5. How can the results of an RK4 n-body simulation be validated?

The results of an RK4 n-body simulation can be validated by comparing them to known analytical solutions, such as the laws of planetary motion for the solar system. Additionally, the simulation can be run with different initial conditions and compared to observed data to ensure its accuracy.

Similar threads

  • Programming and Computer Science
2
Replies
36
Views
3K
  • Introductory Physics Homework Help
Replies
19
Views
2K
  • Programming and Computer Science
Replies
15
Views
2K
Replies
5
Views
366
  • Introductory Physics Homework Help
Replies
2
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
6
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
1K
  • Introductory Physics Homework Help
Replies
1
Views
926
Replies
33
Views
3K
  • Introductory Physics Homework Help
Replies
32
Views
1K
Back
Top