Lessons and the learning game

Python for science and math

Space science in plain Python on a Pi: units, Euler steps, Monte Carlo, error bars and least squares, then a photogate pendulum that measures g.

The course

Python for science and math is an intermediate Coding course in seven lessons, for anyone who has finished Python from zero. There is no numpy on the Pi, so every method is written out by hand, and every number a step quotes is the one the Pi prints. Steps are checked against the Serial Monitor; the last two lessons time real pulses from a photogate on GPIO17.

  1. Units: an astronaut's weight on Mars (259.7 N), the pound-force seconds that lost the Mars Climate Orbiter, the 3 to 22 minute radio delay to Mars, scientific notation, and why 0.1 + 0.2 == 0.3 is False.
  2. Numeric methods: Newton's method for square roots and a stopping rule, bisection on Newton's own cubic, Kepler's equation for where Mars is on its orbit, and the trapezoid rule with its error shrinking a hundred times for ten times the strips.
  3. A falling ball: Euler steps for a 100 m drop on Earth and on Mars, a smaller step, air drag no formula solves neatly, then the ISS's orbit, which drifts 96 km in one lap until velocity and position swap places (Euler-Cromer).
  4. Monte Carlo: pi from random darts, the 1 / sqrt(n) error law, a Mars lander's chance of hitting a boulder (2.4 percent), and seeds that make a random run repeatable.
  5. Statistics of a sensor: mean, sample standard deviation by hand, standard error, a check against statistics, and a photogate that times the same swing far better than a thumb on a stopwatch.
  6. Fitting a line: T^2 against L to straighten a pendulum's curve, least squares by hand, g from the slope, residuals and R^2, and a prediction of the seconds pendulum.
  7. Project: measure g with a pendulum: the finale.

The project

A 0.994 m pendulum swings through a photogate (a KY-010 slotted switch) on GPIO17, and the Pi stamps every beam cut with time.monotonic(). Four gaps make two periods, and g = 4 pi^2 L / T^2 gives 9.81 m/s^2. Set the gate to the speed a Mars pendulum cuts it at and the same code reads 3.73. An error bar from the tape measure and the timing shows the length is now the weak link, the program names the world the pendulum is swinging on, and a five line lab report closes it out.

On the canvas the photogate's one-slot wheel stands in for the bob: each turn cuts the beam once, so 60 RPM is a cut a second, exactly what the bob does passing the bottom of each swing. The slider moves in whole RPM, so the Mars run (37 RPM) lands half a percent high, and the lesson says why.

What the Pi engine does differently

  • It is MicroPython 1.29, not CPython. Its random is its own, so a seed gives different darts than desktop Python, and the Monte Carlo numbers in the lessons are the Pi's.
  • round() and float formatting can differ in the last digit from CPython: round(2.675, 2) is 2.68 here. The lessons print with format specs and compare floats with a tolerance.
  • f-strings take widths, precision and e notation, but not the , thousands separator.
  • A dict does not keep the order its keys went in, so the planet search in the project compares every entry rather than relying on order.
  • There is no numpy, matplotlib or pandas. math and statistics are here, and everything else is a loop you can read.
  • Time is simulated: time.monotonic() reads the circuit's clock, so the photogate's gaps are exact to the microsecond.

There is no Pi photogate circuit in the gallery, so the course draws one: the KY-010 photointerrupter on GPIO17, powered from the Pi's 3.3 V.