Understanding the Universe through Code: Planetary Motion
Tags: visualization, programming, math, physics
Published: 2021-01-15 03:58:12

We know that our planet Earth is in constant motion around the Sun.

Have you ever thought about how our ancestors discovered this?

I think most of us don't fully appreciate the difficulties that they encountered in making this discovery.

Even though we are not feeling it, the Earth is actually moving around the Sun at a staggering speed of 30km/s.

Just go out and look at the sky during the day time or at night and see if you can find any hints that will help you to reach this conclusion. It turns out that there aren't any trivial ones.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/luk2jpxmq84q8p4sw96j.jpg)

It took the effort of a lot of great scientific minds, over several years, to discover this.

In this article, we are going to make an effort to understand and appreciate the beauty of this discovery with the help of visualizations.

Before that, let me tell you about one of the most important observations made by our ancestors that lead to this discovery.

They carefully observed the motion of planets in the sky over several days and months and kept track of their positions very accurately.

They found out that the planets traced out a curve which looked like this.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/dh9wdqyr90va3b8ifrrr.jpg)

Can you believe that this observation was what lead them to speculate (correctly) that the Earth and the other planets are moving around the Sun?

How on earth did this "curve" that they observe in the sky lead them to this seemingly unrelated conclusion?

Let us try to find this out by creating a miniature solar system visualization.

And with the help of a few lines of code, we make our solar system model.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/w38erte8a2vxguv7my4s.gif)

There is Earth, Mars and the Sun.

Now let us shift the centre of the coordinate to the Earth. This is necessary because from our viewpoint, we feel that the Earth is stationary and the rest of the heavenly bodies are moving around us.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/csq5u6s39pu7sne44xjc.gif)

Now from this viewpoint, let us look at the trajectory traced by Mars.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/wuqe259ou0htc8bzb1kf.gif)

At first, it might look confusing, but observe this visualization for some time.

Does the curve look familiar?

The curve that we see in the sky is a small portion of the curve that we obtained now. But it looks squeezed when viewed from Earth's vantage point.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/6n1cyxfzcgb64y8ip5dg.jpg)

We obtained this by shifting the centre of the coordinate system to the Earth and tracing the path of Mars. Cool isn't it?

It took Johannes Kepler almost 40 years to analyse the data of the positions of the planets to finally come up with his laws that mathematically explained these ideas.

If you wish to get more clarity into these ideas, please see the workbook given below and play around with the visualizations and code yourself.

[Workbook](https://dl-next.herokuapp.com/workbook/5ff96ba94d6f6e0017a75a86) (Open in desktop)

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/4nb81mesrql2p84tblsl.gif)


PS - While creating the visualizations, I've assumed that the orbits of the planets are circular. But in reality, they are elliptical. The circular orbit which is an approximation gives us a reasonably good explanation. But to be more precise, we need elliptical orbits. And of course, do not take the scales seriously.

Thanks for reading !!!