# this version of the Euler-Cromer code should look quite like the 1-D version # except that acceleration, velocity, and position are all defined to be 3-D # vectors. You'll notice in the line "funct1.plot(pos=(r.x,r.y))" that when you # just want the x or y component of a vector, you call for it by appending # ".x" or ".y" to the vector name. from __future__ import division from visual import * from visual.graph import * # set up for the graph graph1 = gdisplay(title="y vs. x",xtitle="x", ytitle="y") funct1 = gcurve(gdisplay=graph1, color=color.cyan) # constant gmag=9.8 #the magnitude of the acceleration due to gravity g = vector(0,-gmag,0) #the vector for acceleration due to gravity # set initial conditions & time step t=0 dt=0.002 r = vector(0,0,0) #initiallizing position vector v = vector(15,15,0) #initializing velocity vector # plot initial position funct1.plot(pos=(r.x,r.y)) # apply the Euler-Cromer method to each component while r.y >= 0: #continue the calculation until the projectile lands a = g #set accleration to always be that due to gravity v = v + a*dt #use this acceleration to update velocity r = r + v*dt #use this velocity to update position t=t+dt funct1.plot(pos=(r.x,r.y)) # add the new point to the graph