# Intro

For an N-body simulation of calestial bodies I need to calculate

• on the one hand the accelerations of the calestial bodies based on the received gravitational forces [done]
• and on the other hand the new position of the individual calestial body a time t. [todo].

# Problem

How do I calculate the new position at time t of a body with a constantly changing acceleration?

# Some additional information

• The acceleration is computed in step one for all bodies in the system and after calcualting the new position, then it starts from the beginning with the new calculated position and velocity from step 2.
• For each body I know the mass, the start and current position (from step 2), the initial and current velocity (from step 2), the acceleration (from step 1).

## What I have did so far

• I calculated the current accelerations for each body based on the received gravitational forces.
• I calculated the new position and velocity based of an constant acceleration which is obivsoulsy wrong, because in each iteration the distance betweens the bodies changes and therefore the recevied force which results in a new acceleration.

## 1 Answer

For each of the $$N$$ bodies, define its position as $$x_i$$ and its velocity as $$v_i$$, then write the differential equation for $$x_i$$ and $$v_i$$ as follows

$$\dfrac{d}{dt} x_i = v_i$$

$$\dfrac{d}{dt} v_i = a_i$$

Now $$a_i = \dfrac{1}{m_i} F_i$$

where $$F_i = \displaystyle \sum_{ j , j \ne i} \dfrac{ G m_i m_j (x_j - x_i) }{\| x_i - x_j \|^3}$$

The state vector of the whole system is defined as

$$x = [x_1, x_2, x_3, ..., x_N , v_1, v_2, v_3, ..., v_N ]^T$$

Then you have

$$\dot{x} = f(x)$$

And you just have to integrate this vector differential equation numerically, using an accurate method like the Runge-Kutta $$4$$-th order method. This will give you the position of all the $$N$$ bodies as a function of time.

• @Dawid I've edited solution to correct the expression for $F_i$, now it should be correct. Commented May 8, 2022 at 15:37
• thanks, the calculation of Fi I already done on my own. I also added a softening factor of 0.01 to avoid division by zero if there is a colision between the bodies. Actually was Runge-Kutta 4-th the method, that I was looking for. Thanks for the advice. Commented May 8, 2022 at 15:39
• To be sure, can you tell me what delta t (dt) is exactly? Is it the difference between the current time (ti) and the initial time (t0) or the difference between the current time (ti) and the last point in time of the calculation (ti-1)? Commented May 9, 2022 at 13:31
• Looks like you don't know Calculus at all. Commented May 9, 2022 at 13:52