#Spring in vertical posiion released from rest from visual import* from visual.graph import* #create some graphs graph1 = gdisplay(x=0, y=0, width=600, height=550, title='Spring Energy', xtitle='Time(s)', ytitle='V(J)', xmax=15.0, xmin=0., ymax=200, ymin=0, foreground=color.white, background=(0.3,0.3,0.3)) VPlot = gcurve(color=color.red) EPlot = gcurve(color=color.yellow) #control how the scene will appear scene.range = 15 scene.autoscale=0 #turn off autoscaling and user roation of scene scene.userspin=0 #define the basic start up parameters Li = 10. ball = sphere(radius = 2.0, pos = (0,Li,0), color = (1,0.5,0.3)) spring = helix(pos=(0,0,0), axis=(0,10,0), coils=6, thickness = 0.45,radius=1.5) spring.length = Li ball.mass = 5 ball.vy = 0 k = 25 g = -9.81 b = 0.0 #linear spring damping term L_eq = (Li+ball.mass*g/k) #the loaded equilibrium position equilibrium = arrow(pos=(5,L_eq,0), shaftwidth=0.1, axis = (-4,0,0)) #since we are only interested in 1 dimension just use magnitudes here F_ball = 0 #this is the force that the spring exerts on the ball = 0 until contact time = 0 ts = 0.005 #need a tiny time step to keep errors down while time < 15.0: rate(100) if scene.mouse.clicked: s = (Li-ball.pos.y)#stretch or compression in spring F_ball = k*s-b*ball.vy#upward force of spring on ball spring.length = spring.length+ball.vy*ts ball.vy = ball.vy + (g + F_ball/ball.mass)*ts #change velocity of ball ball.pos.y = ball.pos.y + ball.vy*ts #change position of ball time = time + ts VPlot.plot(pos=(time,0.5*k*s*s)) #EPlot.plot(pos=(time,-ball.mass*g*(ball.pos.y-L_eq)+98.1))