## Model 2: Graphs of Motion from __future__ import division from visual import * from visual.graph import * ## invoke graphing routines scene.x=512 #scene.y = 600 scene.width =800 scene.height = 380 ## User input to get program parameters #### Get values for the initial velocity and force x0 = vector(input("Enter the initial position:")) x0.y=0 #Motion in 1-D for now! x0.z=0 print "Initial position =", x0 v0 = vector(input("Enter the initial x-dir velocity:")) v0.y=0 #Motion in 1-D for now! v0.z=0 print "Initial velocity =", v0 a = vector(input("Enter the x-dir acceleration:")) a.y=0 #Motion in 1-D for now! a.z=0 print "Acceleration =", a #### Define the initial time and time step size ##deltat = input("Enter the time step size:") ##t = 0 deltat=0.01 t=0 ## OBJECTS AND INITIAL VALUES track = box(pos=vector(0,-.35,0), size=(4.0,.05,.10), color=color.white) y_offset = track.pos.y + track.size.y/2 track_x_offset = track.size.x/2 #Make x=0, the left edge of the track x0.x=x0.x-track_x_offset x0.y=x0.y+y_offset cart = box(pos=x0,size=(.1,.04,.06), color=color.green) cart.m = 1 #incline = box(pos=vector(0,-.35,0), axis=(1,1,0), # size=(4.0,.05,.10), color=color.white) ## Define initial cart momentum using mass and velocity cart.p = cart.m*v0 print 'initial cart momentum =', cart.p ## create object that draws a trail behind cart trail = curve(color=cart.color) ## SET UP GRAPHS ## x-motion graphs XMothionWindow = gdisplay(title='x-dir displacement, velocity and acceleration', xtitle='t', ytitle='x(m) - yellow, v_x(m/s) - red, a_x(m/s^2) - cyan', width=512, height=200) x_graph = gcurve(color=color.yellow) ## create a gcurve for x displacement dx_initialize = gdots(color=color.black) #Plot a dot to cut down on autoscaling dx_initialize.plot(pos=(.5, .5)) ## create a gcurve for x velocity XVelWindow = gdisplay(x=0, y=201, title='x-dir velocity',xtitle='t', ytitle='vx(m/s)', width=512, height=200) vx_graph = gcurve(color=color.red) vx_initialize = gdots(color=color.black) #Plot a dot to cut down on autoscaling vx_initialize.plot(pos=(.5, .5)) # create a gcurve for x acceleration XAccWindow = gdisplay(x=0, y=401, title='y-dir accel',xtitle='t', ytitle='ax(m/s^2)', width=512, height=200) ax_graph = gcurve(color=color.cyan) ax_initialize = gdots(color=color.black) #Plot a dot to cut down on autoscaling ax_initialize.plot(pos=(.5, .5)) g=9.8 ## CALCULATIONS while -2.1< cart.pos.x <= 2 and t<2: #print 'the time is now', t #Ffan = vector(-10,0,0) Fair = -mag(cart.p/cart.m)*cart.p/cart.m Fgrav = cart.m*vector(0,-g,0) Fnet = a*cart.m ## momentum update cart.p = cart.p + Fnet*deltat ## position update cart.pos = cart.pos + (cart.p/cart.m)*deltat if cart.pos.y<-.3: cart.pos.y=-.3 cart.p.y=0 ## update trail trail.append(pos=cart.pos) ## x-dir plots x_graph.plot(pos=(t,cart.pos.x + track_x_offset)) ## add a point to the x-position graph vx_graph.plot(pos=(t,cart.p.x/cart.m)) ## add a point to the x-dir velocity graph ax_graph.plot(pos=(t,Fnet.x/cart.m)) ## add a point to the x-dir acceleration graph ## advance time t = t + deltat rate(20) print 'After the loop. The final time is', t