Advertisement
creamygoat

Bouncing

Jan 17th, 2013
456
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.67 KB | None | 0 0
  1. from vegesvgplot import *
  2.  
  3. g = 9.81
  4. m = 5.0
  5. dE = -1.0
  6.  
  7.  
  8. def PrintTime(t):
  9.  
  10.   print 'At t = %gs,' % (t)
  11.  
  12.  
  13. def PrintState(x, v):
  14.  
  15.   Eg = m * g * x[1]
  16.   Ek = 0.5 * m * VLengthSquared(v)
  17.   E = Eg + Ek
  18.  
  19.   print '  x = ' + GFTupleStr(x) + 'm'
  20.   print '  v = ' + GFTupleStr(v) + 'm/s'
  21.   print '  Eg = %gJ' % (Eg)
  22.   print '  Ek = %gJ' % (Ek)
  23.   print '  E = %gJ' % (E)
  24.  
  25.  
  26. def Bounce(x, v):
  27.  
  28.   Eg = m * g * x[1]
  29.   Ek = 0.5 * m * VLengthSquared(v)
  30.   E1 = Eg + Ek
  31.   E2 = max(0, E1 + dE)
  32.  
  33.   v1 = (v[0], -v[1])
  34.   v2 = VScaled(v1, sqrt(E2) / sqrt(E1))
  35.  
  36.   return x, v2
  37.  
  38.  
  39. def Main():
  40.  
  41.   x0 = (0.0, 10.0)
  42.   v0 = (20.0, 0.0)
  43.  
  44.   t = 0.0
  45.   x = x0
  46.   v = v0
  47.   Eg = m * g * x[1]
  48.   Ek = 0.5 * m * VLengthSquared(v)
  49.   E = Eg + Ek
  50.  
  51.   NumBounces = 0
  52.  
  53.   PrintTime(t)
  54.   PrintState(x, v)
  55.  
  56.   dt = sqrt(2.0 * x[1] / g)
  57.  
  58.   t += dt
  59.   x = (x[0] + dt * v[0], 0.0)
  60.   v = (v[0], v[1] - g * dt)
  61.   t1 = t
  62.  
  63.   while v[1] < 0:
  64.  
  65.     PrintTime(t)
  66.     PrintState(x, v)
  67.  
  68.     x, v = Bounce(x, v)
  69.  
  70.     if v[1] > 0.0:
  71.  
  72.       NumBounces += 1
  73.       print 'Bounce %d:' % (NumBounces)
  74.  
  75.       PrintState(x, v)
  76.  
  77.       #Advance to instant before next bounce
  78.       dt = 2.0 * v[1] / g
  79.       x = (x[0] + dt * v[0], 0.0)
  80.       v = (v[0], -v[1])
  81.  
  82.       t += dt
  83.  
  84.       print '%gs later...' % (dt)
  85.  
  86.  
  87.     else:
  88.  
  89.       print 'Splat!'
  90.       PrintState(x, v)
  91.       print
  92.  
  93.   print 'End of simulation\n'
  94.  
  95.   print 'Mass of ball: %gkg' % (m)
  96.   print 'g = %gm/s/s (downward)' % (g)
  97.  
  98.   print 'Initial state:'
  99.   PrintState(x0, v0)
  100.  
  101.   print 'Duration of initial fall: %gs' % (t1)
  102.   print 'Number of bounces:', NumBounces
  103.   print 'Duration of fall and bouncing: %gs' % (t)
  104.  
  105.  
  106. Main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement