Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
from random import *
from math import *
g= 9.8
y0 = 1.9
horizontalDistance = 20.0
hoopHeight = 3.05
startingV = 10
startingT = 30
class Projectile():
def __init__(self,v0,theta):
self.theta = theta
self.v0 = v0
self.error = self.getError(v0,theta)
def getError(self,v0,theta):
arrivalTime = horizontalDistance/(v0*cos(theta*pi/180))
finalHeight = y0 + v0*sin(theta*pi/180)*arrivalTime-0.5*g*arrivalTime*arrivalTime
return (finalHeight-hoopHeight)
class Projectiles():
def __init__(self,N,startingV,startingT,endingV,endingT):
self.N = N
self.startingV = startingV
self.startingT = startingT
self.endingV = endingV
self.endingT = endingT
self.array = self.generate()
self.ordered = self.array.sort(key=lambda x: abs(x.error))
def generate(self):
emptyArray = []
for i in range(self.N):
emptyArray.append(Projectile(uniform(self.startingV,self.endingV),uniform(self.startingT,self.endingT)))
return emptyArray
for j in range(15):
startingV = 10
startingT = 40
endingV = startingV*2
endingT = startingT*3
error = 10
changeError = 10
while(abs(changeError)>0.0001):
a = Projectiles(5000,startingV,startingT,endingV,endingT)
startingV = a.array[0].v0
startingT = a.array[0].theta
endingT = a.array[20].theta
endingV = a.array[20].v0
changeError = error - a.array[0].error
error = a.array[0].error
#print("error: "+str(a.array[0].error) +", v0: " + str(a.array[0].v0) + ", theta:"+str(a.array[0].theta))
print("Calc.setExpression({id:"+str(j)+",latex:'("+str(a.array[0].v0)+"*cos("+str(a.array[0].theta*pi/180)+")*t,"+str(y0)+"+"+str(a.array[0].v0)+"*sin("+str(a.array[0].theta*pi/180)+")*t-4.9*t^2)'})")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment