kellerphysics.comkellerphysics.com/ap_1/ap1_python_all.docx · web viewap physics 1name...

38
AP Physics 1 Name _________________________ Keller VPython programming 1 1. Get a mouse and plug it into one of the side USB ports. Click the Windows icon in the bottom-left and in the Search programs and files box, type VIDLE and hit enter. 2. All programs will begin with same initial line: from visual import * this command simply imports all of the Visual Python features into the Python program 3. Skip a line and type: scene.width=1000 scene.height=800 these commands define the size of the visual window that will appear when the program is run 4. Skip a line and type: scene.autoscale=0 scene.range=(400,400,400) scene.center=(0,0,0) the first of these disables the automatic autoscaling of VPython, allowing you to determine the scale and keep it fixed. The second command sets that scale as having four-hundred units in the x, y, and z planes. The third command puts the position (0,0,0) at the center of that three-dimensional set of planes 5. Skip a line and type: ball=sphere(pos=(0,0,0), radius=50, color=color.blue, material=materials.rough)

Upload: nguyenkhue

Post on 22-May-2018

223 views

Category:

Documents


0 download

TRANSCRIPT

AP Physics 1 Name _________________________KellerVPython programming 1

1. Get a mouse and plug it into one of the side USB ports. Click the Windows icon in the bottom-left and in the Search programs and files box, type VIDLE and hit enter.

2. All programs will begin with same initial line:

from visual import *

this command simply imports all of the Visual Python features into the Python program

3. Skip a line and type:

scene.width=1000scene.height=800

these commands define the size of the visual window that will appear when the program is run

4. Skip a line and type:

scene.autoscale=0scene.range=(400,400,400)scene.center=(0,0,0)

the first of these disables the automatic autoscaling of VPython, allowing you to determine the scale and keep it fixed. The second command sets that scale as having four-hundred units in the x, y, and z planes. The third command puts the position (0,0,0) at the center of that three-dimensional set of planes

5. Skip a line and type:

ball=sphere(pos=(0,0,0), radius=50, color=color.blue, material=materials.rough)

this command creates an object called “ball” and defines it to be a sphere with a given position, radius, color, and texture

6. Now that we have set the scene and put an object in it, we can see how it looks. Click “Run” at the top and then select “Run Module”. This will open both the VPython window in which you will see the ball you’ve made and also a *Python Shell* window with program information in the header.

7. In the VPython window, hold down both mouse keys and move the mouse up and down. This should zoom into and out of the scene. You can also pan around the scene by holding down the right mouse key and moving the mouse up and down, left and right. For now, it’s only going to look like you’re causing the ball to rotate.

8. Close the Vpython window and the shell window, leaving only the program open. Skip a line and type:

print(“This appears in the shell window”)

9. Run the program again and you will see that the ball is still created in the VPython window and the print command has been followed in the shell window. Close both again to return to the program.

10. Make the following changes to the program and run it after each change to notice the effect it has on the scene:

a. change the scene width and height to 600

b. change the scene range to (100,100,100)

c. change the scene center to (20,30,0)

d. change the scene center back to (0,0,0) and move the ball to position (10,20,0)

e. shrink the ball to a radius of 30

f. change the color to one you like (red, green, yellow, orange, cyan, magenta, white)

g. change the material to one you like (wood, plastic, marble, diffuse, emissive, shiny, chrome, blazed, silver, bricks)

11. Go to “File” then “Save As…” and save the file to your desktop as “Your_Name_Python1a.py” and then email it to [email protected]. If you make the next program without emailing this one first, there’s a good chance it will be overwritten.

12. Now use what you have learned to write a program that opens a VPython window that fills the left half of the computer screen and displays a cube of eight different balls, named ball1, ball2, ball3, etc. They must be arranged in the shape of a cube (with a ball as each corner) and each ball should have a different position, size, color, and surface. Here, when you use the mouse functions described in step 7, the effects will be more noticeable.

Save this file as “Your_Name_Python1b.py and then email it.

If time allows, you can play with making other objects shown and explained at: http://vpython.org/contents/docs/primitives.html.

AP Physics 1 Name _________________________KellerVPython programming 2

1. Sign-out your laptop. Create a visual window that fills the left half of the computer screen. Turn off the autoscale and make the scene range 800 in the x, 1000 in the y, and 800 in the z. Set the center of the scene to be (0, 0, 0). [Use Python 1, steps 1-4 for guidance]

2. To create the floor on which objects will slide around, create a box with the following line:

floor = box(pos=(0,-300,0), length=600, height=10, width=600, material=materials.wood)

3. Create a cube that can slide around on the floor with the following line:

cube1 = box(pos=(-290,-285,290), length=20, height=20, width=20, color=color.blue, material=materials.rough)

Because the floor has a height of 10 and is centered on a y-position of -300, the actual top of the floor has a position of -295. And if the cube has a height of 20 and is centered on a position of -285, the bottom of the cube is at -295. This puts the bottom of the cube on the top of the floor.

4. To make the cube slide around on the floor, you’ll need to create a loop in the program. Type only what is italicized.

cube1.velocity = vector(1,0,0)

This tells the program that the cube will move with a velocity of one unit in the x-dimension, zero units in the y-dimension, and zero units in the z-dimension.

