# I Newtonian Gravity Simulator

#### Ben S

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
https://www.dropbox.com/sh/q7pz43cmpxx9a48/AAA_zboKGLaH3XZ-crl_JRsma?dl=1

Last edited:
Related Astronomy and Astrophysics News on Phys.org

#### jedishrfu

Mentor
Welcome to PF!

What have you done to validate your modeling?

Will you post the code?

#### Ben S

jedishrfu said:
Welcome to PF!

What have you done to validate your modeling?

Will you post the code?
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.

#### Drakkith

Staff Emeritus
2018 Award
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)!

#### Ben S

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:

#### DrStupid

Inelastic collisions would be much more realistic and easier to implement.

Currently when two masses approach each other very closely, they both gain extremely high velocities
Do you use an integrator with step size control?

#### Ben S

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!

Inelastic collisions would be much more realistic and easier to implement.
Do you use an integrator with step size control?
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.

"Newtonian Gravity Simulator"

### Physics Forums Values

We Value Quality
• Topics based on mainstream science
• Proper English grammar and spelling
We Value Civility
• Positive and compassionate attitudes
• Patience while debating
We Value Productivity
• Disciplined to remain on-topic
• Recognition of own weaknesses
• Solo and co-op problem solving