Last weekend whilst stuck in traffic on the way to Sacramento, I was talking (ahem, nerding out) with a friend about data, visualizations, coding, finding our respective niches, doing meaningful work, etc. At some point, I told him that I began learning Python for the first time when I was but a humble freshman in physics at Purdue, and that the thing I find most exciting about programming is making the computer do the thing I want it to. Thus, when we learned how to model a ball bouncing on a floor, or a spaceship orbiting the earth, or a skydiver (whom I dubbed “maniac”) jumping off of something, it was basically the coolest thing ever for me.
What struck me was that he was very surprised when I said that I don’t still mess with coding stuff on the side, instead reserving it for work. The thought had never crossed my mind, actually!
So, the next day, when I found myself with a few minutes to spare, I dug out my external hard drive, unzipped my “School” folder which has every file I used in nearly 15 years of school assignments from high school through grad school, and found a few of my old Python files from that freshman physics class (Physics 162, to be precise). I had to install a few things, including the right version of Python and then VPython on top of it, before I could actually run anything, but I did all of that with no problems. Then I recreated my model of the Earth orbiting the Sun, appending a trail to the Earth so I can see where it went:
Basic stuff. BO-RING.
(but I was still glad I got it to work. squee!)
So then I decided that I would add the Moon and make it orbit the Earth:
Still not fun enough for me, because I coded in each of the forces by hand: the force between the Earth and the Sun, between the Earth and the Moon, and between the Sun and the Moon. I generally try to avoid doing anything “by hand” when it comes to coding, so I decided to make a list of all the planets and then iterate through the list to calculate the net force on each, thus making a slightly more accurate (or slightly less inaccurate) model than just the force between the Sun and each planet.
Here’s what happened:
You’ll notice two things here.
- Mercury goes bat-shit crazy very early on.
- The Earth + Moon system is in a degenerating orbit around the Sun. In fact, when the system crosses the path of Mercury, the moon gets stripped off and begins its own orbit around the Sun, independent of the Earth.
Because this is obviously not how our Solar System behaves, we can safely assume that something is incorrect. There are a few things I could have gotten wrong which would result in this kind of behavior, including masses, orbital distances, and orbital speeds for Mercury and the Earth + Moon system, so I went back to trusty Wikipedia to make sure I had the all the right numbers.
In my original code, instead of just using the actual orbital speed for any of my Solar System bodies, I calculated the speed based on the length of the orbit in years and the distance from the Sun:
Using the formula made me feel cool (I suppose feeling “cool” is relative) but it was difficult to check and I was impatient, so I just plugged in the average orbital speeds from Wikipedia. And I feel exactly zero badly about it. So there!
And here is why I feel zero badly about it:
Finally, somewhat normal-looking orbits! Here’s a closer view of the inner Solar System:
The Earth + Moon system is still falling into the Sun gradually, but it’s a good bit slower now (that’s a relief). And Mercury is super off-center because I used its aphelion distance but its average orbital speed; however, I still count it as a win because at least it’s now orbitting instead of going bat-shit crazy.
Obviously, I haven’t created a perfect model here. There is totally room for improvement, and that’s alright! If you found this fascinating, you might check out this other blog post I unearthed in my searching.
And so, dear readers, I think I can safely say that I’ll be trying to do more coding exercises in my free (hahahaha…no really, I’m serious) time.
Until next time,
Edit, later that same day:
PS – You can find my code on my Github!