time = 0time_step = 1

This just sets the starting time as zero and sets the cube to move in one second intervals

while cube1.pos.x < 290:

This creates a loop that will loop until the cube reaches the right edge of the floor

cube1.pos = cube1.pos + cube1.velocity*time_step

This line tells the cube to move from its current position to a new position based upon its velocity and one second of time passing. Lines like this which occur inside the while-loop must be indented as shown.

time = time + time_step

This simply advances the clock as the program loops through the loop.

rate(80)

This slows down the rate at which the computer cycles through the loop (80 times per second). Otherwise, the cube would move too fast across the floor.If you have programs that freeze when they finish, you can always add as the very last line:

exit( )

This will make the program quit once it has run.

5. Run this program and watch what happens. When it works, save it as Your_Name_Python2a.py and email the file to [email protected].

6. Right now, you have a blue cube sliding from the bottom left corner to the bottom right corner. Add three more cubes (different sizes and different colors) that also slide from the three other corners, counter-clockwise along the edges as seen from above. To do this, you’ll need to create cubes 2, 3, and 4 under the cube1 line, then give each cube its own velocity under the cube1.velocity line, etc.

7. Once this works, save this file as “Your_Name_Python2b.py” and email it.

AP Physics 1 Name _________________________KellerVPython programming 3

1. Create a window that fills the left half of the computer screen. Turn off the autoscale, set the scene range to be 400 in the x, 500 in the y, and 400 in the z, and set the scene center to be (0,0,0).[Use Python assignment 1, steps 1-4 for guidance.]

2. Create a wooden floor near the bottom of the window and a blue sphere near the top of the window. [Use Python 2, step 2 and Python 1, step 5 for guidance]

3. Create a while loop that moves the sphere down towards the floor and stops when it gets there.[Use Python 2, step 4 for guidance]

4. Save this program as Your_Name_Python3a.py and email it.

5. Now we are going to do something new by making the sphere accelerate downward like a falling object.

6. Change the velocity vector so that the ball begins at rest and below that line, add:

ball1.acceleration=vector(0,-1,0)

Then, below the line in the while loop that makes the ball move (pos = pos + velocity*time_step)

make the ball accelerate with something like

ball1.velocity.y=ball1.velocity.y + ball1.acceleration.y*time_step

7. When this works, save this program as Your_Name_Python3b.py and email it.

8. Now modify this program so that, instead of a ball accelerating downward, you have a block accelerating to the right as it slides across the floor and then stops at the edge of the floor. [Use Python 2, step 2 as guidance for making a block] Save this program as Your_Name_Python3c.py and email it.

9. Return to the falling sphere program and modify the program so that the ball bounces off the floor. To do this, you only need to change the while condition to something like:

while time < 1000:

Also, inside the while loop, include the conditional statement:

if [the y position of the ball is less than the top of the floor]:[the y-velocity of the ball equals the y-velocity of the ball times negative one]

Save this program as Your_Name_Python3d.py and email it.

AP Physics 1 Name _________________________KellerVPython programming 3b

1. Open your 3d program for the bouncing ball and check that it runs correctly.

1. Now we are going to add some graphs to the display which match the motion of the bouncing ball.

Beneath from visual import * type the following:

from visual.graph import *

This line imports the graphics capabilities of VPython

2. Beneath the scene.center line, type the following:

position_graph = gdisplay(x=800, y=0, width=400, height=275, xmin=0, xmax=500, ymin=0, ymax=400, xtitle='time (s)', ytitle='position (m)')

This creates a graph window where the top-left corner of the new window has a position of (800,0) on the computer screen. The window has a width of 400 and a height of 275. The graph in the window will have an x-axis that goes from 0s to 500s and a y-axis that goes from 0m to 400m. The axes will have titles of “time(s)” and “position(m)”.

3. One line below, type:

position = gcurve(color=color.red)

This line instructs the program that a position graph will be drawn along these newly created axes and the graph will be red.

4. Beneath ball1.pos = ball1.pos + ball1.velocity*time_step (or your equivalent), type the following:

position.plot(pos=(time,ball1.pos.y))

This line instructs the program to plot a position point along the axes every time the while-loop cycles. The x-coordinate of this point along the graph will be based upon the time. The y-coordinate of this point along the graph will be based upon the sphere’s y-position. Run the program to check that the position graph is correct. You may need to adjust the minima and maxima along the scales.

5. Now add a green velocity graph that appears beneath the position graph. Choose appropriate scales and titles for the axes. When both graphs appear correctly, save this program as Your_Name_Python3e.py and email it to [email protected].

AP Physics 1 Name _________________________KellerVPython programming 4

1. Sign-out a laptop. Create a visual window that fills the left half of the computer screen. Turn off the autoscale and make the scene range 800 in the x, 1000 in the y, and 800 in the z. Set the center of the scene to be (0, 0, 0). [Use Python 1, steps 1-4 for guidance]

2. Create a wooden floor near the bottom of the visual window. [Use Python 2, step 2]

3. Create a marble tower that sits on the left edge of the floor and is about as tall as the floor is wide. The tower can be a box just like the floor is, but with a much smaller length and width and a much greater height. [Use Python 2, step 3]

