Week 3: Iterative Simulation

From "One-Shot" Formulas to Step-by-Step Reality

"Calculators use formulas. Simulations use logic."

Today's Objective

Making the Computer "Think" Step-by-Step

  1. The Accumulator Pattern: Learning that x = x + 1 is an update, not an equation.
  2. The "For" Loop: Automating repetitive physical calculations.
  3. Delta Time (): Understanding the fundamental "tick" of a simulation.
  4. Complex Modeling: Simulating air resistance (where basic algebra fails).

The Physics of the "Update"

Equation vs. Instruction

  • In Math: is impossible (it has no solution).
  • In Python: x = x + 5 is an instruction.
  • "Take the value in box x, add 5 to it, and put the result back in x.
  • " Teacher Tip: This is how we simulate motion. We update position based on velocity every "tick" of the clock.

The Simulation Engine: The "For" Loop

Automating the Ticker Tape

 height = 100 # Start at 100m 
 velocity = -10 # Falling at 10m/s 
 for second in range(5): 
	 height = height + velocity 
	 print("Time:", second, "s | Height:", height, "m")

  • range(5) tells the computer to repeat 5 times.

  • Each time it loops, it "updates" the height box.

The Physics of (Delta Time)

The Secret to Computational Physics

  • Textbook: (The "Whole Jump")
  • Simulation: (The "Small Step")

Why? Because in the real world, forces (like air resistance) change every millisecond. A single formula can't handle that. A Loop can.

Meet the "For Loop"

The Engine of the Simulation

# Calculate height every second for 5 seconds
height = 100
velocity = -10 # falling at 10 m/s

for second in range(5):
    height = height + velocity
    print("At second", second, "the height is", height)
    

Key Insight: The = sign means "Update the box with a new value."

Lab Task 1: The "Falling" Loop 🛠️

  1. Copy the code above into a Colab cell.

  2. Run it: Observe the height dropping by 10m each time.

  3. Refine the "Tick": Change the code to use a small time step ():

dt = 0.1
for step in range(10):
    height = height + (velocity * dt)
  1. Observe: Does the final height match your prediction?

The Concept: The "Step-by-Step" Reality

  • The Physics Link: In the real world, gravity doesn't just "calculate" where you are after 10 seconds. It pulls you every millisecond.

  • Code Literacy: Introducing the Loop.

    • for second in range(1, 6):

    • Explain that the code inside the loop repeats for every "step" in the range.

  • Visualizing the Step: Introduce (Delta Time). If , we calculate the new position 10 times every second.

Deconstructing the "Simulation Loop"

# INITIAL CONDITIONS
y = 100    # Starting height (meters)
v = 0      # Starting velocity (m/s)
g = -9.8   # Gravity (m/s^2)
dt = 0.1   # Time step (0.1 seconds)

# THE SIMULATION (The Loop)
for step in range(10):
    v = v + (g * dt)   # Update velocity
    y = y + (v * dt)   # Update position
    print("Height:", round(y, 2))

Deconstruction Exercise:

  • The "Accumulator": Explain why v = v + ... isn't an algebraic equality, but an "Update Command."

  • The Ordering: "What happens if we update position before we update velocity?"

LLM Interaction: Adding a Condition

Use an LLM to make the simulation "smarter" by using a while loop (simulating until a condition is met).

The Guided Prompt:

"I have a Python simulation for a falling object. Currently, it runs for 10 steps. Change this to a while loop so the simulation continues as long as the height (y) is greater than zero. Print a message saying 'The object hit the ground!' at the end."

Lab Task: The "Air Resistance" Architect

This is where students see the power of coding over calculators.

  • The Challenge: Ask the LLM to add a simple air resistance formula: drag = -0.1 * v.

  • Modification: Students must find the line where v is updated and change it to include the drag: v = v + (g + drag) * dt.

  • Verification: Does the object fall slower? Does it reach a "terminal velocity"?

Prompting for "Terminal Velocity" 🤖

Prompt Template:

"Write a Python simulation of a falling coffee filter.

  1. Use a while loop that runs until height > 0.

  2. Include a drag variable that reduces the acceleration as the object speeds up.

  3. Print the final time it took to hit the ground."

Your Job: Look at the code. Can you find the line where the drag is calculated?

The Architect's Challenge: Air Resistance

Can you make it fall slower?

  • Find the drag_coefficient in the code.

  • Double it.

  • Observe: Does the "terminal velocity" (the constant speed at the end) change?

This is the 'Aha!' moment for students: they are controlling the laws of physics.

Homework: The "Bouncing" Ball

Task: Create a simulation where a ball falls. If the height becomes less than zero, change the velocity direction so it "bounces" back up!

Hint: Use an if statement inside your while loop.

"In Weeks 1 and 2, we used Python to solve equations we already knew, like $d = vt$. But today, we unlock the real power of coding. We are going to stop using 'Final State' formulas and start simulating the world one tiny 'tick' at a time. This is how NASA simulates rocket launches and how game engines like Unity calculate physics."

"This is the biggest hurdle for new coders. In your math class, $x = x + 5$ is a headache. In Python, it's a command. Imagine you are standing at position 10. If I tell you `position = position + 2`, you simply take two steps forward. You are now at 12. This 'Update Pattern' is how we simulate motion without needing complex calculus."

"Instead of you typing out every step, we use the `for` loop. The `range(5)` is our clock. Every time the clock ticks, the computer looks at the current velocity and updates the height. As teachers, we can use this to show students exactly how a falling object 'loses' height every second."

"I want you to run the simple loop in your notebook. But then, I want you to look at the 'Delta Time' or `dt`. In physics, we know that motion is continuous. By making `dt` smaller—say, 0.01 instead of 1.0—our simulation becomes much more accurate. Try it and see if the 'jumpiness' of the output changes."

"Standard high school physics usually ignores air resistance because the math is too hard for 10th graders. But with a loop, it’s easy. We just tell the computer: 'Every step, calculate the drag and subtract it from the gravity.' Use the prompt on the screen to have the AI build this 'Complex' model for you. You'll see that the code structure is almost identical to the simple version."

Now, verify the AI. If you increase the drag, does the object slow down? This is where you become a 'Virtual Scientist.' You are testing the parameters of a digital universe. Look for the 'Terminal Velocity'—that moment where the speed stops changing. This is a visual concept that is much easier to see in a simulation than in a textbook diagram."

"Why do we do this? Because it moves students away from 'plugging numbers into a formula' and toward 'understanding the mechanism.' If a student understands that velocity changes position every millisecond, they understand the core of kinematics. You are giving them a 'Numerical Microscope'.

"Your homework is to make a ball bounce. This requires an `if` statement. You're telling the computer: 'Keep falling, BUT if you hit the floor, flip your velocity.' It’s a great exercise in logical 'branching' within a physics context."