Marr, Winter 2014

advertisement
1
Union College
Winter 2014
Physics 120 Lab 2: Modeling Motion of Cart on Track:
no force, constant force, and position dependent force
Modeling motion with VPython
Every program that models the motion of physical objects has two main parts:
1.
2.
Before the loop: The first part of the program tells the computer to:
a. Create numerical values for constants we might need
b. Create 3D objects
c. Give them initial positions and momenta
The “while” loop: The second part of the program, the loop, contains the lines that the computer
reads to tell it how to increment the position of the objects over and over again, making them
move on screen. These lines tell the computer:
a. How to calculate the net force on the objects
b. How to calculate the new momentum of each object, using the net force and the
momentum principle
c. How to find the new positions of the objects, using the momenta
We will now introduce how to model motion with a computer. You will write program that will model the
motion of a “fan cart” on a track, which experiences a constant force of interaction due to the action of the
small electric fan mounted onto the cart.
1. Before the loop
Creating the objects
Open IDLE for Python, and type the usual beginning line:
from visual import *
First, we create the track. Let’s let the length of the track be a variable, and let’s make it one meter long.
On the next line, type the following.
length=1.0
And then, to make a visual of the track, we make a box object. So, type the next line.
track = box(pos=vector(0,-0.05, 0), size=(length, 0.05, .10))
The pos attribute of a box object is the position of the center of the box. We place it at a slightly negative
y value so that the cart (which we’ll add next) can move on top of the track, with y=0. The size attribute
gives the box’s length in the x, y and z directions. We put “length” as the x-length because the cart will run
along the x-axis.
To represent the fan cart, we will add another box object. We first make sure that the cart starts at the left
end of the track. We will give the cart a length of 0.1 meters, so we want its center to be 0.05 meters from
the end. So, make the next line as follows
start = -0.5*length+0.05
Then, we make the cart, using the following line :
cart=box(pos=vector(start,0,0),size=(0.1,0.05,0.1),color=color.green)
2
Run the program to see the track and “cart”.
Initial conditions
Any object that moves needs two vector quantities declared before the loop begins:
1. initial position; and
2. initial momentum.
We’ve already given the cart an initial position of < 0.45,0,0 > m. Now we need to give it an initial
momentum, which involves both a mass and an initial velocity:
cart.m = 0.80
We have just made a new attribute named “m” for the object named “cart”. The symbol cart.m now
stands for the value 0.80 (a scalar), which represents the mass of the cart in kilograms.
Let’s give the cart an initial velocity of < 0.5, 0, 0 > m/s (which might occur from a gentle push with your
hand):
cart.vel = vector(0.5, 0, 0)
Then, the initial momentum would be the mass times this initial velocity vector:
cart.p = cart.m*cart.vel
This now gives the cart an initial momentum value that is 0.80 kg < 0.5, 0, 0 > m/s = < 0.4, 0, 0 > kg m /s.
The symbol cart.p will stand for the momentum (a vector) of the cart throughout the program.
Note: We could have called the momentum just p, or the mass just m, instead of cart.p or cart.m. But,
by defining these quantities as attributes of the cart, we will (in the future) easily tell apart the masses and
momenta of different objects.
Time step and time
To make the cart move, as we did with Mercury in the first lab, we will make a while loop and use the

 
position update formula, i.e. rf ri vt , repeatedly, in the loop. We need to define a variable, deltat,
to stand for the time step t . Here we will use the value t = 0.01 s. So add the following line next.
deltat = 0.01
Each time through the program loop, we will increment the value of t by deltat.
We also set the variable t, which stands for cumulative time, to start at zero seconds.
t = 0
This completes the first part of the program. The program now creates numerical values for constants we
might need, create 3D objects, and gives them initial positions and momenta.
2. The “while” loop
Constant momentum motion
Now that the cart has an initial momentum, we’ll make the program predict how the cart will move at
future times.
3
We will create a “while” loop. Each time the program runs through this loop, for now, it will do two things:
1. Use the cart’s current momentum to calculate the cart’s new position
2. Increment the cumulative time t by deltat
Now, we want the cart to move until it reaches the end of the track. So, in our while loop, we use the
position of the cart relative to the end of the track as the condition for the program to check. So, add the
next line:
while t<5:
Make sure the line ends with “:”, then press Enter. Note, again, that the cursor is now indented on the next
line. If it is not, place the cursor just after the “:” and press Enter.
This tells the computer to keep executing the loop while the total accumulated time is less than 5 seconds.
In class, you learned that the new position of an object after a short time interval
t is given by
  
rf ri vt

ri is its initial position. This assumes that the velocity
doesn’t change very much during the short time interval t .