4. Create a colored sphere that sits on the top of the tower. [Use Python 1, step 5]

5. Give the sphere an initial velocity of 50 in the x-dimension and an acceleration of -10 in the y-dimension. [Use Python 2, step 4 and Python 3, step 6]

In standard units, this gives the sphere an initial velocity of 50m/s to the right and an acceleration of -10m/s2 downward, similar to the surface of the Earth.

6. Define the starting time as zero and give the upcoming while loop a time step of 0.02. [Use Python 2, step 4]

7. To make the sphere move, create a while loop. [Use Python 2, step 4]. Translate into code the following ideas:

while the sphere is above the floor and to the left of the floor’s right edge:position = previous position plus velocity times time stepvelocity = previous velocity plus acceleration times time steprun through this loop eighty times per second

Once you have a sphere that leaves the tower and stops when either it reaches the floor or the right boundary of the floor, save it as Your_Name_Python4a.py and email the file to [email protected].

8. Now lessen the initial velocity of the sphere so that it only travels about one-quarter of the way across the floor before it hits the floor.

9. Also, change the while loop to having only one condition:

while the sphere is left of the floor’s right edge

10. Now add an if condition near the start of the while loop that makes the ball bounce when it reaches the floor. [Python 3, step 9]

When the sphere bounces across the floor and stops at the right edge of the floor, save and email it as Your_Name_Python4b.py.

11. Now we’ll add a few more options. One is to draw a vector which represents the instantaneous velocity of the projectile. Do this with the following line before the while loop:

velocity_vector=arrow(pos= sphere1.pos, axis= sphere1.velocity, shaftwidth=2)

And inside the while loop:

velocity_vector.pos= sphere1.pos velocity_vector.axis= sphere1.velocity

The first of the three lines creates the arrow, the second places the base of the arrow at the center of the ball, and the third orients the arrow along the velocity of the ball. You’ll need to use whatever you called your sphere instead of “sphere1”.

12. We’ll add a trail that shows the path of the projectile in flight. All it takes is adding the following to the properties of the ball (the line with the color and materials):make_trail=True

13. Now we’ll add a display that shows the speed of the ball as it is in flight. Use this line before the while loop:

speed_label=label(pos=(0,0,0), height=10, color=color.yellow, border=4)

This just creates a box in which the information will appear.

14. And these lines in the while loop:

sphere1_speed=round(mag(sphere1.velocity),1) speed_label.text = "Speed is: " + str(sphere1_speed) + " m/s"

What the first of these two lines is doing is defining the speed of the sphere to be the velocity vector magnitude and then rounding it to one decimal place. The second line is defining the text to be inserted into the label created before the while loop.

When your program runs correctly, save it as Your_Name_Python4c and email it.

Bonus (due by the end of class):

Add six graphs on the right side of the computer screen. [Use Python 3b]x-position vs. time y-position vs. timex-velocity vs. time y-velocity vs. timex-acceleration vs. time y-acceleration vs. time

When your program runs correctly, save it as Your_Name_Python4Bonus and email it.

AP Physics 1 Name _________________________KellerVPython programming 5

1. Sign-out a laptop. Create a visual window that fills the left half of the computer screen. Turn off the autoscale and make the scene range 800 in the x, 1000 in the y, and 800 in the z. Set the center of the scene to be (0, 0, 0). [Python 1, steps 1-4]

2. Create a wooden floor near the bottom of the visual window. [Python 2, step 2]

3. Create a marble block (called cube1) that sits on the left edge of the floor. [Python 2, step 3]

4. Now we are going to use some equations from dynamics:ΣF=m·a which is Newton’s second law of motionFfriction = ±μ·FN where μ is the coefficient of friction and FN is the normal force of the floor up on the block

To set this up, type what is in italics.

cube1.mass = 2this gives the block a mass of 2kg

friction_coefficient=0.40this sets the coefficient of friction to be 0.40

normal_force=cube1.mass*9.8because the Earth will pull down on the cube with a force of mass times -9.8N/kg, the floor must push up with a normal force of mass times +9.8

friction_force_magnitude=normal_force*friction_coefficientthis is simply the equation (Ffriction = ±μ·FN) above

friction_force=vector(-1*friction_force_magnitude, 0, 0)because the block will be sliding to the right, we want friction to act leftward in the x-dimension, but not in the y or z dimensions

net_force=friction_forcebecause friction is the only significant force, it is the net force. If there were more forces involved, we could add them to the friction force

cube1.acceleration=net_force/cube1.massthis is simply a rearrangement of Newton’s second law, ΣF=m·a

cube1.velocity=vector(60,0,0)this gives the cube an initial velocity to the right

time=0time_step=0.02

while cube1.pos.x < 280 and cube1.velocity.x>0: cube1.pos = cube1.pos + cube1.velocity*time_step cube1.velocity = cube1.velocity + cube1.acceleration*time_step rate(80)

These lines make the cube move as long as it stays to the left of the floor’s right edge and is moving right. You will probably need to adjust the 280 number to match your floor. If we didn’t include the second condition, the block would slow down and then reverse itself, but this doesn’t happen with real friction.

Save this program as Your_Name_Python5a.py and email it to [email protected].

5. Modify the program so that the block slides from front to back instead of from left to right. As before, it should slow down due to friction and then stop.

