A Byte of PhysicsLogo

From equations to code

Physics equations are not magic syntax. Treat each one as a small program contract: name its inputs, state its assumptions, compute an output, then test a behaviour that should stay true.

Think like a programmer

An equation is a contract, not an assignment statement. Its inputs have units, its output has a meaning, and its assumptions are preconditions that your code should make visible.

Model checklist

Inputs
Initial position x0 in metres, velocity v in metres per second, and time t in seconds.
State
For this closed-form model, the initial position and constant velocity are enough state.
Rule
Add velocity multiplied by elapsed time to the initial position.
Output
Position x in metres.
Check
At zero time, the result must equal the initial position.

The translation

For constant velocity, position changes with time:

\[x(t) = x_0 + vt\]

In code, x0, v, and t are typed inputs. x is the output. The model assumes velocity does not change during the interval. That last sentence is as important as the equation: code can run a bad model perfectly.

function positionAt(x0: number, velocity: number, time: number) {
  return x0 + velocity * time;
}

A programmer's checklist

For this model, positionAt(x0, 0, t) === x0 is a useful invariant. A unit test can protect it. A simulation cannot replace that test; it makes the behaviour easier to see.

Try this experiment

Prediction: At fixed velocity, doubling elapsed time doubles the displacement from the initial position.

Start with x0 = 2 m and v = 3 m/s. Predict the position after 4 s before calculating it, then repeat at 8 s. Which value is state, and which values are inputs to this pure function?

Where this model breaks

Constant velocity excludes acceleration, changing direction, drag, and discontinuous events such as collisions. If velocity changes during the interval, use an update rule that includes acceleration instead of silently reusing this formula.

Summary

Translate a relationship into named inputs, a result, and a test. The equation stays the source of truth; the function is its executable form.

Glossary

Self-check

  1. What unit should positionAt return?
  2. What result must it return at zero time?
  3. Which physical change makes constant velocity invalid?

Sources

Model contract

Treat the lesson as a small function before treating it as a fact to memorize. Give every value a unit, keep only the state needed for the next step, and make the output easy to inspect.

\[\text{observable output} = f(\text{inputs},\,\text{state})\]
Inputs
Quantities you set or measure, with units and useful bounds.
State
Values the program must retain to reproduce the next result.
Rule
The relationship or update that turns inputs and state into a result.
Check
A known limit, unit check, invariant, or measured result that can expose a bad model.

Implement the idea as a model

For From Equations to Code, write down the quantities you can control, the values your program must retain, and the result a reader could inspect. In Physics as Computation, the useful program is not the drawing: it is the smallest explicit model that makes a prediction you can test.

Guided experiment

Prediction: changing one declared input while holding the others fixed should change only the outputs that the model connects to that input. Choose one input, predict the direction of change, then check a limiting case such as zero, a symmetric arrangement, or a familiar low-speed or small-change approximation.

Where this model breaks

This lesson is a teaching model, not a complete simulator. Before using it outside the stated question, check which interactions, scales, uncertainties, boundary conditions, and measurement limits it leaves out.

Summary

Treat From Equations to Code as a contract: named inputs and units enter a rule, the rule produces an observable result, and a known limit or invariant checks whether the implementation deserves trust.

Glossary

  • Input: a measured value or chosen parameter supplied to a model.
  • State: the smallest set of values needed to continue or reproduce a model.
  • Validation: comparing an output with a known result, limit, invariant, or measurement.

Self-check

  1. Which values are inputs, and which values must remain state?
  2. What observable result would tell you the model is behaving as expected?
  3. Which assumption would you test first before applying the model to a real system?

Model review: turn From Equations to Code into a test

Translate a physical relationship into inputs, state, an update rule, and tests.

  1. Name the inputs and units that the physics as computation model needs.
  2. Separate the state you must keep from values you can calculate when needed.
  3. Write one rule that maps the current state and inputs to an observable result.
  4. Choose a limiting case, unit check, invariant, or known result before trusting an output.
  5. State one assumption you would change before using this simplified model for a real decision.

Share to: