1. Limited time only! Sign up for a free 30min personal tutor trial with Chegg Tutors
    Dismiss Notice
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: N-body simulation - straight line orbits

  1. Apr 5, 2017 #1
    1. The problem statement, all variables and given/known data

    The problem is your typical N-body simulation, implemented using Python and Numpy. The implementation specifically calls for using the Euler-Cromer method. For this particular case I used the Sun and the first 4 planets of the solar system.

    Essentially the problem is I'm getting straight orbits, tending towards infinity. Attached is an image of the output.

    2. Relevant equations

    The following are the equations used to evaluate the system :
    $$\mathbf{a_i} = \sum^N_{j \neq i} \cfrac{Gm_j}{|\mathbf{R}_{ij}|^3}\mathbf{R}_{ij}$$
    $$\mathbf{v_i}(t + \Delta t) = \mathbf{v_i}(t) + \mathbf{a_i}\Delta t$$
    $$\mathbf{x_i}(t + \Delta t) = \mathbf{x_i}(t) + \mathbf{v_i}\Delta t$$

    3. The attempt at a solution

    Below is my code (commented as best as I can) - I tried various slight alterations, such as calculating the position first, the the velocity, rather than the other way round - but to no avail!

    My suspicion is something related to the acceleration - I simply cannot figure out what. I noticed (after printing 10,000 values!) that as the x-coordinate of a body approaches zero, the y-coordinate simply increases more and more, and never goes below it's previous value. In order words I noticed the following issue with regards to the y-positions (which I cannot understand why or if its actually an issue) :
    $$y_n < y_{n+1} \ \text{(always)}$$

    Appreciate any help - Cheers!

    Code (Python):

    import numpy as np

    from matplotlib import pyplot as plt


    # Defining Constants
    AU = 149.6*(10**9)
    G = 6.6743*(10**(-11))
    deltaT = 24*3600

    # Initializing Planetary Data
    # planetary_data = [['name', mass, initial position, initial velocity]]
    planetary_data = [['Sun',1.989*(10**30),0*AU,0],

    # Structures to store vel(ocity) vectors and pos(ition) vectors after one full iteration on all bodies
    vel_vectors = np.array([[0,0],

    pos_vectors = np.array([[0,0],

    # Structures to store temp_vel(ocity) vectors and temp_pos(ition) vectors during calculations
    temp_vel_vectors = np.array([[0,0],

    temp_pos_vectors = np.array([[0,0],

    Attached Files:

    Last edited by a moderator: May 9, 2017
  2. jcsd
  3. Apr 5, 2017 #2


    User Avatar
    2017 Award

    Staff: Mentor

    Assuming you use SI base units, your initial velocities are a large fraction of the speed of light (or even above that for Venus). At that speed you won't see a relevant gravitational influence of the Sun.
  4. Apr 5, 2017 #3
    Much better now - I feel silly for copying those off my assignment sheet without noticing the mistakes! Cheers!

    Attached Files:

Share this great discussion with others via Reddit, Google+, Twitter, or Facebook

Have something to add?
Draft saved Draft deleted