where r f is the final position of the object, and


 
Since (at non-relativistic speeds) p  mv , or v  p / m , we can write
  
rf ri (p/m
)
t
We will use this equation to increment the position of the cart in the program. First, we must translate it so
VPython can understand it. After the “while” statement, type the following.
cart.pos = cart.pos + (cart.p/cart.m)*deltat
Notice how this statement corresponds to the algebraic equation:

r
f

r
i

 (
p
/
m
)
t
cart.pos = cart.pos + (cart.p/cart.m)*deltat
Final
position
Initial
position
Velocity
Timestep
In the program, the final and initial position is written the same way: cart.pos. This isn’t really an
equality, but an assignment statement; cart.pos is being assigned a new value equal to its old value plus
(cart.p/cart.m)*deltat.
Finally we need to increment the cumulative time t. On the indented line after the “while” statement,
type the following
4
t = t + deltat
And, slow down the animation by adding a rate statement immediately after the “while” statement.
rate(100)
This ensures that the computer will only execute the loop 100 times per second.
Now run the program.
You should now see that the small green box, which represents the cart, moves to the right at a constant
speed along the top of the track. And then it keeps going and going... even after it leaves the track. This
clearly is not a physical reality. We never put anything in the code that tells the computer that this is not
possible, and so it doesn’t know any better. We need, then, to put in a physical constraint:
Adding a physical constraint to the motion:
The easiest way to do this is with the constraint in the “while” statement. Instead of using a random time
limit, we can limit the position of the cart. In short, we want to stop the while loop when the cart reaches
the end of the track.
We first define the end of the track. Add the following line anywhere before the while loop:
end = 0.5*length-0.05
Note that we set the end to be 0.05 m before the actual end. This is because of the finite size of the cart.
The center of the cart will be 0.05 meter from the end, when the edge of the cart is at the actual edge.
And, now, change the “while” statement to tell the program to do the following calculations as long as the
cart has not reached the end of the track. This constraint can be written as “cart.x<end.” So, change the
“while” statement to the following.
while cart.x<end:
Run the program again. This time you should see the small green box travel to the right at a constant
velocity, and stop just at the end of the track.
3. Force and momentum principle
Now we will modify the program to include a force on the cart. This means we will have to add lines to the
program that tell the computer that there is a force on the cart, and tell it how to use the momentum
principle to change the cart’s momentum due to the force.
Suppose you push a cart, and after leaving your hand it has an initial momentum to the right, as before. But
now, the fan on the cart is opposing this initial momentum, so that there is a net force on the cart to the left.
This force is about 0.3 Newtons.
Let’s use our program to model this situation. First let’s add a line to that creates a force vector. In the
loop, insert the following line right after the “rate(100)” statement:
Fnet = vector(-0.3, 0, 0)
Note: This force has a constant (vector) value; it doesn’t change each time through the loop, unlike the
cart’s position. We could have, therefore, written this line before the loop. But in most of the programs we
write, force will be changing with time, so force calculations should go inside the loop.
5
Next, we need to use the momentum principle. The momentum principle says the change in an object’s
momentum in a short time period is equal to the net force on it, times the time interval (this assumes that
the force doesn’t change much during the short time interval). In symbols, this is:
   

pp
p
F

t
f
i
net


where p is the cart’s momentum, and Fnet is the net force on the cart. We can rewrite this as:
  
pf pi F
t
net
We now use this equation in the program to tell the computer how to calculate a new momentum for the
cart. Insert the following line in the loop, right after the line that reads Fnet = vector(-0.3,0,0):
cart.p = cart.p + Fnet*deltat
Note the similarity between this line and the line that updates the position. Here is how the algebraic
equation corresponds to the VPython code:

p
f
cart.p

F

t
net

p
i
= cart.p
Final
momentum
+
Fnet*deltat
Initial
momentu
m
Net force
Timestep
Run the program. You should see the cart move to the right a small distance before changing direction and
then moving to the left, and speeding up. You will also see, now, that the cart will keep going and going to
the left. You can stop it by closing the graphics window.
Again, we have not written anything in the code telling the program not to let the cart leave the left end of
the track. We need to add this constraint as well and we can do this by adding to the “while” statement
constraint. Change the “while” statement so that it says the following:
while cart.x<end and cart.x>(start-0.01):
(Note: we need to include the ‘-0.01’ because the cart starts right at “start,” which is not greater than start,
and so the while condition would fail right immediately if we didn’t add the “-0.01”.)
Change the initial momentum of the cart so that it moves a greater distance to the right before changing
direction: make the initial speed of the cart 0.8 m/s to the right. Run the program. You should now see the
sphere move much closer to the right end of the track before it starts moving to the left.
4. Analyzing the Motion
Getting the computer to reproduce a video of the motion is cool, but the video alone doesn’t provide a test
of physics. We need tools to analyze the motion demonstrated in the video. Here are two popular methods.
6
Printing out the calculations.
The motion of a fan cart is a relatively simple motion and we could calculate almost any aspect of the
motion by hand. We’ll do that and compare to the calculation made by Vpython to demonstrate that with
more complex motion, which cannot be solved by hand so easily, we can rely on computer simulations.
We’ll Calculate by hand, first, what the final momentum of the cart should be when it returns to the starting
position. The force is constant, so we can use the position update and momentum update formulas for the
entire time.
The final momentum is given by the momentum update formula,



