fork download
  1. import math
  2.  
  3. def deg_to_rad(x):
  4. return x*math.pi/180.0
  5.  
  6. def arsinh(z):
  7. return math.log(z+math.sqrt(z*z+1))
  8.  
  9. # The quantity that Shouryya Ray says is constant, according to my interpreteation:
  10. def ray(u,v,g,alpha):
  11. return 0.5*g**2/(u**2)+0.5*alpha*g*( v*math.sqrt(u*u+v*v) / (u*u) + arsinh(math.fabs(v/u)) )
  12.  
  13. # Numerically simulate the motion, printing out Ray's invariant at a certain interval.
  14. # For testing purposes, we also tabulate the KE+PE and show the range.
  15. def projectile(v0,theta,k,t,n1,n2):
  16. # All units are mks.
  17. # v0 = initial speed
  18. # theta = angle of launch, in radians
  19. # k Fair = kv^2
  20. # t = amount of time to simulate
  21. # n1 = number of tests to print out
  22. # n2 = number of iterations between printouts
  23. m = .146 # mass of a baseball
  24. g=9.8 # gravitational field
  25. alpha = k/m
  26. vx = v0*math.cos(theta)
  27. vy = v0*math.sin(theta)
  28. x = 0.0
  29. y = 0.0
  30. dt = t/(n1*n2)
  31. r_th = (v0*v0/g)*math.sin(2.0*theta)
  32. heat = 0
  33. for i in range(n1):
  34. e = .5*m*(vx*vx+vy*vy)+m*g*y+heat
  35. print("u=",vx," v=",vy," Ray=",ray(vx,vy,g,alpha)," KE+PE+heat=",e)
  36. for j in range(n2):
  37. theta = math.atan2(vy,vx)
  38. f_air = k*(vx*vx+vy*vy)
  39. #f_air = k*math.sqrt(vx*vx+vy*vy)
  40. f_grav = m*g
  41. fx = -f_air*math.cos(theta)
  42. if fx>0.0:
  43. print("fx>0, vx=",vx," vy=",vy," theta=",theta)
  44. return
  45. fy_air = -f_air*math.sin(theta)
  46. fy = fy_air-f_grav
  47. ax = fx/m
  48. ay = fy/m
  49. k1 = ax*dt
  50. k2 = (ax+0.5*k1)*dt
  51. k3 = (ax+0.5*k2)*dt
  52. k4 = (ax+k3)*dt
  53. vx = vx + 1/6*(k1+2*k2+2*k3+k4)
  54. k1 = ay*dt
  55. k2 = (ay+0.5*k1)*dt
  56. k3 = (ay+0.5*k2)*dt
  57. k4 = (ay+k3)*dt
  58. vy = vy + 1/6*(k1+2*k2+2*k3+k4)
  59. heat = heat - (fx*vx+fy_air*vy)*dt
  60. if y>0.0 and y + vy*dt<0.0:
  61. print("range=",x,", vs ",r_th," from theory without air resistance")
  62. k1 = vx*dt
  63. k2 = (vx+0.5*k1)*dt
  64. k3 = (vx+0.5*k2)*dt
  65. k4 = (vx+k3)*dt
  66. x = x + 1/6*(k1+2*k2+2*k3+k4)
  67. k1 = vy*dt
  68. k2 = (vy+0.5*k1)*dt
  69. k3 = (vy+0.5*k2)*dt
  70. k4 = (vy+k3)*dt
  71. y = y + 1/6*(k1+2*k2+2*k3+k4)
  72.  
  73. v0 = 45.0 # m/s, a good home-run hit
  74. theta = deg_to_rad(35.0) # degrees
  75. k = 7.0e-4 # realistic value for a baseball
  76. t = 10.0
  77. n1 = 10
  78. n2 = 10000
  79. projectile(v0,theta,k,t,n1,n2)
  80.  
Success #stdin #stdout 1.21s 5836KB
stdin
Standard input is empty
stdout
u= 36.861841993  v= 25.8109396358  Ray= 0.0707591549121  KE+PE+heat= 147.825
u= 30.6460105389  v= 12.4672412589  Ray= 0.0707593974715  KE+PE+heat= 147.821356936
u= 26.6085068359  v= 1.66154761008  Ray= 0.0707595771377  KE+PE+heat= 147.819476425
u= 23.5589998625  v= -7.76317320581  Ray= 0.0859752590612  KE+PE+heat= 147.818185657
u= 20.8611111524  v= -16.0963558001  Ray= 0.104135569518  KE+PE+heat= 147.817053036
range= 120.889926224 , vs  194.171179295  from theory without air resistance
u= 18.250769534  v= -23.2441328751  Ray= 0.120670561911  KE+PE+heat= 147.815868287
u= 15.705986757  v= -29.0905334306  Ray= 0.135389265609  KE+PE+heat= 147.814491849
u= 13.3006299513  v= -33.6533870692  Ray= 0.148680491027  KE+PE+heat= 147.812835214
u= 11.1118542201  v= -37.0763391248  Ray= 0.160964928224  KE+PE+heat= 147.810866343
u= 9.1853959308  v= -39.5657065064  Ray= 0.17256336715  KE+PE+heat= 147.808600709