Python Orbits

In this interactive programming exercise, students will refine a naive implementation of an orbit calculator so that it produces a more correct physical simulation.

Educational objective

Instill a healthy distrust for numerical computation in students.

Audience

This material was presented to a group of high school students (grades 9-11) with varying computer experience.

Prerequisites

A number of packages need to be installed on the machine before the students can run the simulation. We have included copies of the windows installer at reflectometry.org.

  1. Python 2.5 - the python interpreter.
  2. Numeric 24.2 - array calculations needed for pygame.
  3. Pygame 1.7.1 - graphics engine.

Code

The file orbits.zip must be expanded on the students machine. The student will modify orbit.py and run the program by clicking the filename.

Outline

  1. Introduce the Newton's Law of Gravity: F = G m1 m2 / r2.
  2. Introduce the idea of updating the position and velocity in response to the force at time t. Show how this is done in the source code.
  3. Have students run the simulation with parameters for circular orbits. The program includes a visualization of a planet going around a sun. It uses real numbers for the masses, distances and velocities.
  4. Choose different initial conditions leading to an elliptical orbit (shorten the initial velocity vector) and rerun the simulation. The simulation is clearly broken.
  5. Discuss problem. The fixed step size for the simulation breaks down at perihelion where forces are changing rapidly. Get suggestions for solutions: small step size everywhere takes to long, so use dynamic stepping. (We should have presented the real solution, applying a correction factor in addition to the dynamic step size, but didn't have the code ready in time for class).
  6. Have student teams implement the simple fix of smaller step size.
  7. Remainder of the class, different students did different projects, including replacing the picture of the earth with others, implementing dynamic step size, implementing a binary star system (make sure the velocity of the center of mass is zero!).