p f  pi  Fnet t .
But we need to know the total t to solve for the final momentum. We can find the total t by first using
the position update formula with a constant force, which is given by


 
r f  ri  vi t  12 ( Fnet / m)t 2 .
In our latest program, the cart stops when it returns to its initial position, so


r f  ri , and these, then, cancel
in the equation above, leaving


0  vi t  12 ( Fnet / m)t 2
The solutions for t, here are 0, which is just the first moment of the motion, and
t 

2m | vi |

.
| Fnet |
Use this equation, and the momentum update equation above, and the values of m,


| vi | , and | Fnet | to
solve the time of this motion and the final momentum. Write them in the space below.
t = ______________________________

p f = < ____________________ , 0 , 0 >
Now, let’s see what the computer calculates. After the “while” loop, start a new line, hit delete to unindent,
and add the following line:
print “Delta t =”, t, “Final momentum =”, cart.p
Run the program. Read the the printout value in the Python Shell window. According to the program,

what are the values of the t and p f ?
t = ______________________________

p f = < ____________________ , 0 , 0 >
Compare the printout value with your calculated values. Do they agree?
Note how easy the computer simulation was. Now, imagine changing some parameters and redoing the
calculations. With the computer, this is easy. Change the initial velocity to be 0.6 m/s to the right, the
mass of the cart to 0.7 kg, and the Net Force to 0.5 N to the left. What should be the final momentum now?
7
Don’t bother to do the calculation by hand, but make these changes in the code and then run the program

again. According to the program, what are the values of the t and p f ?
t = ______________________________

p f = < ____________________ , 0 , 0 >
Graphing the motion.
Another, and even more useful tool with computer simulations, is to graph the results.
To enable your VPython code to do any graphing, at the top of the program, immediately after the line that
says “from visual import * ” add the following line.
from visual.graph import *
And anywhere before the “while” loop, add the following two lines:
xcurve = gcurve(color=color.cyan)
pcurve = gcurve(color=color.red)
These define xcurve and pcurve as continuous lines on a plot, of colors cyan and red, respectively. We
will, in the “while” loop, then, assign the cart’s x positions and x-momenta to these curves. To label the
axes, add the following line BEFORE the xcurve line:
gdisplay(x=100,y=500, xtitle='time (sec)', ytitle='X (cyan), Px (red)')
We put in the x=100 and y=500 to make the graph window appear at a different location than the video.
Now, before letting the program make the lots, make a prediction. What do you think the graph of the
cart’s x values vs. time will look like? What do you think it’s x-component of momentum vs. time will look
like? Draw your predictions for both curves on a separate sheet of paper.
Now, let’s see what the VPython program says. In the “while” loop, after the cart’s position has been
recalculated and before the time gets modified for the next loop, insert the following two lines (make sure
they are indented, like all calculations in the “while” loop must be):
xcurve.plot(pos=(t,cart.x))
pcurve.plot(pos=(t,cart.p.x))
The first line will add a point at coordinates (t, cart.x) to the cyan curve, and the second line will add to the
red curve. When you run the program, as it goes through the “while” loop, it will add the x and p.x values
to the plots as they are calculated.
Run the program. Examine the plot window. How do these curves look? Were your predictions right?
For your report, add comment lines to your program, save the program as yourname_fancart.py, e-mail
the program to your instructor, and turn in answers to the following questions either by email in a separate
document, or on a separate sheet of paper. .
1. Explain how VPython enables you to simulate the motion of a body acting under the influence of a
force—how do the update formulae get used? What structure of the code causes the position of the cart to
change, and, hence, move, many times?
2. Briefly discuss the need for considering physical constraints when running the code. Use the example
of the cart running off the end of the track, without falling, to explain how the programmer needs to
account for ALL relevant physical constraints to make sure the simulation is realistic.
3. Discuss your predicted graph of momentum vs. time and what VPython showed and discuss how the
computer code can be used to test physics models by comparing with predictions?
Download