Create a gist now

Instantly share code, notes, and snippets.

CAPM Position & Velocity Data generator
# Written by Evan Weinberg in Python
# 0.1 Initial version
# 0.2 Added initial_t as a variable to allow for the model to start at t!= 0.
# 0.3 Adjusted to make this work for a constant acceleration model.
# This program creates a table of values of position and velocity for a particle moving with constant acceleration.
# The acceleration, velocity, position, and time at the beginning of the interval can all be set in the lines below.
#The initial data for the object goes here.
initial_v = 2
initial_x = -4
initial_t = 0
acceleration = 0
steps = 10 # of data points to be calculated - no need to change this unless you have a good reason.
total_t = 10
delta_t = total_t*1./steps
output = []
current_x = initial_x
current_time = initial_t
current_v = initial_v
#Be careful changing anything below this line - this could break the program.
#That said, can you figure out what it is doing?
total_steps = total_t*1./delta_t
current_step = 0
while(current_step<=total_steps):
print "Time (s): %s Current Position (m): %s Current Velocity (m/s): %s" % (current_time, current_x, current_v)
output.append('%f, %f, %f, %f \n'%(current_time, current_x, current_time, current_v))
current_time += delta_t
current_x = current_x + current_v*delta_t+0.5*acceleration*delta_t**2
current_v = current_v + acceleration*delta_t
current_step += 1
outFile = open('output.txt','w')
for row in output:
outFile.write(row)
outFile.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment