Week 3 Computing Homework

Modeling Motion
Due: January 20

The purpose of this week's homework is to get used to using Python as a language and using it to simulate uniform acceleration. In particular, we will study two possible ways of writing a simulation: 1) using numerical methods to compute approximate values of a function, in this case then displacement function for uniform acceleration and 2) computing the result directly using the equation representing the exact solution. Note that in real systems, that equation is often not known and the first method is the only one available. We will also look at how to write a Python function, the 'if' statement and use some of the graphical capability of VPython.

Many of the exercises ask you to make small modifications to the same program. The only thing you need to turn in is your answers to the questions and a listing of your final program. Don't forget to "save early save often".

  1. We'll start with the program you worked on for this week's computing assignment--a simulation of a particle undergoing uniform acceleration.

    x  =  0   # starting position
    v  =  5   # v is velocity in m/s
    a  = -1   # a is the acceleration in m/s/s
    t  =  0   # starting time in seconds
    dt =  1   # delta t, the change in time
    
    while t<=20 :
          print t,x
          x = x + v*dt
          v = v + a*dt
          t = t +   dt
    

    Cut and paste this Python code into IDLE and run it to make sure you copied it correctly. It should print out displacement values for times up to t=20. Record the final displacement at t=20.

    Solution

    Final displacement at t=20 with Δt=1 is -90m.

  2. Now change the program to simulate the results with a smaller time step, Δt, of 0.1 seconds instead of one second and rerun the simulation. You'll find that due to numerical approximations within the computer, the simulation won't go all the way up to 20 seconds unless you adjust the time limit up a little in the while statement's test (t<=20). Try adding dt on to the limit so it is one step higher than needed and change the '<=' (less than or equal) to just '<' (less than).

    Questions

    1. What is the particle's final displacement after 20 seconds? -99.0m @ t=20s
    2. How does it compare to the result from your first run with Δt=1s?Uh..Its lower (more negative).
    3. How do you account for the different result? Which simulation was likely closer to the "real" answer and why do you think so?Since we have been assuming velocity is constant over each time interval Δt, we have been getting only an approximate answer. Using a smaller Δt reduces the error because the result for a particular time step will be closer to the actual curved path we are approximating with a straight line.

  3. We're getting quite a lot of output when all we really care about is the final number at t=20s. Try moving the print statement down to the bottom of the program after the while loop has finished. Remember that Python uses indention to recognize what belongs in the body of the 'while' loop so you must remove the indention from the print statement. Now rerun the program. Why don't you get the value at t=20? Modify the program again to make it print out the value at t=20.
  4. Use your program to complete the following table:

     

    Δtx at 20s
    1s 
    .1s 
    .01s 
    .001s 
    .0001s 

    Solution

    Δtx at 20s
    1s-90m
    .1s-99m
    .01s-99.9m
    .001s-99.99m
    .0001s-100.0005m

    (Note: You could use a 'for' loop around your 'while' loop to change dt and rerun the while loop automatically.)

    Questions

    1. As Δt decreases what happens to x?x gets closer to -100. It oversteps it a little in the final row because rounding errors caused us to go a little past 20s, but -100.0005m is still closer to -100m than -99.99m.
    2. What do you think the "right" answer is?-100m
    #--------------------------------------------------------
    # UNIFORM.PY - Uniform motion in Python
    #              This version runs the same simulation
    #              for increasingly small delta t's printing
    #              The numerical solution at t=20s.
    # AUTHOR: Barry Tolnas
    # CREATED: 1/6/04
    #--------------------------------------------------------
    from visual import *
    
    x  =  0   # starting position
    v  =  5   # v is velocity in m/s
    a  = -1   # a is the acceleration in m/s/s
    t  =  0   # starting time in seconds
    
    for dt in [1, .1, .01, .001, .0001]:
          #don't forget to reset all values that were modified
          #the previous run through the simulation!
          t=0 
          x=0 
          v=5 
          while t<20 :
                x = x + v*dt
                v = v + a*dt
                t = t +   dt
          print t,x # print the final result at t=20 only
    
  5. Recall the equation from your physics notes for uniform acceleration that relates final displacement as a function of time, initial position, initial velocity, and acceleration, x=x0+v0t+(1/2)at2. We should be able to use this equation instead of our numerical simulation to get a much more accurate approximation because this equation gives the exact solution. The only reason we can't get an exact answer from the computer is because it can't perform the calculations with 100% accuracy (but you can by hand on paper).

    Define a Python function named displacement which uses this equation to compute displacement for a given time. There is an example of functions on the website HERE and you should check the Python Tutorial or a book for more examples. The function should allow the following program to run:

         print "The displacement at t=20 is",displacement(20)
    

    Solution

    def displacement(t):
        return x0 + v0*t + a*t*t/2.0
    

    Note: this function assumes that values for x0, v0, and a have already been defined prior to the function definition in the program.

    Note 2: Many people wrote a function that takes x0, v0, and a as additional arguments. While this makes the function more general it would not work in the one-line program given in the exercise which assumes only a single argument (time).

  6. Write a program using your displacement function to draw a graph of displacement over time for our particle. Use the SIR sample code which draws plots of the S, I, and R values from our epidemic model in calculus as an example of how to use the graphing functions in VPython. Also check the VPython documentation for information on using the graphing objects, gdisplay and gcurve.

    Solution

    #-----------------------------------------
    # UNIFORM.PY - Uniform motion in Python
    #              This version gives an exact solution solved analytically
    #              rather than a numerical approximation and graphs the result
    #              using VPython's graph module.
    # AUTHOR: Barry Tolnas
    # CREATED: 1/6/04
    #-----------------------------------------
    from visual.graph import *
    
    x0 =  0   # starting position
    v0 =  5   # v is velocity in m/s
    a  = -1   # a is the acceleration in m/s/s
    t  =  0   # starting time in seconds
    dt =  1   # delta t, the change in time
    
    def displacement(t):
        return x0 + v0*t + a*t*t/2.0
    
    graph = gdisplay()
    curve = gcurve()
    
    # For a slightly fancier graph replace the above lines with these:
    #graph = gdisplay(title="Uniform Acceleration", xtitle="displacement", ytitle="time")
    #curve = gcurve(color=(1,1,0))
    
    while t<=20 :
          x = displacement(t)
          print t,x
          curve.plot(pos=(t,x))
          t = t+dt
    
    
  7. Now lets make a ball that bounces. The following program shows an animated ball dropping onto a box centered around the origin representing the ground. Its simulates 1.6 seconds of time and shows the ball falling right through the ground!. Modify this program to make the ball bounce off of the ground. To do this, check to see if the ball's position is low enough that it is touching the ground's surface or below. Keep in mind the ball's radius (1m) and the box's thickness (0.2m). If the ball is touching the box or is slightly intersecting it, change its velocity so it is moving at the same speed but in the opposite direction. (Hint: use an 'if' statement)
    from visual import *
    
    scene.range=10      #How many units wide the display window shows
    scene.center=(0,5,0)#The point in the center of the window
    
    v  = 0      #initial velocity
    t  = 0      #initial time
    dt = 1/30.0 # delta t
    y  = 10     # initial position
    
    #setup objects in the scene
    ball   = sphere(pos=(0,y,0), color=(1,0,0))
    ground = box(pos=(0,0,0), size=(20,.2,20), color=(0,1,0))
    
    print "ball radius=",ball.radius
    print "box thickness=",ground.size.y
    
    while t<1.6:
        ball.y = ball.y + v*dt
        v = v - 9.8*dt
        t = t + dt
        rate(1/dt)  #this controls the frame rate of animation
    

    Solution

    # Week 3 Computing HW Solution for exercise 7  -- the bouncing ball problem.
    from visual import *
    
    scene.range=10
    scene.center=(0,5,0)
    
    v  = 0      #initial velocity
    t  = 0      #initial time
    dt = 1/30.0 # delta t
    y  = 10     # initial position
    
    #setup objects in the scene
    ball   = sphere(pos=(0,y,0), color=(1,0,0))
    ground = box(pos=(0,0,0), size=(20,.2,20), color=(0,1,0))
    
    
    print "ball radius=",ball.radius
    print "box thickness=",ground.size.y
    
    while t<10:
        ball.y = ball.y + v*dt
        v = v - 9.8*dt
    
        # bounce if one radius from the ground
        if ball.y<(ball.radius+ground.size.y/2.0):
            ball.y=ball.radius
            v = -v
        t = t + dt
        rate(1/dt) #animation rate in frames per second
    
    print "ending at time t =",t