Advertisement
Guest User

Python

a guest
Sep 21st, 2019
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.43 KB | None | 0 0
  1. # solution starts below
  2.  
  3. # this line loads numpy and matplotlib - it has to be a part of your code
  4.  
  5. %pylab notebook
  6.  
  7. # Initialize Constants
  8. V0 = 50
  9. y0 = 1
  10. x0 = 0
  11. theta0 = 45*pi/180
  12. Cd = .35
  13. r = .23/(2*pi)
  14. A = pi*r**2
  15. m = .142
  16.  
  17. #Setting step size (dt) and number of points
  18. n = 100
  19. t = 8
  20. dt = t/n
  21.  
  22. #Creating Arrays
  23. x = zeros(n)
  24. y = zeros(n)
  25. vx = zeros(n)
  26. vy = zeros(n)
  27. theta = zeros(n)
  28. v = zeros(n)
  29.  
  30. #Initializing conditions and finding trajectory
  31. def trajectory(x, y, vx, vy, theta, v, g, D):
  32. x[1] = x0
  33. y[1] = y0
  34. theta[1] = theta0
  35. vx[1] = V0*cos(theta[1])
  36. vy[1] = V0*sin(theta[1])
  37. v[1] = V0
  38. for i in range(1, n-1):
  39. vx[i+1] = vx[i] - ((cos(theta[i])*.5*D*Cd*A*v[i]**2)/m)*dt #Change in vx due to horizontal component of air resistance
  40. vy[i+1] = vy[i] - ((sin(theta[i])*.5*D*Cd*A*v[i]**2)/m)*dt - g*dt #Change in vy due to vertical component of air resistance (and gravity)
  41. x[i+1] = x[i] + vx[i]*dt
  42. y[i+1] = y[i] + vy[i]*dt
  43. theta[i+1] = math.atan(vy[i+1]/vx[i+1])
  44. v[i+1] = sqrt(vx[i+1]**2+vy[i+1]**2)
  45. if(y[i+1] < 0):
  46. y[i+1] = 0
  47. vy[i+1] = 0
  48.  
  49. figure()
  50. trajectory(x,y,vx,vy,theta,v,9.81,0)
  51. plot(x,y,marker='o')
  52. trajectory(x,y,vx,vy,theta,v,9.803,1.2)
  53. plot(x,y,marker='o')
  54. trajectory(x,y,vx,vy,theta,v,9.796,0.96)
  55. plot(x,y,marker='o')
  56. grid()
  57. xlabel('Horizontal Displacement')
  58. ylabel('Vertical Displacement')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement