Hamiltonian of a bouncing ball Consider a ball free falling from a certain height onto a flat surface. Assuming there is no energy losses, the ball would keep on bouncing and exert a periodic motion.
The problem is to find the period of motion using action-angle variables.
I am struggling to write out the Hamiltonian. I think
$H=\frac{p^2}{2m}+mgy$
is not enough as it doesn't account for the periodic motion. Its phase diagram is not of closed curve or periodic function. What should the Hamiltonian be?
 A: You can add the bouncing behavior using a mirroring method, where we relax the system and instead of bouncing the ball one allows it to cross to y<0 and changes the sign of the potential accordingly. The dynamic is exactly the same with a gain: trajectories become $C^2$. It can be written as
$H=\frac{p^2}{2m}+mg|y|$
where $|y|=y$ if $y>=0$ and $|y|=-y$ if $y<0$. Potential then looks like (for $m,g=1$)

Equation of motion becomes
$\ddot y(t)=-mg\,sgn(y)$,
where $sgn(y)$ is the sign function of y (a Heaviside-theta-like function). To allow equlilibrium at $y=0$ to exist, we can define $sgn(0)\equiv0$.
Writing this in Wolfram Alpha (with $m=g=1$ for simplicity) gives the following graphs related to explicit solution $y(t)$ and phase diagram trajectories (similar to a spring but instead of $sin$/$cos$ functions, its smoothly connected parabolas):

Inserting the Hamiltonian gives a better look at phase space appearance

As for explicit solutions, here's one:
For given initial conditions $y_0>0$, $v_0$, the (downward-facing) initial parabolic trajectory is given by
$y_{init}(t)=y_0+v_0t-\frac g2t^2$.$\,\,\,\,\,\,\,\,\,\,$(1)
It will bounce for the first time when $y_{init}(t)=0$ with $t>0$ and the "last time" it "has bounced" is the other root. The difference between the two roots is the period of bouncing $T$, given by the square root of the Bhaskara formula's $\Delta$:
$T=\sqrt{v_0^2+2g\,y_0}$.
Then, by using a sawtooth function of period $T$
$\tau(t)=t-T \lfloor t/T \rfloor$,
the bouncing trajectory is
$y(t)=y_{init}(\tau(t))$,
with $y_{init}$ given by (1).
For $C^2$ trajectories (and actual solutions to the above hamiltonian) the sign of the function should change every period.
A: You're right that the Hamiltonian you've provided is incomplete, you need to include the fact that there's an impenetrable barrier underneath the ball (i.e., the "floor"). As a result, your potential energy can't be a smooth function, but would rather be something like:
$$V(y) = \begin{cases}mg y \quad &y\geq0 \\\infty\quad &y < 0\end{cases}.$$
The Hamiltonian for the system would then just be $$H = \frac{p^2}{2m} + V(y).$$
In the case of such problems it's usually much more convenient to model the barrier as a constraint rather than as a potential. But nevertheless, the potential will be as shown below (left), and I'll leave it to you to show why the phase diagram looks like the image on the right (ask yourself what happens when the ball hits the floor, and it should be clear).
 
In the diagrams above I've assumed units in which $m=1$, $g=1$, and that the ball is released at a height of $y=2$, so that its total energy is $E=2$ in these units.
To find the "action" variable, you'll need to calculate $$J = \oint p \text{d}y = \int_\text{Going down} \text{(something)} + \int_\text{Going up} \text{(something else)},$$
where I'll leave it to you to find the appropriate limits and integrand.
