Python in the classroom - lesson 5

We have already arrived at the fifth lesson on modeling force and motion with Python. Now that we have mastered the basics of programming and we know how to create graphs, we should start working towards modeling Force and Motion.

We advise teachers who want to teach Python within the context of something other than physics to try to find a connection within the teaching methods. In physics there are often exercises on modeling in the handbooks. Try to combine the theory and the Python model. The students also become stronger in the subject and have more in-depth knowledge if they have also made a computer model of it. It may be possible to model market forces within economics, and probability calculations within mathematics.

We will be working on a computer model for free fall. The goal is to create an (x, t) diagram and a (v, t) diagram of free fall during the first 5 seconds. We assume that the object starts to fall from a standstill.

Come up with a model

Let's consider the case of free fall. This motion is characterized by a constant acceleration of 9.81 m / s2. We ignore the air friction. We're starting from a standstill, so for simplicity let's pick the initial values ​​like this:

  • t = 0 s
  • v = 0 m / s
  • x = 0 m

At time 0 (the beginning) the velocity is 0 (m / s) and the position is 0 (m).

We then want to take a small step in time each time and then do the following calculations:

  1. Since we ignore air friction, there is a constant acceleration of 9.81 m / s2
  2. The new velocity follows from the old velocity plus the change in velocity
  3. With the new speed you can calculate the displacement
  4. The new position follows from the old position plus the displacement

Once we have calculated the new velocity and position, we want to repeat these calculations for the next time step. We take 0.01 seconds as a time step. We will take a total of 500 steps in time, so that we ultimately modeled for 5 seconds.

It is good to explain this in class, so that the students understand this theory before programming the model. In the following lessons we will expand this model with a calculation of the resulting force, where the acceleration is no longer constant, but depends on the resulting force on the object. The step-by-step plan is then expanded, but the last steps remain the same.

Initial values ​​of the model

We now define the following initial values:

#initial time is 0 seconds
t = 0

#initial velocity is 0 m/s
v = 0

#initial position is 0 m
x = 0

#delta t time step for each iteration
dt = 0.01

#total time of the simulation is 5 s
total_time = 5

#start with an empty list of times, velocities and positions
times = []
velocities = []
positions = []

We have already talked about the initial values ​​t = 0 (s), v = 0 (m / s) and x = 0 (m). We also take a small step dt of 0.01 s each time.

However, we must also include the total time of the simulation in our initial values. We can easily adjust this later to make the simulation last longer or shorter.

Finally, to make the (x, t) and (v, t) diagrams we need lists of time, position and velocity, so that we can again use the plot function of Matplotlib. We start with an empty list for each parameter and will fill it in every time step.

Small time steps in a loop

For this simulation we choose the while loop. The idea is that the simulation should stop when the time is equal to the total time of the simulation. Once we've done this, we'll move on to creating graphs. We will start by making the while loop without all the calculations in between. This way we can see if the while loop works.

while t <= total_time:
  t += dt


So we always add the time step dt to the time t. We keep repeating the code as long as the time is less than or equal to the total time of the simulation. We see from the print statements that the loop repeats each time, prints the current time (in steps of 0.01 s) and stops at 5 seconds. What you will see is that the numbers sometimes contain a lot of digits after the decimal point and are only rounded off. This is due to “floating-point representation error”. Interested students can find more information about this on the internet.

We are now going to extend the loop with the steps to calculate the velocity and position:

while t <= total_time:
  t += dt
  v += 9.81 * dt
  x += v * dt


The change in velocity is the acceleration times delta t. We add this to the old velocity. Remember that v + = 9.81 * dt is the same as v = v + 9.81 * dt. The v to the right of the equals sign is the old velocity.

The displacement is equal to the velocity times delta t. Attentive students will (hopefully) notice that the velocity over delta t was not constant. However, the smaller delta t is taken, the more accurate the result. You can also calculate the displacement with the average of the old and new velocity. Nice (extra) task for the fast and attentive student.

At the end of the loop we add the time, velocity and position to the lists, so that we have them ready to make beautiful graphs.

Create charts

We want to make this free fall into the (x, t) diagram and the (v, t) diagram. We use Matplotlib for this.

import matplotlib.pyplot as plt

plt.plot(times, velocities)
plt.ylabel('v (m/s)')
plt.xlabel('t (s)')

plt.plot(times, positions)
plt.ylabel('x (m)')
plt.xlabel('t (s)')

We first create the (v, t) diagram. For this we use the lists “times” and “velocities” that we have already filled in our code. As we've seen before, we can add labels to the x and y axis and give the chart a title.

We do the same for the (x, t) diagram. At the end we get two nice graphs of free fall:

Here you will find the Python Colab Notebook of the model.


About the author

Roeland is the HandiHow company owner and lead education developer.

Leave A Reply

Your email address will not be published.


Contact HandiHow for an informal exploratory meeting.