Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
One dimensional simulation of a falling object
#One dimensional gravity simulation.
#Programmed by Evan Weinberg
#v0.1 - Initial program
initial_height = 30 #meters
initial_velocity = 10 #m/s
mass = 1 #kilogram
g = 9.81 #m/s^2
is_ground = True #Set this to True if you want to simulate the ground.
is_there_air_resistance = True #Set this to True to turn on air resistance.
air_resistance_coefficient = 0.1 #N*s^2/m^2
steps = 50 #This is how many data points you want to collect
total_time = 5 #seconds
print_data = True
#Simulation data
delta_t = total_time*1./(steps*10)
current_t = 0
current_y = initial_height
current_v = initial_velocity
i = 0
data = []
data.append([current_t,current_y,current_v])
def is_air_resistance(is_there_air_resistance):
if is_there_air_resistance == True:
return 1
else:
return 0
def sgn(x):
if(x >0):
return 1
else:
return -1
while(current_t < total_time):
current_t+=delta_t
accel = 1./mass*(-mass*g-is_air_resistance(is_there_air_resistance)*air_resistance_coefficient*sgn(data[i][2])*data[i][2]**2)
v = data[i][2]+accel*delta_t
y = data[i][1] + data[i][2]*delta_t+0.5*accel*delta_t**2
if(y<0 and is_ground == True):
y = 0
data.append([current_t,y,v])
i = i + 1
#print 't = %s, x = %s, v = %s'%(str(current_t), str(y), str(v))
print "Completed calculations."
outFile = open('gravitysim.txt','w')
count = 0
for row in data:
if(count==0):
count = 0
new_t = row[0]
new_y = row[1]
output = str(new_t)+','+str(new_y)+'\n'
outFile.write(output)
if(print_data == True):
print (output)
count+=1
if(count ==9):
count = 0
outFile.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment