Unit 6: Simulations · Exploring simulations

Article

Simulations in physics and space

8 min read

A space agency wants to land a robot on Mars. A real try costs a huge amount of money, and one mistake loses the robot for ever. So the engineers land it thousands of times first, inside a computer. This article shows how a program can imitate things that fall, orbit and blow around.

A falling ball, one step at a time

Many science simulations use one trick: move time forward in small jumps called time steps. After each jump, the program works out the new situation from the old one.

Here is a ball dropped from a height of 100 metres. We keep two rules and leave the rest out:

  • Gravity makes the speed go up by 10 every second. (Real gravity is 9.8, so we round it: an assumption.)
  • The ball moves down by its speed.

We leave out the air, which slows a real ball. The loop below runs one time step of 1 second on each pass.

One time step per pass

truefalseheight ← 100, speed← 0, time ← 0height ≤ 0?speed ← speed + 10height ← height -speedtime ← time + 1DISPLAY(time)Stop
The program repeats the same three updates until the ball reaches the ground.
AP pseudocode
height ← 100
speed ← 0
time ← 0
REPEAT UNTIL(height ≤ 0)
{
    speed ← speed + 10
    height ← height - speed
    time ← time + 1
}
DISPLAY(time)

This is AP pseudocode. Trace it: each row is the state after one pass.

After passtimespeedheight
(start)00100
111090
222070
333040
44400

At the end of pass 4, height ≤ 0 is true, so the loop stops and the program displays 4. Now run the same simulation in Python. step is the length of one time step.

Runs in your browser

Check your understanding

A ball simulation starts with height 60 and speed 0. On each pass it does speed ← speed + 10, then height ← height - speed. What is height after the second pass?

Smaller steps, better answer, more work

A physics formula says that this ball lands after about 4.47 seconds, but our simulation said 4. Why? In the program the speed stays the same for a whole step, but in real life it grows all the time. A smaller step fixes more of this. Change step to 0.1, then to 0.01, and run it again.

Time stepTime displayedPasses of the loop
1 second44
0.1 second4.545
0.01 second4.47447

A smaller step gives a more accurate answer, but the computer must do more work. The programmer has to choose. This is the trade-off in every simulation: accuracy against speed.

Orbits and weather

An orbit simulation is the ball again, in two directions. On each time step the program (1) calculates the pull of gravity on the planet, (2) changes its speed and (3) moves the planet a little. Run for thousands of steps of one day each, it predicts where Earth, or a satellite, will be next year. Nobody can move a planet to test it, so a simulation is the only way.

A weather simulation cuts the air into millions of boxes, like a huge grid. Each box holds a temperature, a pressure and a wind. On every time step each box is updated from the boxes around it.

ChoiceBetter accuracyFaster result
Smaller boxesyesno, many more boxes
Shorter time stepsyesno, many more steps
Bigger boxes and stepsno, small storms are missedyes

A perfect forecast for tomorrow is useless if the computer needs three days to produce it. So forecasters pick boxes and steps that give a good enough answer in time. Small errors at the start grow as the forecast looks further ahead, which is why a forecast for next week is less reliable than one for tomorrow. Forecasters often run the simulation many times with slightly different starting values. If all the runs agree, they trust the forecast more.

Check your understanding

A weather team cuts the air into smaller boxes. What is the most likely result?

Worked example: a hotter planet

A student simulates a made-up planet with a program that has rules for sunlight and heat. It predicts that the planet warms by 2 degrees in 50 years. The student runs it again but turns up the sunlight setting and keeps everything else the same. The new run predicts 3 degrees.

  • The change in the prediction came from the one setting that changed. This is why a simulation is good for testing "what if" ideas.
  • Nothing proves the planet will warm by 3 degrees. The simulation leaves out things such as clouds and oceans, so its answer is only as good as those assumptions.
  • A real experiment on a whole planet is impossible. That is why we simulate.

Common mistakes

  • "A simulation gives the exact real answer." It gives an approximation. The ball landed at 4, not 4.47, with a 1-second step.
  • "Smaller steps always make a simulation better." The answer gets better, but the run takes longer. There is always a cost.
  • Changing two settings at once. Then you cannot tell which one caused the change.
  • Forgetting what was left out. Our ball ignored the air. A real ball in strong air would land later.
  • "Weather forecasts are wrong, so simulations are useless." They have limits, but they are very useful for the near future.

On the exam

Questions ask why scientists use a simulation (dangerous, expensive, slow or impossible to try), what a simulation leaves out, and what happens to accuracy and run time when the simulation uses more detail or smaller steps. The safe answer: more detail and smaller steps usually mean a more accurate result but more time and computing power. You may also be asked to trace a short loop like the falling ball.

Key words

Time step: a small jump forward in time. A simulation recalculates everything on each step.

Approximation: an answer that is close to the real one but not exact.

Trade-off: gaining one good thing by giving up another, such as accuracy for speed.

Grid: the boxes into which a weather simulation cuts the air.

AP® is a trademark registered by the College Board, which is not affiliated with, and does not endorse, this site. All lessons and questions here are written by JrCodex.