1 Modelng moton wth VPython Eery program that models the moton o physcal objects has two man parts: 1. Beore the loop: The rst part o the program tells the computer to: a. Create numercal alues or constants we mght need b. Create 3D objects c. Ge them ntal postons and momenta 2. The whle loop: The second part o the program, the loop, contans the statements that the computer reads to tell t how to ncrement the poston o the objects oer and oer agan, makng them moe on screen. These statements tell the computer: a. How to calculate the net orce on the objects b. How to calculate the new momentum o each object, usng the net orce and the momentum prncple c. How to nd the new postons o the objects, usng the momenta We wll now ntroduce how to model moton wth a computer. The program you wll wrte wll model the moton o a an cart on a track. A small electrc an mounted on a cart pushes on the ar wth a constant orce, so the ar exerts a constant orce on the an blades. 1. Beore the loop Creatng the objects Open IDLE or Python, and type the usual begnnng statement: rom sual mport * Frst we ll create a box object to represent the track that s one meter long. On the next lne type the ollowng: track = box(pos=ector(0,-.075, 0), sze=(1.0, 0.05,.10)) The pos attrbute o a box object s the poston o the center o the box. The sze attrbute ges the box s length n the x, y and z drectons. To represent the an cart, we wll use a sphere object. (You could use a box you preer, but spheres are somewhat easer to deal wth.) We wll place the cart at the let sde o the track to start. On the next lne type the ollowng: cart = sphere(pos=ector(-0.5,0,0), radus=0.05, color=color.green) Run the program to see the track and cart. Intal condtons Any object that moes needs two ector quanttes declared beore the loop begns: 1. ntal poston; and 2. ntal momentum. We e already gen the cart an ntal poston o 0.5, 0, 0 m. Now we need to ge t an ntal momentum. I you push the cart wth your hand, the ntal momentum s the momentum o the cart just ater t leaes your hand. Snce the denton o momentum, at speeds much less than the speed o lght, s p m, we need to tell the computer the cart s mass and the cart s ntal elocty.
2 On a new lne type the ollowng: cart.m = 0.80 We hae made up a new attrbute named m or the object named cart. The symbol cart.m now stands or the alue 0.80 (a scalar), whch represents the mass o the cart n klograms. Now that we hae the mass, multplyng the mass by the ntal elocty wll ge the ntal momentum. Let s ge the cart an ntal elocty o 0.5, 0, 0 m/s. Then, the ntal momentum would be the mass tmes ths ntal elocty ector. On a new lne type the ollowng: cart.p = cart.m*ector(0.5, 0, 0) Ths now ges the cart an ntal momentum alue that s ( 0.80 kg) 0.5, 0, 0 m/s = 0.4, 0, 0 kg m/s. The symbol cart.p wll stand or the momentum o the cart (a ector) throughout the program. Note: We could hae called the momentum just p, or the mass just m, nstead o cart.p or cart.m. There are no bult n physcs attrbutes p or m or objects lke there are bult-n geometrcal attrbutes pos or radus. We can make any attrbutes we want. It s useul to create attrbutes lke mass or momentum or objects; ths way, we can easly tell apart the masses and momenta o derent objects. Tme step and tme To make the cart moe we wll use the equaton deltat to stand or the tme step t On a new lne type the ollowng: deltat = 0.01 r = r + repeatedly n a loop. We need to dene a arable. Here we wll use the alue t = 0.01 s. Also set a arable t, whch stands or cumulate tme, to start at zero seconds. Each tme through the program loop we wll wrte, we wll ncrement the alue o t by deltat (n ths case, 0.01 seconds). On a new lne type the ollowng: t = 0 In your notebook, wrte the answers to the ollowng questons. Your nstructor wll ask you about them. (1) Calculate how ar the cart wll moe n one tme step (one executon o the loop). (2) How many executons o the loop wll t take or the cart to moe one meter? (3) What wll the tme t be ater mong one meter? CHECKPOINT M1: Ask an nstructor to check your work or credt. You can read ahead whle you re watng to be checked o. Ths completes the rst part o the program, whch tells the computer to: a. Create numercal alues or constants we mght need b. Create 3D objects c. Ge them ntal postons and momenta
3 The whle loop Constant momentum moton Somebody or somethng gae the cart some ntal momentum. We re not concerned here wth how t got that ntal momentum. We ll predct how the cart wll moe n the uture, ater t acqured ts ntal momentum. We wll create a whle loop. Each tme the program runs through ths loop, t wll do two thngs: 1. Use the cart s current momentum to calculate the cart s new poston 2. Increment the cumulate tme t by deltat On a new lne type the ollowng: whle t<3.0: Make sure the statement ends wth :, then press Enter. Note that the cursor s now ndented on the next lne. I t s not, place the cursor just ater the : and press Enter. Ths tells the computer to keep executng the loop whle the cumulate tme s less than 3 seconds. In class, you learned that the new poston o an object ater a short tme nteral r = r + t s gen by where r s the nal poston o the object, and r s ts ntal poston. Ths assumes that the elocty doesn t change ery much durng the short tme nteral t. Snce at low speed p m, or p / m, we can wrte r = r + ( p / m) We wll use ths equaton to ncrement the poston o the cart n the program. Frst, we must translate t so VPython can understand t. On the ndented lne ater the whle statement, type the ollowng: cart.pos = cart.pos + (cart.p/cart.m)*deltat Notce how ths statement corresponds to the algebrac equaton: r = r + ( p / m) cart.pos = cart.pos + (cart.p/cart.m)*deltat Fnal poston Intal poston Velocty Tme step
4 In the program, the nal and ntal poston s wrtten the same way: cart.pos. Ths sn t really an equalty, but an assgnment statement; cart.pos s beng assgned a new alue equal to ts old alue plus (cart.p/cart.m)*deltat. Fnally we need to ncrement the cumulate tme t. On the ndented lne ater the whle statement, type the ollowng: t = t + deltat Now, run the program Slowng down the anmaton When you run the program, you should see the sphere at ts nal pont. The program s executed so rapdly that the entre moton occurs aster than we can see. We can slow down the anmaton rate by addng a rate statement. Place the cursor at the end o the statement that reads whle t<3.0:. Press Enter to nsert a new, ndented statement rght beore the cart.pos statement. On ths new lne, type the ollowng: rate(100) Eery tme the computer executes the loop, when t reads rate(100), t pauses long enough to ensure the loop wll take 1/100 th o a second. Thereore, the computer wll only execute the loop 100 tmes per second. Now, run the program. You should see the sphere trael to the rght at a constant elocty, stoppng beyond the edge o the track. Note: The cart gong beyond the edge o the track sn t a good smulaton o what really happens, but t s what we told the computer to do. There are no bult-n physcal behaors, lke gratatonal orce, n VPython. Rght now, all we e done s tell the program to make the cart moe n a straght lne. I we wanted the cart to all o the edge, we would hae to enter statements nto the program to tell the computer how to do ths. CHECKPOINT M2: Ask an nstructor to check your work or credt. You can read ahead whle you re watng to be checked o. 2. Force and momentum prncple Now we wll mody our program to nclude a orce on the cart. Ths means we wll hae to add statements to the program that tell the computer that there s a orce on the cart, and tell t how to use the momentum prncple to change the cart s momentum due to the orce. Suppose you push a cart, and ater leang your hand t has an ntal momentum to the rght, as beore. But now, the an on the cart s opposng ths ntal momentum, so that there s a net orce on the cart to the let. Ths orce s about 0.4 newtons. In your notebook, wrte the answer to the ollowng queston. (4) Wrte the orce ector that ponts to the let (the -x drecton) wth a magntude o 0.4 N. Let s use our program to model ths stuaton. Frst let s add a statement that creates a orce ector. In the loop, nsert a new statement rght ater the rate(100) statement. Type the ollowng:
5 Fnet = ector(-0.4, 0, 0) Note: Ths orce has a constant (ector) alue; t doesn t change each tme through the loop, unlke the cart s poston. We could hae, thereore, wrtten ths statement beore the loop. But n most o the programs we wrte, orce wll be changng wth tme, so orce calculatons should go nsde the loop. Next, we need to use the momentum prncple. The momentum prncple says the change n an object s momentum n a short tme perod s equal to the net orce on t, tmes the tme nteral (ths assumes that the orce doesn t change much durng the short tme nteral). In symbols, ths s: where p s the cart s momentum, and F net p = p p = F net s the net orce on the cart. We can rewrte ths as: p = p + Fnet We then use ths equaton n the program, whch tells the computer how to calculate a new momentum or the cart. Insert a new statement n the loop, rght ater the lne that reads Fnet = ector(-0.4, 0, 0). On ths new lne, type the ollowng: cart.p = cart.p + Fnet*deltat Note the smlarty between ths statement and the statement that updates the poston. Here s how the algebrac equaton corresponds to the VPython code: p = p + Fnet cart.p = cart.p + Fnet*deltat Fnal momentum Intal momentum Net orce Tme step Run the program. You should see the sphere moe to the rght a small dstance beore changng drecton and mong to the let. Change the ntal momentum o the cart so that t moes a greater dstance to the rght beore changng drecton: make the ntal speed o the cart 0.9 m/s, wth drecton to the rght. Run the program. You should now see the sphere moe much closer to the rght end o the track beore t starts mong to the let.
6 3. Moton n two dmensons Let s try gng the cart a component o ntal momentum n the y-drecton (that s, upward). Let s assume that we are n outer space, where gratatonal orces on the cart are neglgble. We want to explore a stuaton where the moton s two-dmensonal. Change the ntal elocty o the cart to 0.7, 0.1, 0 m/s and run the program. The sphere now makes a path n the shape o a parabola. Let s look more careully at the momentum o the cart durng ts moton by prntng out the momentum ector at each executon o the loop. Insert a new statement n the loop, just ater the lne that reads cart.p = cart.p+fnet*deltat. Type the ollowng on ths new lne: prnt t=, t, cart.p=, cart.p Run the program. The alues o tme t and momentum cart.p are now prnted n the text output wndow at eery executon o the loop. Reer to these alues when dong the ollowng exercses. In your notebook, wrte the answers to the ollowng questons. Your nstructor wll ask you about them. (5) Usng the prnted lst o momentum ectors, calculate p. That s, pck one o the ectors to be the nal momentum, and subtract rom t the ector just aboe t, whch s the ntal momentum: p = p p =?,?,? kg m/s (6) Calculate the ector Fnet. (In the program, ths would be Fnet*deltat.) F net =?,?,? N s (7) Do your calculatons show that the change n momentum p s equal to (8) Look at the prnted lst o momentum ectors agan. Why aren t the y or z components o the momentum changng? Add a tral to the cart, to see ts trajectory clearly. You saw n your ball n a box program that ths noles creatng a cure object beore the whle loop, then appendng the mong object s poston to ths cure object eery tme you go through the loop. I the name o your cure object s cart.tral, the statement to add a pont to the cure s cart.tral.append(pos=cart.pos). In your notebook, make approxmate graphs o p x s. t, p y s. t, and p z s. t. Be prepared to explan your graphs. F net? CHECKPOINT M3: Ask an nstructor to check your work or credt. You can read ahead whle you re watng to be checked o. Ater beng checked o, you can submt your nal program to be graded. 4. Playng around What would you need to do to nclude the eects o graty? The gratatonal orce near the Earth s 0, mg, 0, where g = +9.8 N/kg ( the magntude o the gratatonal eld ; see textbook p. 40). Turn on graty and turn o the an orce, use a small deltat o 0.001 or accuracy, and use an statement to make the cart bounce on the track, as you dd to make a ball bounce o the walls o a box. Ge the cart an ntal elocty o 0.7, 3.0, 0 m/s. Eentually you ll see the cart bouncng o nothng! Can you thnk how to change your program to do somethng more realstc? Hnt: You can combne logcal tests by usng the keywords and and or. For example, the logcal expresson (y < 3 ) and (z > 2) s true only both o these condtons are true.