Contact
CoCalc Logo Icon
StoreFeaturesDocsShareSupport News AboutSign UpSign In
| Download
Views: 4665
Kernel: Python 2 (Ubuntu Linux)

Modeling Constant Acceleration

For an object that is accelerating, you need to update the velocity at each time step in addition to updating the position. The easiest way to update velocity is to use the definition of average acceleration:

a=ΔvΔt=vfvitfti\vec{a}=\frac{\Delta \vec{v}}{\Delta t}=\frac{\vec{v}_f - \vec{v}_i}{t_f - t_i}vf=vi+a(tfti)\vec{v}_f = \vec{v}_i + \vec{a}(t_f - t_i)

Because of the way compute programs work, the line ball.vel = ball.vel + g*dt is equivalent to the above equation. In other words, Python calculates everything to the right of the equals sign and then assigns it to the variable to the left. This means that ball.vel on the right side is the initial velocity vi\vec{v}_i, while ball.vel on the left side is actually the final velocity vf\vec{v}_f.

Run the code below to see a ball moving with a constant gravitational force. You'll need to click on the animation frame to get the motion started.

from vpython import * scene=canvas() #Create canvas to draw on graph_screen=graph(width=400,height=400,xtitle='time', ytitle='Position',xmin=0,xmax=3,ymin=-10,ymax=10) pos_graph = gcurve(color=color.blue, width=4) #Create curve to plot position vs. time graph_screen2=graph(width=400,height=400,xtitle='time', ytitle='Velocity', xmin=0,xmax=3,ymin=-20,ymax=10) vel_graph = gcurve(color=color.red,width=3) #Curve for velocity ground=box(pos=vector(0,-1,0),axis=vector(1,0,0),length=20,height=0.1,color=color.green) #Grass-colored box for reference ball=sphere(color=color.blue) #Create the ball ball.vel=vector(10,10,0) #Give ball a velocity attach_arrow(ball,"vel",scale=.5,color=color.yellow) #Attach an arrow to show velocity attach_trail(ball,color=color.yellow,type="points",retain=10,pps=1) g=vector(0,-9.8,0) dt=0.01 t=0 tf=3 scene.waitfor('click') #Won't start animation until you click inside the frame while t<tf: rate(30) ball.pos=ball.pos+ball.vel*dt #update position ball.vel=ball.vel + g*dt #update velocity position=ball.pos.y #For plotting position velocity=ball.vel.y #For plotting velocity t=t+dt #Advance time step pos_graph.plot(pos=(t,position)) #Plot a single data point on the graph vel_graph.plot(pos=(t,velocity))
MIME type unknown not supported

The ball leaves a trail behind it because of the attach_trail() command. The arrow pointing in the direction of motion is from the attach_arrow() command.

The two graphs below the animation frame dispaly the y-position vs. time graph and the y-velocity vs. time graph.

For You To Try

  • Add a second object with a different initial velocity

  • Add curves to both graphs for this new object and plot the y-position and y-velocity

For You To Try

  • Plot the analytical solution for the y-position on the graph to compare to the computational result

To compare this result to the exact analytical result (yf=yi+v0yt+12at2y_f = y_i+v_{0y}t + \frac{1}{2} a t^2) to the computed results, we can plot the analytical solution on the same graph as the position. To do this you will need to add another gcurve() to the first graph screen. Create another gcurve called pos_exact_graph and insert the code below where it says ### Add code for pos_exact_graph here. Give the curve a different color.

Next you will need to plot the graph. You can do this inside the while loop by calculating the y-position using yf=yi+v0yt+12at2y_f = y_i+v_{0y}t + \frac{1}{2} a t^2 and then calling pos_exact_graph.plot() to plot the exact position vs. time. All of this code can go where it says ###Add code to calculate position using analytical equation and plot it here. I recommend shifting yiy_i for the exact solution up 1 m so the two curves are slightly shifted.

from vpython import * scene=canvas() #Create canvas to draw on graph_screen=graph(width=400,height=400,xtitle='time', ytitle='Position',xmin=0,xmax=3,ymin=-10,ymax=10) pos_graph = gcurve(color=color.blue, width=4) #Create curve to plot position vs. time ### Add code for pos_exact_graph here graph_screen2=graph(width=400,height=400,xtitle='time', ytitle='Velocity', xmin=0,xmax=3,ymin=-20,ymax=10) vel_graph = gcurve(color=color.red,width=3) #Curve for velocity ground=box(pos=vector(0,-1,0),axis=vector(1,0,0),length=20,height=0.1,color=color.green) #Grass-colored box for reference ball=sphere(color=color.blue) #Create the ball ball.vel=vector(10,10,0) #Give ball a velocity attach_arrow(ball,"vel",scale=.5,color=color.yellow) #Attach an arrow to show velocity attach_trail(ball,color=color.yellow,type="points",retain=10,pps=1) g=vector(0,-9.8,0) dt=0.01 t=0 tf=3 scene.waitfor('click') #Won't start animation until you click inside the frame while t<tf: rate(30) ball.pos=ball.pos+ball.vel*dt #update position ball.vel=ball.vel + g*dt #update velocity position=ball.pos.y #For plotting position velocity=ball.vel.y #For plotting velocity t=t+dt #Advance time step pos_graph.plot(pos=(t,position)) #Plot a single data point on the graph ###Add code to calculate position using analytical equation and plot it here. vel_graph.plot(pos=(t,velocity))
MIME type unknown not supported
MIME type unknown not supported
MIME type unknown not supported
MIME type unknown not supported
MIME type unknown not supported
MIME type unknown not supported
MIME type unknown not supported
MIME type unknown not supported
MIME type unknown not supported

Once you are done you can move on to Motion Diagrams in VPython.