##This is how to execute the find x given y code I gave you without the wonders of VPython import math ###set givens for this problem ##Global givens V = 5 theta = 20 ballm = 0.1 # mass in kg deltat = 0.01 t = 0 #Air Drag factors Cd = 0.5 A = 0.01 p = 1.2 ##x, y specific givens y0 = 0 y = 2 x0 = 0 #Convert angle to radians, calc components of vectors #and vectorize everything that ought to be vectorized #This is where it gets messy... theta = theta*math.pi/180 #converts angle to radians vy0 = V*math.sin(theta) vx = V*math.cos(theta) #What follows are called 'lists'. They are not #vectors, but we'll pretend they are because it's #the best option we have. ballv = [vx, vy0, 0] ballpos = [x0, y0, 0] g = [0,-9.8,0] #Initialize zero vectors of length 3 (for x, y and z directions) Fg = [0,0,0] Fd = [0,0,0] Fnet = [0,0,0] balla = [0,0,0] #Is the ball ending up above, below or at the same height as it starts? finalH = 1 if(y > y0): finalH = 2 #Python is an index-0 language, which means the first item in a list is labeled as the 0th item #The next item is the first, etc. To access a particular item from a list, you can #simply type in the list name (like ballpos) and then right next to it, in square brackets, #type in the index number for that item. Item 1 (the second item) in ballpos corresponds #to the y-position of the ball. while((ballpos[1] < y and finalH > 1) or (ballpos[1] >= y and finalH == 1)): #This is a 'for loop'. A for loop basically says, 'For every item from this here #first number (0 in the below example) to this other number (the length of the ballpos list) #execute the following code. #In this case, i goes from 0 to 2, and what it allows us to do is perform the same #set of calculations for every item in our various lists. Since each direction is #independent, we can do all the x-axis stuff first, then the y-axis, then the z-axis for i in range(0, len(ballpos)): Fg[i] = ballm*g[i] Fd[i] = -0.5*Cd*p*A*ballv[i]*ballv[i] Fnet[i] = Fg[i] + Fd[i] balla[i] = Fnet[i]/ballm ballv[i] = ballv[i] + balla[i]*deltat ballpos[i] = ballpos[i] + ballv[i]*deltat #Evaluate whether we've hit the desired height -- may be hit twice! if(ballpos[1] > y and finalH > 1): finalH = 1 print(t, ballpos[0], ballpos[1], y) if(ballpos[1] < y and finalH > 1 and ballv[1] < 0): print("Not going to make it!") break t += deltat print(t, ballpos[0], ballpos[1], y)
Run
Reset
Share
Import
Link
Embed
Language▼
English
中文
Python Fiddle
Python Cloud IDE
Follow @python_fiddle
Browser Version Not Supported
Due to Python Fiddle's reliance on advanced JavaScript techniques, older browsers might have problems running it correctly. Please download the latest version of your favourite browser.
Chrome 10+
Firefox 4+
Safari 5+
IE 10+
Let me try anyway!
url:
Go
Python Snippet
Stackoverflow Question