6. Save this program as Your_Name_Python5b.py and email it.

7. Close Python5b and reopen Python5a.

8. Add the following for the block sliding from left to right:

- a red velocity vector that moves with the block [Python 4, Step 11]- a green acceleration vector that moves with the block [Python 4, Step 11]- a trail [Python 4, Step 12]

9. Save this program as Your_Name_Python5c.py and email it.

Bonus 1

Add:- a graph of x-position versus time [Python 3b]- a graph of x-velocity versus time- a graph of x-acceleration versus time

Save and email this program as Your_Name_Python5Bonus1.py.

Bonus 2

Replace the friction force with a force of air resistance. Use the model that the force of air resistance is proportional to the velocity of the cube and opposes the velocity of the cube. The graphs should look like exponential decays. Save and email this program as Your_Name_Python5Bonus2.py.

These bonuses must be completed by the end of class.

AP Physics 1 Name _________________________KellerVPython programming 6

1. Create a visual window that fills the left half of the computer screen. Turn off the autoscale and make the scene range 600 in the x, 800 in the y, and 600 in the z. Set the center of the scene to be (0, -200, 0). [Python 1, steps 1-4]

2. Copy what is italicized. The text in regular type-face explains each line.

ground=box(pos=(0,-400,0),size=(600,10,600), material=materials.wood)

Creates the floor, which you’ve done many times before.

ramp=box(pos=(0,-300,0), size=(400,5,200), material=materials.rough)

Creates the ramp on which the block will slide. Initially, this is parallel to the floor.

ramp.rotate(angle=-pi/6, axis=(0,0,1))

Rotates the ramp 30º clockwise. The negative sign makes the rotation clockwise and pi/6 is the same as 30º, but converted to radians, which is what Python assumes. The axis of (0,0,1) tells the program to rotate the ramp around the z-axis.

support=box(pos=(-125,-315,0), height=175, width=10, length=10, material=materials.rough)

This just creates a pillar so that the ramp’s left side isn’t hovering in the air.

block=box(pos=(-100,-230,0), size=(20,20,20), color=color.blue)block.rotate(angle=-pi/6, axis=(0,0,1))

This creates the block that will slide and rotates it so that it rests flat on the ramp.

block.velocity=vector(0,0,0)

The block begins at rest.

block.acceleration=vector(4.33, -2.5, 0)

This is what the acceleration would be on the Earth (to the right and down) with a 30º ramp and no friction.

time=0time_step=0.02

while block.pos.y > -383: block.pos = block.pos + block.velocity*time_step block.velocity = block.velocity + block.acceleration*time_step rate(80)

This is the while loop that makes the block slide until it reaches the floor.

3. When the program runs successfully, save it as Your_Name_Python6a.py and email the file to [email protected].

4. Now modify the program so that the block begins on the ramp near the floor, slides up the ramp, and then slides back down. To do this, you will need to change the initial position of the block and give it an initial velocity in the x and y dimensions. These can be calculated knowing the ramp is inclined by thirty degrees.

Once you get the block to slide up and back down, save it as Your_Name_Python6b.py and email the file.

5. Now make the following additions:

- Give it a velocity of 4 or -4 in the z-dimension- Add a trail [Python 4, Step 12]- Add vectors for velocity and acceleration [Python 4, Step 11]

6. When this program runs successfully, save it as Your_Name_Python6c.py and email the file.

AP Physics 1 Name _________________________KellerVPython programming 7

1. Create a visual window that fills the left half of the computer screen. Turn off the autoscale and set the center of the scene to be (0, 0, 0). [Python 1, steps 1-4]

2. Because this is an astronomic situation, most of the numeric values will be very high because the measurements are in meters. Make the scene range 3E11 in the x, 4E11 in the y, and 3E11 in the z.

3. From here, type only what is in italics:

sun = sphere(pos=(0,0,0), radius=3.48E10, color=color.yellow, material=materials.emissive)This model sun is actually 500 times larger than the scale.

sun.mass = 1.989E30

earth = sphere(pos=(0,0, 1.496E11), radius=6.37E9, material=materials.earth, make_trail=True)This Earth is 1000 times larger than the scale. If made to scale, it would be invisible.

earth.mass = 5.972E24earth.orbit = mag(earth.pos)This last statement just defines the distance of the Earth’s orbit to be the magnitude of its position relative to the sun.

earth.velocity=vector(29800, 0, 0)The Earth does travel at 29,800m/s.

gravity_magnitude_earth = 6.67E-11*earth.mass*sun.mass/earth.orbit**2

This is Newton’s law of universal gravity, FG = G·m·m

R2

acceleration_magnitude_earth=gravity_magnitude_earth/earth.mass

This is Newton’s second law, a = ΣFm

time = 0time_step = 5000

while time < 9.5E7: #this is about three years of time earth.pos=earth.pos + earth.velocity*time_step #this makes the Earth move earth.acceleration = -1*acceleration_magnitude_earth*norm(earth.pos) the acceleration is centripetal, so it is antiparallel to the Earth’s position around the sun

earth.velocity=earth.velocity+earth.acceleration*time_step # vf = vi + a·Δt earth.rotate(angle=0.01, axis=(0,1,0)) this makes the Earth spin on its axis, though much slower than it would so that the spinning can be seen time = time + time_step

