* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Download Physics 120 Lab 3: Modeling Motion of Cart on Track: no force
Survey
Document related concepts
Photon polarization wikipedia , lookup
Classical mechanics wikipedia , lookup
Angular momentum operator wikipedia , lookup
Laplace–Runge–Lenz vector wikipedia , lookup
Relativistic mechanics wikipedia , lookup
Work (physics) wikipedia , lookup
Equations of motion wikipedia , lookup
Centripetal force wikipedia , lookup
Relativistic angular momentum wikipedia , lookup
Transcript
1 Union College Spring 2014 Physics 120 Lab 3: 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. r f = 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 ! ! ! r f = 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 ! ! ! r f = 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: ! rf = ! ri + ! ( 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 f − pi = Fnet Δt ! ! where p is the cart’s momentum, and Fnet is the net force on the cart. We can rewrite this as: ! ! ! p f = pi + Fnet Δt 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: ! pf cart.p ! pi + = cart.p + = Final momentum ! Fnet Δt 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 plots, 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 in the space below. 8 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? Explain why or why not below. 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? If there is time available in lab, continue onto part 5. 9 5. Modeling a Position-Dependent Force We now modify our force to be non-constant. We use a spring force, which is given in your book as ! Fspring = −k s sLˆ , where ks is the “spring constant” (force per unit distance), s stretched from its natural length, and end to the end with the mass. ! =| L | − L0 is the amount that the spring is L̂ is the unit vector in the direction along the spring from the fixed In our program, then, we must change the force from that due to the fan from to that of a spring. (Imagine we physically remove the fan and attach a spring.) We first need to define the spring constant in the first part of the program. Somewhere near the top of the program, perhaps right after defining the cart, but necessarily before the “while” loop, add the following. ks=10 #spring constant Note that we have added a comment to remind ourselves later what this parameter is. Then, in the “while” loop, we must change the expression for the force. We know that a mass on a spring will undergo oscillation and so we expect the cart to move between the left and right ends of the track. Therefore, we pick the center of the track to be the position of the mass when the spring is at its natural length. Conveniently, the center of the track is the origin, and so the amount of stretch of the spring equals the x-position of the cart. So, change the equation for Fnet to be as follows: Fnet=-ks*(cart.x)*vector(1,0,0) #spring force Note, now, why we chose to put the line defining the force inside the “while” loop. As the cart’s position changes, the force changes, and so we need the code to recalculate the force every cycle of the “while” loop. Change the cart’s initial velocity to be zero, so that the cart starts at rest but with the spring compressed. And, set the cart’s mass to 0.75 kg. Run the program. You should see the cart go to the end of the track, and stop. This is not the motion we expect for a cart on a spring. We know that it should undergo periodic motion (i.e. oscillation). To find why this is, read through the code and try to think like the computer. What command in the code tells the program to stop the motion? This is the same as asking, what causes the program to exit the “while” loop. And the only statement that tells the code when to exit the loop is the condition clause in the “while” statement itself. We conclude, then, that the cart has stopped at the end because of the condition “while cart.x<end.” The cart has reached the end, and so that statement is no longer true-->ergo exit the loop. Now, does this make sense? The mass on the spring will oscillate and so it will go as far to the right of the ‘natural length’ of the spring as it starts to the left. So, to prevent the cart from reaching the end of the cart we simply need to start the cart a bit away from the left end of the track. So, find the line where you initially define the cart, and change its position to be (start+0.01,0,0). Run the program. Now you should see a nice oscillation. 1 0 The video, however, doesn’t show a spring. We have put in the effect of the spring only through the equation for its force. To make the video more representative of the physics, we will now add the image of a spring. At the beginning of the program, just after defining the spring constant, add the following lines. sprL=(start-0.05)-0.1 #sets position of left end of spring spring=helix(pos=(sprL,0,0),axis=((cart.x-0.05)-sprL,0,0),radius=0.02,color=color.yellow) With a helix object, “pos” defines the position of one end, not the center, as with the box object. The ‘axis’ (as with the arrow object) is the vector from one end to the other. So, since we want the other end to connect to the left edge of the cart, we set the axis to be the relative position of the left edge of the cart, which is located at <cart.x-0.05,0,0> relative to the left end of the spring, i.e <sprL,0,0>. This spring is not really attached to the cart, but we make it seem attached by setting its axis so that the position of its end is equal the position of the left edge of the cart. Run the program. You should see the oscillating cart and the spring, but the spring is not stretching and compressing as the cart moves. This is much like what happened in lab 1 with your arrow from the Earth to Mercury. So, you need to make the same kind of fix. In the “while” loop add a line that ensures that the code re-calculates the spring axis every time the cart’s position changes. Run the program. Now, just for demonstration, change deltat to equal 0.2 and run the program. How does it work now? If your program seems to run correctly now, be sure you have a sufficient amount of commenting in your code, save it as yournamespringcart.py, and e-mail it to your instructor. Add answers to the following questions to your report. 4. Discuss, briefly, the importance of setting deltat small enough. Is there a deltat that would be too small? 5. Why, do you think, we change the time for each cycle of the “while” loop, rather than position of the cart? Why don’t we just change the cart’s position and recalculate everything? (Question #5 in WebAssign Homework 5 almost implies that we do this. But, you may note that it does actually say to use a time step, not a distance step.) If the answer to this question is not clear to you, try making this change in your program and see what happens. In a future lab, you will analyze the graphs of this oscillatory motion, so keep this program handy.