Python Tutorial 7: Modeling The Momentum Principle
Last updated
Last updated
Students finally get to see how a force can be modeled in Tychos and how the change in an object's state can be calculated using the momentum principle. The objectives of this tutorial are:
Learn how to define a mass attribute for an object.
Learn how to calculate a change in momentum due to an applied force.
Learn how to update an object's velocity based on a change in momentum caused by the force.
The momentum principle is a very powerful principle that helps us understand the cause and effect relationship between forces acting on an object, and how that object's state of motion is changing. The momentum principle can be stated like this:
The idea communicated in this simple formula is that the sum of all forces acting on an object for an interval of time will cause the object to experience a change in velocity. It also states that the larger the mass of the object, then the smaller the change in velocity over that interval of time.
Forces are vectors, just like velocity, and therefore they exist in more than one spacial dimension. They have direction as well as magnitude. We are going to need to modify the above formula to represent these values as vectors:
Although this might seem like a small change, it is a very important one.
In this tutorial we will investigate how to simulate a force as a 2D vector that acts on a particle, and then we will consider how a simulated particle behaves when acted upon by more than one force.
To follow along in this tutorial, open the scenario in a new tab by clicking on the link below:
Tutorial 7: Simulating The Momentum Principle - Python
The first thing that you are going to need to do is create a particle that we can apply our force on. For review, see if you can remember how to do this by achieving this goal:
Create a circle
called "ball" with an initial position of (0, 0) and a radius of 5."
Once you have done this, your goal will turn green.
The next step in the Initial State pane is to give the ball a velocity that is [0, 0] like this:
To define a force, all we need to do is to define the magnitude of the force in each dimension using a matrix. We can simply define a matrix variable in the Initial State pane, just as we would any matrix variable:
In order for the object to respond to the force defined by the momentum principle, the object must have a mass. Just as we defined a velocity property for an object, we can define a mass property.
Remember, Tychos has no idea what mass is, it will be up to us to tell Tychos how to update the position of the object based on some mathematical calculation that will be dependent on this new property that we are defining here.
Add this line of code to the Initial State pane:
This new property will be used in the Calculations pane.
We are ready to do some calculations so that we can define the behavior of the object based on the applied force we defined above. Click on the Calculations pane because that is where we need to write the rest of our code.
Based on the momentum principle, a force applied for an interval of time will affect the momentum of an object (sometimes referred to as a particle). We can therefore define a change in momentum in code like this:
The interval time is of course the amount of time that passes each step - dt
. The small change in momentum, defined by dp
is simply the force vector multiplied by dt
, the time interval. Once again, we use dt
here because it scales the simulation to real time no matter what the step rate of our virtual universe is set to.
We can calculate the change in velocity that the object will experience based on its mass property. We divide the change in momentum by the object's mass, like this:
The variable dv
represents the change in velocity that will occur in one step. We need to then add this value to the current velocity value so that it is updated. Again, remember that Tychos will do this iteratively, updating the velocity each step as long as the calculation is indeed in the Calculation pane:
Once again, it is important to remember that this line of code should be read like this:
"The new value of the ball's velocity is assigned to the old ball's velocity plus a small change in the velocity that occurred in this step."
Now that we have the new velocity of the ball
we can use that value to calculate the new position that ball
should move to in the animation. We just use the same iterative calculation process that we have used before:
Once again, this is assigning a new value for the position by adding a small amount of position change to the old position.
When you play the simulation, you should see the circle
accelerate as its momentum is changing because of the force interaction we modeled. Stop the animation, then change the mass
of the ball
and then replay the simulation to see how the ball
changes its behavior. Is it acting as you would expect?
We can create a motion graph to see if the virtual object's motion is similar to a real object's motion based on your observations of real objects. Go ahead and create four graphs in the Initial State pane, two for analyzing the X and Y positions, and then two for analyzing the X and Y velocities:
Once you have created the graphs, go to the Calculations pane and plot the points:
Once you plot some points, you should see that the velocity graphs are linear while at least one of the positions graphs is curved...Does this match your expectations?
Before moving onto the next section, you should try to accomplish the following goals because they will ask you to create a different Particle that is acted upon by a force that has both an X and a Y component:
Create a new Particle called "ball2" that begins at [0, 30], has a radius of 5, a mass of 5.
Define a force called "force2" whose value is [5, 5].
Apply the force on "ball2" using the Momentum Principle to change its velocity.
In order to do the above, you are going to need to define a new dp2
and dv2
representing the new change in momentum and change in velocity for ball2
.
How we can model the behavior of an object when more than one force acts on it? As we stated earlier in this tutorial, forces are vectors. This means that when forces are added together, we have to consider that they are now vector quantities and not just scalars:
That means that everything that we have learned about vectors applies here when we are calculating the net force on an object. The magnitude AND the direction of each force matters when we add the vectors together.
We are going to apply a new force to our original circle
called ball
. This new force will be called force3
. Add this line of code to the Initial State pane:
This is all that needs to be done in the Initial State pane. Click on the Calculations pane because that is where we need to modify some of the code that we wrote earlier. In the previous section of this tutorial, we defined the change in momentum for the ball
object using this line of code:
The problem with this is that we now have more than one force acting on the circle
. The momentum will be affected by the NET force, the sum total of all forces acting on it. We can simply modify this line of code so that it now looks like this:
When you start your simulation, you should see the ball3
circle accelerate along a diagonal path because two forces are acting on it now, not just one.
You can also define a new variable that is simply the net forces acting on ball3.
We do that below by defining a new variable called Fnet3
that represents all the forces combined:
Obviously this is an optional step, but it can help when reading the code to define variables that represent quantities that we define in Physics.
The following goals will test if you can model multiple forces acting on a Particle:
Create a new Particle called "ball3" that begins at [100, 0], has a radius of 5, a mass of 10 and any color you like.
Apply all three forces ("force1", "force2" and "force3") on "ball3" using the Momentum Principle to change its velocity.
Once again, in order to do the above, you are going to need to define a new dp3
and dv3
representing the new change in momentum and change in velocity for particle 3.
As a last step, let's analyze the motion of ball3
just a little more closely. Change these lines of code in the Calculations Pane from:
To:
It should reveal something interesting. The object is accelerating in each of the dimensions, and so there really is a kind of independence of motion as it exists in each dimension without affecting the other. If you were to change one of the X components of one of the forces, it would only change the behavior of the particle's motion in that dimension. This is a powerful idea that we will return to in a following tutorial on projectile motion.
This was a brief but important tutorial on how to simulate forces. Hopefully you can see that with a few lines of code that model the Momentum Principle calculations, we can simulate objects accelerating in our 2D universe. Here are the steps required to simulate a force on a Particle
Define a force variable as a vector.
Define a mass attribute for an object. This is done by simply using the "dot" notation and giving the attribute a value.
Define the change in momentum using the momentum principle and the fact that the duration of time is defined by dt:
dp = force * dt
Update the velocity based on the change in velocity caused by the change in momentum:
dv = dp / object.mass
object.v = object.v + dv
Update the position using the updated velocity.
object.pos = object.pos + object.v * dt