Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from vegesvgplot import *
- g = 9.81
- m = 5.0
- dE = -1.0
- def PrintTime(t):
- print 'At t = %gs,' % (t)
- def PrintState(x, v):
- Eg = m * g * x[1]
- Ek = 0.5 * m * VLengthSquared(v)
- E = Eg + Ek
- print ' x = ' + GFTupleStr(x) + 'm'
- print ' v = ' + GFTupleStr(v) + 'm/s'
- print ' Eg = %gJ' % (Eg)
- print ' Ek = %gJ' % (Ek)
- print ' E = %gJ' % (E)
- def Bounce(x, v):
- Eg = m * g * x[1]
- Ek = 0.5 * m * VLengthSquared(v)
- E1 = Eg + Ek
- E2 = max(0, E1 + dE)
- v1 = (v[0], -v[1])
- v2 = VScaled(v1, sqrt(E2) / sqrt(E1))
- return x, v2
- def Main():
- x0 = (0.0, 10.0)
- v0 = (20.0, 0.0)
- t = 0.0
- x = x0
- v = v0
- Eg = m * g * x[1]
- Ek = 0.5 * m * VLengthSquared(v)
- E = Eg + Ek
- NumBounces = 0
- PrintTime(t)
- PrintState(x, v)
- dt = sqrt(2.0 * x[1] / g)
- t += dt
- x = (x[0] + dt * v[0], 0.0)
- v = (v[0], v[1] - g * dt)
- t1 = t
- while v[1] < 0:
- PrintTime(t)
- PrintState(x, v)
- x, v = Bounce(x, v)
- if v[1] > 0.0:
- NumBounces += 1
- print 'Bounce %d:' % (NumBounces)
- PrintState(x, v)
- #Advance to instant before next bounce
- dt = 2.0 * v[1] / g
- x = (x[0] + dt * v[0], 0.0)
- v = (v[0], -v[1])
- t += dt
- print '%gs later...' % (dt)
- else:
- print 'Splat!'
- PrintState(x, v)
- print
- print 'End of simulation\n'
- print 'Mass of ball: %gkg' % (m)
- print 'g = %gm/s/s (downward)' % (g)
- print 'Initial state:'
- PrintState(x0, v0)
- print 'Duration of initial fall: %gs' % (t1)
- print 'Number of bounces:', NumBounces
- print 'Duration of fall and bouncing: %gs' % (t)
- Main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement