language: Python 3 (python-3.2.3)
date: 355 days 14 hours ago
link:
visibility: public
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import math
 
def deg_to_rad(x):
  return x*math.pi/180.0
 
def arsinh(z):
  return math.log(z+math.sqrt(z*z+1))
 
# The quantity that Shouryya Ray says is constant, according to my interpreteation:
def ray(u,v,g,alpha):
  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)) )
 
# Numerically simulate the motion, printing out Ray's invariant at a certain interval.
# For testing purposes, we also tabulate the KE+PE and show the range.
def projectile(v0,theta,k,t,n1,n2):
  # All units are mks.
  # v0    = initial speed
  # theta = angle of launch, in radians
  # k     Fair = kv^2
  # t     = amount of time to simulate
  # n1    = number of tests to print out
  # n2    = number of iterations between printouts
  m = .146 # mass of a baseball
  g=9.8 # gravitational field
  alpha = k/m
  vx = v0*math.cos(theta)
  vy = v0*math.sin(theta)
  x = 0.0
  y = 0.0
  dt = t/(n1*n2)
  r_th = (v0*v0/g)*math.sin(2.0*theta)
  heat = 0
  for i in range(n1):
    e = .5*m*(vx*vx+vy*vy)+m*g*y+heat
    print("u=",vx," v=",vy," Ray=",ray(vx,vy,g,alpha)," KE+PE+heat=",e)
    for j in range(n2):
      theta = math.atan2(vy,vx)
      f_air = k*(vx*vx+vy*vy)
      #f_air = k*math.sqrt(vx*vx+vy*vy)
      f_grav = m*g
      fx = -f_air*math.cos(theta)
      if fx>0.0:
        print("fx>0, vx=",vx," vy=",vy," theta=",theta)
        return
      fy_air = -f_air*math.sin(theta)
      fy = fy_air-f_grav
      ax = fx/m
      ay = fy/m
      vx = vx + ax*dt
      vy = vy + ay*dt
      heat = heat - (fx*vx+fy_air*vy)*dt
      if y>0.0 and y + vy*dt<0.0:
        print("range=",x,", vs ",r_th," from theory without air resistance")
      x = x + vx*dt
      y = y + vy*dt
 
v0 = 45.0 # m/s, a good home-run hit
theta = deg_to_rad(35.0) # degrees
k = 7.0e-4 # realistic value for a baseball
t = 10.0
n1 = 10
n2 = 10000
projectile(v0,theta,k,t,n1,n2)
 
  • upload with new input
  • result: Success     time: 0.48s    memory: 5852 kB     returned value: 0

    u= 36.861841993  v= 25.8109396358  Ray= 0.0707591549121  KE+PE+heat= 147.825
    u= 30.6462536241  v= 12.4678301768  Ray= 0.0707593974484  KE+PE+heat= 147.82340481
    u= 26.6088469837  v= 1.66254890559  Ray= 0.0707595771036  KE+PE+heat= 147.822430352
    u= 23.5594201658  v= -7.76184161898  Ray= 0.0859724743979  KE+PE+heat= 147.821713101
    u= 20.8616382626  v= -16.0948023952  Ray= 0.104132074212  KE+PE+heat= 147.821152309
    range= 120.889365695 , vs  194.171179295  from theory without air resistance
    u= 18.251416064  v= -23.2425061291  Ray= 0.120666666457  KE+PE+heat= 147.820728647
    u= 15.7067336398  v= -29.0889765847  Ray= 0.135385086927  KE+PE+heat= 147.820430788
    u= 13.3014376668  v= -33.6520004806  Ray= 0.148676037201  KE+PE+heat= 147.82023562
    u= 11.1126791141  v= -37.0751711583  Ray= 0.16096016949  KE+PE+heat= 147.820114408
    u= 9.18620095669  v= -39.5647637  Ray= 0.172558265671  KE+PE+heat= 147.82004161