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

I Newtonian Gravity Simulator

  1. Mar 5, 2017 #1
    I recently put together a simple 2D gravity simulator and I'd like to get some feedback or suggestions you may have for future updates. I'm planning to make a 3D version and improve the animation (framerate, realism, etc).

    This first simulation includes the Sun, Mercury, Venus, Earth, and Mars. The initial positions and velocities were taken from NASA's JPL site. Notice how the Earth's orbit on the purple line is ~365 days shown by the time display on the left. Planets are not to scale in this model because they would be too small to see.
    The inputs for the above simulation are shown below.
    There are actually more inputs than this, but these are the most important ones. Additional inputs include the animation speed, viewing area (whether to include the full trajectory of every mass on the screen or to constantly update the viewing area size as shown in the final gif below), trajectory line visibility, and whether to show actual planet size or scaled up sizes for visibility.

    This simulation shows the Earth (largest grey circle), the Moon (following the yellow line), and a GSO satellite which orbits at the same rate as the Earth rotates. The simulation period is 7 days. The Earth and Moon are to scale, but the satellite size was increased so that it would be visible.

    This final simulation does not model a real system of planets like the two previous simulations. Instead, it starts with nine masses at equal distances from each other. All masses except the center mass are given an initial velocity in a counterclockwise direction around the center mass. It can produce some interesting patterns.

    How it works
    Variables are defined for the initial position (x and y), initial velocity (x and y), mass, and radius. For each planet, the acceleration vectors due to all other planets are calculated in the x and y directions using Newton's law of universal gravitation. Given a small time step, the change in velocity and position for each planet can be found using kinematic equations. I assumed the acceleration was nearly constant over the small time step. With this, the program outputs the position, velocity, and acceleration of every planet at distinct time steps over some period of time. The program is written in MATLAB.

    Future work
    • Improve animation frame rate
    • Convert to 3D
    • Apply modern gravitational theories like general relativity
    MATLAB code (check the 'read_me.txt')
    Last edited: Mar 5, 2017
  2. jcsd
  3. Mar 5, 2017 #2


    Staff: Mentor

    Welcome to PF!

    Nice work. Was this a school project?

    What have you done to validate your modeling?

    Will you post the code?
  4. Mar 5, 2017 #3
    Thanks. I made this simulator for fun.

    The only real validation that I've done was checking the orbital periods of the planets after inputting the initial positions and velocities of the planets using data recorded by NASA JPL. Some error is expected because the acceleration is assumed to be constant over each time step, but I might be able to calculate the maximum error in the position of each planet at each time step.

    I'll see if I can attach the MATLAB code to the original post.
  5. Mar 5, 2017 #4


    User Avatar
    Staff Emeritus
    Science Advisor

    I've made a very, very simple orbital simulator in another program, but I'd love to be able to use Matlab to do this (I'm taking a Matlab class right now)!
  6. Mar 5, 2017 #5
  7. Mar 7, 2017 #6
    I learned today that the animation runs a lot smoother on my university's lab computers, so I added a function that allows you to set the number of planets in the simulation and it randomly generates initial positions and velocities. The animation ran smoothly with up to 200 planets so I might be able to add some interesting gifs or video soon. I'd like to try something similar to the last gif in the original post but with 100 masses and longer capture time.

    I might also try to make the program detect when masses collide, so I'll probably start with simple elastic collisions. Currently when two masses approach each other very closely, they both gain extremely high velocities, which makes it difficult to keep every mass in the viewing area.
    Last edited: Mar 7, 2017
  8. Mar 8, 2017 #7
    Inelastic collisions would be much more realistic and easier to implement.

    Do you use an integrator with step size control?
  9. Mar 25, 2017 #8
    It's been a few weeks since the last post because I've been busy with school and searching for internships, but I made some progress on collision detecting. Know an internship position I would fit in? Let me know!

    I added an option that allows you to set how much kinetic energy is conserved in the collisions, from 0% to 100% conserved. With 0% KE conserved, the collisions are perfectly inelastic and the planets stick together. With 100% KE conserved, the collisions are elastic and the planets bounce off of each other. I used conservation of momentum to calculate the final velocities of each planet after a collision.

    I don't use an integrator with step size control but I'll look into it.
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook

Have something to add?
Draft saved Draft deleted