# Making a bouncing ball simulation interactive [closed]

Problem

A ball initially at height $h_0$ above a horizontal surface is dropped at time $t = 0$ with zero initial velocity. When it arrives at the surface, the ball bounces in the vertical direction with a fractional loss of speed at each bounce, such that $v_{after}/v_{before} = \alpha$, where $\alpha$ is a constant.

Plot the height of the ball as a function of time.

I know how to do it by hand, but I want to try plotting it using Mathematica. The following is my approach:

g = 9.81;
h[h0_, v_, t_] := h0 + v*t - 0.5*g*t^2

time = Array[t, 5, 0];
velocity = Array[v, 5, 0];

alpha = 0.8; (*arbitary*)
h0 = 10; (*arbitary*)

v[0] = Sqrt[2*g*h0]; (* velocity before first impact *)
For[i = 1, i < 5, i++,
v[i] = v[i - 1]*alpha] (* v[i]=velocity after i^th impact *)

t[0] = 0; (*start time*)
t[1] = t /. Solve[h[h0, 0, t] == 0 && t > 0, t][[1]]; (* time of 1st impact *)

For[i = 2, i < 5, i++,
t[i] = t[i - 1] + t /. Solve[h[0, v[i - 1], t] == 0 && t > 0, t][[1]]]
(* calculate all subsequent t[i], t[i]=time of i^th impact *)

plot = Array[p, 5, 0];
p[0] = Plot[h[h0, 0, s], {s, t[0], t[1]}];
For[i = 1, i < 5, i++, p[i] = Plot[h[0, v[i], s - t[i]], {s, t[i], t[i + 1]}]]

Show[p[0], p[1], p[2], p[3], PlotRange -> All, ImageSize -> 350]


The above gives the plot that I am looking for; however, I have some questions.

• Are there any ways to make the above code more elegant and efficient?
• I want to make $\alpha$ and $h_0$ variables instead of fixing them in the beginning. How should I do it so they can be controls in a Manipulate expression?
• How can I automate the last line so that I don't have to manually type p[0], p[1], p[2]?
• How I can as make 'envelope' as I like on the plot?

PS: I am new to Mathematica, so please forgive me if I ask anything silly.

• I don't understand why you have an argument v and a v t term in the definition of h. That produces an upward retardation proportional to v. Very strange. Commented Jul 18, 2016 at 14:53
• Commented Jul 18, 2016 at 14:58
• Commented Jul 18, 2016 at 15:01
• I recommend you look at this documentation article. It gives a solution that is much more elegant than yours. Commented Jul 18, 2016 at 15:06

A Copy-Paste of (or a shameless plagiarism to mark the question answered) link suggested by m_goldberg

http://reference.wolfram.com/language/example/SimulateABouncingBall.html

h = 20;
a = 0.7;
t0 = 10; (*time of flight*)
ball[t_] = y[t] /. NDSolve[{y''[t] == -9.81, y[0] == h, y'[0] == 0,
WhenEvent[y[t] == 0, y'[t] -> -a y'[t]]}, y, {t, 0, t0}][[1]];

Animate[Graphics3D[Sphere[{0, 0, ball[t]}, 0.5],
PlotRange -> {{-1, 1}, {-1, 1}, {-0.5, h + 0.5}}], {t, 0, t0, 0.5}]


Moving under ground

There is one problem with this is when you go close to the ground you may have a sudden flip which comes from the flipping of the velocity in this example it happens at t= 11.442558634226998. So the result for t0>11.44... is not valid anymore. To get rid of this just add a small positive velocity when it is flipping back.

t0=15;
ball[t_] = y[t] /. NDSolve[{y''[t] == -9.81, y[0] == h, y'[0] == 0,
WhenEvent[y[t] == 0, y'[t] -> If[Abs[y'[t]] > 0.0001, -a y'[t], 0.0001]]},
y[t], {t, 0, t0}][[1]]

Plot[ball[t], {t, 0, t0}]


This will make it stay on the ground.

• Trouble with such Animate is the endless looping - which is hardly simulating how a real ball would be bouncing, ultimately causes it to a stop at the bottom. It's as if a mysterious force periodically and suddenly propels the ball back up to the top. Commented Jul 19, 2016 at 15:09