Modeling Motion Python Examples
This page serves as an index to the program code referred to in the computer science section of Modeling Motion. I will post individual programs and saved scripts of interactive sessions from the lectures.
Week 1
- for loop
- while loop
- defining functions
- Lissajous - A colorful Lissajous figure in 3D shows use of the curve object.
Week 2
- SIR - The SIR example from Calculus in Context translated into Python
- SIRPLOT This version graphs the data computed.
- dec2bin Convert decimal floating-point numbers to their binary representations.
Week 3
- sequence.py - Python Translation of SEQUENCE from the Calculus book
- length.py - LENGTH program from Callahan
- babylon.py - BABYLON from Callahan
- Lissa2.py - Just a pretty shape using trig and VPython.
- normalize.py
- sir-limit.py - shows limit behavior of Euler's method by showing successively better approximation to the SIR curves.
- logistic.py - compute the shape of the logistic equation numerically.
- logistic-limit.py - successive approximations to the logistic curve.
- scope.py - An explanation of Python's variable scope rules.
- week3lecture.py - Code from Thursday's lecture.
Week 4
- readdata.py - A short program demonstrating how to read data from a file. It requires the file "data.txt" to run.
Week 5
- Midterm - Solutions to the computing part of the midterm exam.
- gendata.py, and differ.py - The two programs from Tuesday's lab—the first prompts the user for a formula and generates data for it in a file. The second reads in that data, plots the function as well as its derivative.
Week 6
- elephants.py my version of the elephant model from the calculus midterm.
- arms-race.py my version of the arms race model from the calculus midterm.
- arms-graph.py another version of the arms race model from the calculus midterm which graphs x vs y for a variety of initial conditions showing the general behavior of the model.
- ln.py uses a binary search algorithm to find the natural log of a number greater than one.
Week 7
- galaxy.py - A numerical simulation of point masses following Newton's Universal Law of Gravitation.
- arrow.py - a program which fires an arrow at a target to be used as a starting point for a binary search optimization technique.
- dogs.py - A simple example using classes and inheritance.
- pendulum.py is a module that defines a Pendulum class. trypendulum.py is a test program to show how to use it. You need to put pendulum.py in the same directory as the program you are trying to use it with and import it as shown in trypendulum.py.
- riemannoo.py - An object-oriented (OO) example that defines a class RiemannSum for computing areas under curves and lengths using Riemann sums. rietest.py Creates instances of the RiemannSum class and tries it out.
- fountain.py - an object-oriented physics example using lots of uniformly accelerated balls to make a fountain. It requires this universe.py module which controls creation and collision detection among the balls.
Week 8
- springsim.py and spring.py - Code needed for computing homework. It simulates a mass hanging from a spring.
- timewasters.py and tvtest.py - Solution to a the Television lab exercise and a "test first" test program to exercise the class.
- vectorbasic.py and testbasic.py - A 2D vector class along with a test program to exercise it.
- vector.py and testvector.py - A 2D vector that overrides Python's builtin numeric operators so it cann be used in ordinary expressions like 'a+b/2' where 'a' and 'b' are vectors.
- makepage.py and html.py - A program for dynamically generating web pages and the module it uses which defines a 'Page' class and a 'Heading' class.
- testsolvers.py, euler.py, and rungekutta.py - A program to compare two classes for solving systems of two differential equations—one using Euler's method and another using the 4th order Runge-Kutta method.
Week 9
- part.py and usepart.py - Another simple example of a class I was using to show that classes have state while functions do not.
- state.py - This is perhaps a better example of "statefull" versus "stateless".
- bounce.py - A collision handling example simulating billiard balls.
Week 10
- newton.py, newton2.py and newtonoo.py - Three versions of the NEWTON program from Calculus in Context. The final version uses an object-oriented programming style to create a general abstraction of the Newton-Raphson algorithm independent of any particular function we want to find the roots of.
- apollo.py and associated modules earthmoon.py, orbitformulas.py,masssystem.py - code for the Apollo 18 mission to the moon lab.
- marsmission.py and associated modules orbitformulas.py,solarsystem.py, masssystem.py - code for simulating a mission to Mars. It starts with a rocket in a hohmann transfer orbit to Mars, but starting opposite the earth so it won't be affected by Earth's gravity as it leaves. To do things properly we need to start it out with the earth and compute the proper Δv taking into account Earth's gravity and also either position mars properly or wait until the appropriate time to fire our rockets so that Mars will be there when we reach its orbit.