# This program uses the Euler-Cromer method to update the position of a ball in response to # a force, here defined as a spring force in the x direction. It has three types of output; # it annimates a ball and traces it's trajectory, it plots the x component of the ball's # positon against time, and it prints the final momentum and position of the ball. # This current version gives the ball a mass of 1kg so that the ball's velocity is numerically # equal to its momentum. This should be a useful 'shell' for you to modify to address a # variety of computational problems. from __future__ import division from visual import * from visual.graph import * # Constants k=5 b=0 t=0 tmax = 60 dt=0.001 # Objects ballec=sphere(pos = (10,0,0), p=vector(0,0,0), m=1, radius = 0.1, color=color.red) # the above line defines a ball with initial position (10,0,0)m, # initial momentum (0,0,0)kg m/s, a radius of 0.1 m, and a mass of 1 kg. balle=sphere(pos = (10,0,0), p=vector(0,0,0), m=1, radius = 0.1, color=color.cyan) trail1=curve(color = ballec.color) # This sets up the program for creating a curve which we will, # inside the loop below, choose to trace the path of the ball trail2 = curve(color = balle.color) funct1 = gcurve(color=ballec.color) funct2 = gcurve(color = balle.color) funct1.plot(pos=(t,ballec.x)) # Plot the initial point funct2.plot(pos =(t,balle.x)) trail1.append(pos=ballec.pos) # Start's tracing the ball's trail at it's initial position trail2.append(pos=balle.pos) Fe = -k*vector(balle.x,0,0) #Execution while t