 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 911) 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.
 Python 2.5  the python interpreter.
 Numeric 24.2  array calculations needed for pygame.
 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

 Introduce the Newton's Law of Gravity: F = G m1 m2 / r2.
 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.
 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.
 Choose different initial conditions leading to an elliptical orbit (shorten the initial velocity vector) and rerun the simulation. The simulation is clearly broken.
 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).
 Have student teams implement the simple fix of smaller step size.
 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!).