rate(200)

4. When the Earth is orbiting, right-click and drag the mouse down to get a more elliptic view. Save this as Your_Name_Python7a.py and email it to [email protected].

5. Add the orbit of Venus, using the following data.

Mass – 4.8676E24 kgPlanet radius – 6.051E9 m (scaled-up)Orbital radius (position) – 1.082E11 mPlanet velocity – 35,020 m/s

6. Save this as Your_Name_Python7b.py and email it.

Bonus: Add Mercury and Mars. Save this as Your_Name_Python7B.py and email it.

AP Physics 1 Name _________________________KellerVPython programming 8

1. Create a visual window that fills the left half of the computer screen. Turn off the autoscale and make the scene range 600 in the x, 800 in the y, and 600 in the z. Set the center of the scene to be (0, 0, 0). [Python I, steps 1-4]

2. Make a wooden floor near the bottom of the visual window which has a size (600, 10, 600). [Python 2, step 2]

3. Create a red cube (called block1) with a size of (40,40,40) that sits on the left edge of the floor. [Python 2, step 3]

4. Create a blue cube (called block2) with the same size that sits on the right edge of the floor. [Python 2, step 3]

5. Give the red cube an initial velocity of 10m/s to the right and the blue cube an initial velocity of -15m/s to the left. [Python 2, step 4]

6. Give the red cube a mass of 2kg and the blue cube a mass of 3kg. [Python 5, step 4]

7. Define the momentum of the red block as mass times velocity. Define the momentum of the blue block as mass times velocity.

8. Set the time to be zero and set the time_step to be 0.1. [Python 2, step 4]

9. Create a while loop which runs as long as the blue block stays on the floor and which moves the two cubes through each other. [Python 2, step 4]

10. If the blocks actually made contact and the collision was inelastic, they would stick together. So you need to create an if-then condition in the while loop that states, “If the two leading faces of the blocks touch each other, then they will move at the velocity determined by conservation of momentum.”

11. You also need to adjust the while loop condition so that the program stops whenever either block reaches an edge of the floor.

When this runs successfully, you should see the two cubes stick, slide left, and stop when they reach the left edge of the floor.

12. Change the mass of block1 to 8kg and run the program again. You should now see them slide right after the collision and stop at the right edge.

13. Save this file as Your_Name_Python8a.py and email it to [email protected].

14. If the collision is elastic, then the two blocks will bounce off each other and both momentum and kinetic energy will be conserved. When you work-out that algebra, you get these two final velocities:

block1.velocity = (2*block2.momentum + (1-(block2.mass/block1.mass))*block1.momentum)/(block2.mass + block1.mass) block2.velocity = (2*block1.momentum + (1-(block1.mass/block2.mass))*block2.momentum)/(block1.mass + block2.mass)

15. Make those two substitutions and run the program to see if the cubes bounce off each other. Again, you can change the masses and the initial velocities to see how those affect the collision. Try making one block extremely more massive than another.

16. Save this file as Your_Name_Python8b.py and email it.

Bonus:

Reopen Python 8a. To the right of the display window, add the following graphs: [Python 3b]

- block1 position versus time- block1 velocity versus time- block2 position versus time- block2 velocity versus time- total momentum versus time- total kinetic energy versus time

Specify the x-dimension and give the gcurve variable a different variable than the concept (i.e. don’t use K to represent both kinetic energy and the gcurve of kinetic energy).

Email this as Your_Name_Python8B.py

Physics Name _________________________KellerVPython programming 9

1. Create a visual window that fills the left half of the computer screen. Turn off the autoscale and make the scene range 600 in the x, 800 in the y, and 600 in the z. Set the center of the scene to be (0, 0, 0). [Python 1, steps 1-4]

2. Create a floor near the bottom of the visual window made of materials.rough. [Python 2, step 2]

3. Create a wooden ball called ball1 with a radius of 25 that sits on the left edge of the floor. [Python 1, step 5]

4. Create a disk above the floor with the following command:

disk1=cylinder(pos=(0,0,0), axis=(0,0,10), radius = 100, material=materials.wood)

The position is the center of the disk, the axis of 10 gives it a thickness and orients the disk to point along the z-axis.

5. Give the ball a velocity of 100 in the x-dimension, set the initial time to zero, and the time_step to 0.01. [Python 2, step 4]

6. Create a while loop that moves the ball across the floor and stops when it reaches the right edge of the floor. [Python 2, step 4]

7. Now we’re going to make the disk rotate in the air and the ball rotate as it moves, like it would do if it was rolling across the floor without slipping.

Inside the while loop, add the commands:

disk1.rotate(angle=-0.01, axis=(0,0,1))This makes the disk rotate 0.01 radians clockwise every time the while loop completes one cycle. The axis tells the disk to rotate around the z-axis.

ball1.rotate(angle=-0.04, axis=(0,0,1))This makes the ball rotate as it moves from left to right. To determine how much it should rotate every

time step, the equation used was Δθ = ΔsR =

v·tR =

(velocity)(timestep)radius

= (100)(0.01)

25 = 0.04

8. Run this program and when to works, save it as Your_Name_Python9a.py and email this to [email protected].

