My first python project is getting close to done.

I wanted to start with something quick and easy. Something that would show me the syntax of the language, but not complicated enough to make me spend hours of research trying to figure it out.

What I did was write a script that graphs the height of a bouncing ball over time. What the program really does, is ask the user to input the initial height and COR of the ball, then it just runs through some easy calculations and shows a graph. I’m not quite done though, right now I have it graphing only the first two bounces. What I want to do is write it as a loop and terminate when the height of the ball doesn’t reach higher than a certain percentage of the initial height.

Here’s the code so far:

import numpy as np
import matplotlib.pyplot as plt
import math
#h1 = 10 #input the initial height
h1 = input("Initial Height ")
e = input("Coefficient of Restitution ")
g = 9.806 #gravety at system
#NOTE: Somehow turn into a loop
v1 = math.sqrt(g*h1*2)
#instant velocity before impact, from energy equation
v2 = v1*e #instant velocity after impact,
tlim = math.sqrt(2.0*h1/g) #time of impact, setting h = 0
t = np.linspace(0,tlim,100)#array of time from 0 to impact
h = -1/2.0*g*(t*t)+h1 #h from 0 to impact
t2 = np.linspace(tlim,(v2*2/g+tlim),100)
#array of time from 1st impact to 2nd
h2 = -(1/2.0)*g*((t2-tlim)*(t2-tlim))+v2*(t2-tlim)
#equation for h from 1st impact to 2nd
plt.ion
plt.plot(t,h)
plt.plot(t2,h2)
plt.show()

And here’s the output:

>>>
Initial Height = 10
Coefficient of Restitution = .8

For a bit of fun, I set gravity as a changeable parameter. So if you wanted to see how the ball would bounce in microgravity, you could set the value of g to .000001 (1e-6) and see what happens.

### Like this:

Like Loading...

*Related*