9. Add a second ball, smaller than the first, that rolls from the front of the floor towards the back.

10. Add a second disk, smaller than the first, that touches the first side-by-side and rotates counter-clockwise so that the two disks rotate together as they would if they were meshed gears.

11. Run this program and when to works, save it as Your_Name_Python9b.py and email it.

Bonus: Create a ramp on the right side of the floor so that the first ball rolls up the ramp and slows down to a stop. Save this as Your_Name_Python9B.py and email it.

AP Physics 1 Name _________________________KellerVPython programming 10

1. Create a visual window that fills the left half of the computer screen. Turn off the autoscale and make the scene range 600 in the x, 800 in the y, and 600 in the z. Set the center of the scene to be (0, 0, 0). [Python 1, steps 1-4]

2. Copy what is italicized. The text in regular type-face explains each line.

floor=box(pos=(0,-250,0),size=(600,10,600), material=materials.wood)support1=pyramid(pos=(0,-245,35), size=(70,20,20), axis=(0,1,0),color=color.red, material=materials.rough)support2=pyramid(pos=(0,-245,-35), size=(70,20,20), axis=(0,1,0), color=color.red, material=materials.rough)These last two statements create two pyramids that will support the beam in the center. The position defines the center of the base of the pyramid. The size is the height, length, and width. The axis indicates which way the tip points.

balance=frame()beam=box(frame=balance, pos=(0,-175,0), size=(400,10,100), material=materials.rough, color=color.blue) disc1=cylinder(frame=balance, pos=(-100,-170,0), axis=(0,20,0), radius=20, material=materials.marble)disc2=cylinder(frame=balance, pos=(150,-170,0), axis=(0,20,0), radius=20, material=materials.marble)The first line defines a composite object called the balance. This object is then made of the smaller objects: the beam and the two discs that sit on the beam. We do this so that when the balance beam rotates, we don’t need to make loops for all three objects rotating together, we can just make one loop for the composite object.

disc1_mass=0.5disc2_mass=0.4

radial_position1=vector(disc1.pos-(0,-215,0))radial_position2=vector(disc2.pos-(0,-215,0))These lines define the positions of the disks relative to the fulcrum at (0, -215, 0).

radial_distance1=mag(radial_position1)radial_distance2=mag(radial_position2)

These are saying that distance is the magnitude of radial vector. We need this in the equation for rotational inertia.

theta=0omega=0time=0time_step=0.05The initial angle, angular velocity, and time are all zero.

while -pi/10 < theta < pi/10:While the beam has not yet hit the floor

torque = cross(0.01*radial_position1,(0,-9.8*disc1_mass,0))+ cross(0.01*radial_position2,(0,-9.8*disc2_mass,0))Technically, the torque of each disc is the cross product of the position vector and force of gravity vector.

rotational_inertia = 1+disc1_mass*(0.01*radial_distance1)**2+disc2_mass*(0.01*radial_distance2)**2 This gives the beam a rotational inertia of one and adds it to the two rotational inertias of the discs. alpha = torque.z/rotational_inertia Just like acceleration = force/mass, for rotation, angular acceleration = torque / rotational inertia omega=omega+alpha*time_step ωf = ωi + α·Δt delta_theta = omega*time_step Δθ = ω·Δt theta=theta+delta_theta balance.rotate(angle=delta_theta, origin=(0,-215,0), axis=(0,0,1)) These lines rotate the beam around the fulcrum and the z-axis

time=time+time_step This line advances the clock rate(20)

3. When this program works, save it as Your_Name_Python10a.py and email it to [email protected].

4. Modify the program so that both cylinders sit on the same side of the beam. Give them different radii, colors, and materials. The beam should now rotate faster. When it works, save it as Your_Name_Python10b.py and email.

5. Add a third cylinder (with a unique height, radius, color, and material) to the other side of the beam so that the beam rotates very slowly. When it works, save it as Your_Name_Python10c.py and email.

Bonus: Make graphs [Python 3, Step 8] for angular position (theta), angular velocity (omega), and angular acceleration (alpha) versus time. The graphs should begin at time zero and end when the beam strikes the floor. When it works, save it as Your_Name_Python10Bonus.py and email.

AP Physics 1 Name _________________________KellerVPython programming 11

1. Create a visual window that fills the left half of the computer screen. Turn off the autoscale and make the scene range 600 in the x, 800 in the y, and 600 in the z. Set the center of the scene to be (0, 0, 0). [Python 1, steps 1-4]

2. Copy what is italicized. The text in regular type-face explains each line.

Just below the from visual import * line, type:angle = int(raw_input("Enter an integer angle between -30 and 30: "))This line has the program ask for user input. The int at the front indicates the input will be an integer.

Under the scene center line, continue with the following:

theta = angle *(6.283/360)This converts the angle from degrees to radians, which is what Python computes with.

ceiling=box(pos=(0,250,0),size=(600,10,600), material=materials.wood)

pendulum=frame()string=curve(frame=pendulum, pos=[(0,245,0), (0,-150,0)], color=color.red)ball=sphere(frame=pendulum, pos=(0,-170,0), radius=20, color=color.blue, material=materials.rough)Just like the balance beam, this creates a composite object, a pendulum made of ball and string.

pendulum.rotate(angle=theta, origin=(0,245,0), axis=(0,0,1))This rotates the pendulum from its initial downward position to the angle input by the user.

omega=0The pendulum begins at rest.

# the pound sign is a way of writing notes to yourself about the program lines# these will be ignored by Python when the program runs

time=0 # you can also write comments on a line like thistime_step=0.01

while time < 100: alpha = -9.8*sin(theta)/4.15

This gives the pendulum an angular acceleration dependent upon its angle.

∝ = τI = −mgr·sin (θ)

m·r2 = −g·sin (θ)r

where I converted 415 to 4.15 for r

omega=omega+alpha*time_step ωf = ωi + α·Δt

delta_theta = omega*time_step Δθ = ω·Δt

theta=theta+delta_theta pendulum.rotate(angle=delta_theta, origin=(0,245,0), axis=(0,0,1)) These two lines rotate the pendulum around the contact point and z-axis.

time=time+time_step

rate(80)

3. When it works, save and email the file as Your_Name_Python11a.py.

4. Let’s replace the input angle with an angle between -30° and 30°, selected randomly.

Beneath from visual import * add from random import *to import the randomization function into the program

Then replace angle = int(raw_input("Enter an integer angle between -30 and 30: "))with angle = 30 – 60*(random())

The random() statement creates a random number between 0 and 1, so the line above creates a random angle between -30° and 30°.

5. When this works, save and email the file as Your_Name_Python11b.py.

AP Physics 1 Name _________________________KellerVPython programming 12 – part 1

1. Create a visual window that fills the left half of the computer screen. Turn off the autoscale and make the scene range 600 in the x, 800 in the y, and 600 in the z. Set the center of the scene to be (0, 100, 0).

2. Copy what is italicized. The text in regular type-face explains each line.

ground=box(pos=(0,-25,0),size=(600,10,600), material=materials.wood)This creates the floor, like you’ve done before.

support=box(pos=(200,0,0), size=(50,50,50), material=materials.wood)This creates a little wooden post to which the spring will be fixed.

cube1=box(pos=(-70,0,0), size=(50,50,50), material=materials.rough)This creates the cube that will oscillate.

spring=helix(pos=(175,0,0), radius=15, coils=15, thickness=2)This creates the spring that will connect the cube and the wooden post. Radius is the radius of the coil loops, coils is the number of loops, and thickness is the thickness of the wire. The position is the position of the right end of the spring.

cube1.mass = 20spring_constant=0.2These set the mass of the cube and the spring constant of the spring, how strong the spring is.

cube1.velocity=vector(0,0,0)This sets the cube to begin at rest.

time=0time_step=0.2

while time < 200:This sets the while loop to run for 200 seconds

cube1.pos = cube1.pos + cube1.velocity*time_step This moves the cube from one position to the next depending upon its velocity

cube1_rightface=cube1.pos+(25,0,0) This defines the position of the right side of the cube

support_leftface = support.pos - (25,0,0) This defines the position of the left side of the support

spring.axis = cube1_rightface - support_leftfaceThis tells the program to align the spring between those two positions. Without the two previous statements, the spring would have connected the center of the cube and the center of the support.

spring_force=cube1.pos*spring_constant*-1This is just using the equation for the force applied by a spring (also called Hooke’s law), Fspring= -k·dnet_force=spring_force

Because the only relevant force on the cube is the spring force, this is the net force.

cube1.acceleration=net_force/cube1.mass By Newton’s second law, a = ΣF / m.

cube1.velocity = cube1.velocity + cube1.acceleration*time_step This is just velocity = initial velocity + acceleration x time

rate(50) This tells the program to cycle through the loop 50 times per second

time=time+time_step This advances the clock of the loop

exit() # keeps the program from freezing when finished

3. When the program runs correctly, save it as Your_Name_Python12a.py and email it to [email protected]. If you zoom-in, you’ll see that the coil stays between the two faces rather than passing into the cube and support.

4. Add the following:- a red velocity vector for the block [Python 4, Step 11](you may need to multiply the axis by a factor to make it appear outside the cube)- a green acceleration vector for the block(you can offset the two vectors in the z-dimension so that they don’t overlap)- the following graphs for at least two complete cycles [Python 3b]

position vs. timevelocity vs. timeacceleration vs. timekinetic energy vs. timespring potential energy vs. time

It’s necessary to distinguish between the display of a graph (which you might call kinetic_energy_graph), the numeric value of kinetic energy (which you might call K) and the graph variable (which you might call K_point), so that your lines are:

kinetic_energy_graph = gdisplay(…

K_point = gcurve(color=color.red)

K = 0.5*cube.mass*cube.velocity.x**2

And then, inside the while loop:

K_point .plot(pos=(time, K))

Notice how this last line requires the differentiation.

When these additions work, save and email the program as Your_Name_Python12b.py.

AP Physics 1 Name _________________________KellerVPython programming 12 – part 2

1. Reopen Python 12a. We’re going to add a second window with a vertical oscillator, but first we need to specify that the horizontal oscillator is in its own window which we’ll call “Scene 1”. So take all of your introductory code (between “from visual import *” and when you started creating objects, and rewrite it this way:

scene1=display( title = 'Horizontal Oscillator', x = 0, y = 0, height = 400, width = 600, autoscale=0, range = (600,800,600), center = (0,0,0) )

This is basically like what you’ve had before. I’ve added a title to the window and specified where the top-left corner will be on the screen when the program runs (at x=0, y=0). I also made the scene height smaller than before to make room for scene two.

It’s also in the preferred structure of programs, rather than:

scene1=display(title = 'Horizontal Oscillator', x = 0, y = 0, height = 400, width = 600, autoscale=0, range = (600,800,600), center = (0,0,0))

which is essentially the same thing, but harder to read and correct if there is an error.

Run this reformatted version to make-sure it still works.

2. Reformat your ground, support, cube1, and spring using same block-format as shown above for scene1.

3. After cube1.velocity=vector(0,0,0) and before time_step=0.2, create scene2 using the format above, but with the title Vertical Oscillator and a y-position of 400.

4. Beneath this, create the objects that will go into scene2:

a marble ceiling from which the spring can hanga chrome cube beneath it that will oscillatea second spring that begins at the bottom of the ceiling and reaches the top of the chrome cube

Then give cube2 a mass and initial velocity of zero and give the second spring a spring constant.

5. Inside the while loop, create lines that make cube two oscillate in the same way there are lines that make cube one oscillate. The main difference is that there is now also a force of gravity in the net force for cube two.

You may also want to rename items like spring.axis to spring.axis1 to differentiate it from the now necessary spring.axis2.

When it runs, both cubes should oscillate in their respective windows. Save and email this as Your_Name_Python12c.py.

AP Physics 1 Name _________________________KellerPython Bonus – “for” loops and arrays

Making one block slide doesn’t take that much code. And making four slide is manageable. But for physical systems of dozens or hundreds of particles, the process would be needlessly laborious. As a rule, if something in coding is repetitive and follows a pattern, there is generally some way to code the pattern rather than all of the individual elements.

1. Create a display with a scene height of 800 and width of 600. Set the range to 20 in each dimension, turn-off the autoscale, and set the center as position (5,5,5).

2. Enter the following:

for x in range(1,10): for y in range(1,10): for z in range(1,10): ball = sphere(pos=(x,y,z), radius=0.25, color=(x*0.1,y*0.1,z*0.1), material=materials.rough)

Similar to a “while” loop, a “for” loop runs multiple iterations. The first line creates a list of integers from one to ten and cycles through the loop with x equal to each of those values.

Nested inside that loop is a similar loop for y. Nested inside that loop is a similar loop for z. So, altogether, the first three lines really create a loop that goes from (1,1,1) through (10,10,10), a total of 100 values.

The last line creates the spheres, where their positions depend upon the x,y, and z values of that particular cycle in the loop. The color is also set to depend upon the x, y, and z values, using standard RGB notation. For example, the sphere at position (10, 1, 1) will have 1.0 parts red, 0.1 parts green, and 0.1 parts blue.

3. If it works and creates a rainbow-like cube of spheres, email this as Your_Name_Optional1.py.

Now we would like some way of making multiple particles move without coding each individual movement.

4. Delete the last four lines, change the scene range to (600, 800, 600) and change the scene center to (0,0,0).

5. Type the following:

Nparticles = 10

floor = box(pos=(0,-200,0), height=10, width=400, length=400, material=materials.wood)

time=0time_step=0.01

The first line defines the number of particles is our system. The other lines, you’ve seen before.

6. Enter the following of what is italicized. What is regular-type explains what the lines are doing.

particle = []

Initializes an array called “particle” which is a group of objects. We’re using a singular word to represent a group so that the while loop later-on looks like something you’ve seen before.

for i in range(Nparticles):

range(Nparticles) creates the list of integers from 1 to 10“for i in" means “for each element in this list, perform the following operations”

particle = particle + [sphere(radius = 10, color=color.red)]

Each time the loop cycles at a given value for i, it adds a sphere to the array that can be addressed by its i value

x_coordinate=-120+i*30particle[i].pos = vector(x_coordinate,100,0)particle[i].velocity = vector(0,0,0)particle[i].acceleration = vector(0,-10,0)

The first line gives each element a different horizontal position value based upon its integer value in the range. The next three lines then define the starting kinematics values for each element/particle.

while time < 5000:rate (800)for i in range(Nparticles):

particle[i].pos = particle[i].pos + particle[i].velocity*time_stepif particle[i].pos.y < -185:

particle[i].velocity.y = particle[i].velocity.y*-1particle[i].velocity = particle[i].velocity + particle[i].acceleration*time_step

time=time+time_step

This is very similar to what you already know. The third line means, “for every value of i in our array, complete the following for each element”.

7. If this program runs successfully, email it as Your_Name_Optional2.py.

8. Modify the particle = particle + [sphere(radius = 10, color=color.red)] line so that each particle has a different color based upon its position. Also, give each particle a different initial height, ranging evenly from 50 to 100. Now, when the program runs, it should look something like the pendulum wave. Email it as Your_Name_Optional3.py.

9. Return everyone to the same initial height. Change the number of particles to fifty and adjust the range and floor size as necessary. Now give every particle a slightly different downward acceleration that ranges from -10 on the far left to -5 on the far right. When this works, email it as Your_Name_Optional4.py.