Energy conservation for changes in the Hamiltionian If the Hamiltonian represents the total energy of the system,
then how does it change?
Does a change in the Hamiltonian violate the conservation of energy?
Of course, we know from the Hamiltonian equation of motion that the derivative of the Hamiltonian with respect to the spatial coordinate is not equal to zero.
Does that violate the conservation of energy?
 A: That the Hamiltonian changes with respect to position does not violate the conservation of energy, because it is only a statement about what energy a system would have if it were in a certain state (point in phase space). In order to know which path in phase space a particle will actually take, one has to solve the equations of motion and the solution obtained will generally globally (for the meaning of this, please read on) conserve energy.
This does not mean, however, that the Hamiltonian does not change over time when plugging in the solutions. If non-conservative forces act on a system, it is entirely possible that energy is added to or dissipated from the system. In this case, indeed energy is not conserved locally within that system. However, the energy difference has to be balanced in a larger or more complex (global) system, meaning that overall the energy is still conserved.
Example:
Consider a rod rotating with constant angular velocity $\omega$ on which a bead of mass $m$ may slide frictionlessly. Let $\theta$ be the angle of the rod and $d$ the bead's position on it. The Lagrangian of the system is
$$
\mathcal L(\theta,d,\dot \theta,\dot d,t) = \frac 12 m \omega^2 d^2 + \frac 12 m \dot d^2 = \frac 12 m \dot \theta^2 d^2 + \frac 12 m \dot d^2~.
$$
For the canonical momenta, there follows
$$
p_\theta = \partial_{\dot \theta} \mathcal L = m \dot \theta d^2~, \qquad p_d = \partial_{\dot d} \mathcal L = m\dot d~.
$$
Consequentially, the Hamiltonian can be written as
$$
H(\theta,p_\theta,d,p_d,t) = \frac{p_\theta^2}{2md^2} + \frac{p_d^2}{2m}~,
$$
so Hamilton's equations read
$$
\dot \theta = \partial_{p_\theta} H = \partial_{p_\theta} \frac{p_\theta^2}{2md^2} = \frac{p_\theta}{md^2}~, \qquad \dot d = \partial_{p_d} H = \frac{p_d}{m}~,
$$
$$
\dot p_\theta = -\partial_\theta H = 0~, \qquad \dot p_d = -\partial_{d} H = - \partial_d \frac{p_\theta^2}{2md^2} = \frac{p_\theta^2}{md^3}~.
$$
Using the first equation, there follwos $\dot \theta m d^2 = \omega m d^2 = p_\theta$, which can be plugged into the last one to obtain
$$
\dot p_d = \frac{\omega^2 m^2 d^4}{md^3} = m \omega^2 d~,
$$
so differentiating the second equation by time yields
$$
\ddot d = \frac{\dot p_d}{m} = \omega^2 d~.
$$
This equation is solved by $d(t) = e^{\omega t}$, meaning that $\ddot d(t) = \omega^2 e^{\omega t}$, so the bead is accelerated exponentially and infinitely away from the point around which the rod rotates. This clearly violates the conservation of energy in the system considerd here, but it was totally neglected by what means the rod is kept rotating. The energy to accelerate the bead has to come from the outside and is added to the system through whatever mechanism keeps the rod going.
Remark:
It was not necessary to show this in order to make my point, but in fact $p_\theta$ is also going to infinity for $t \rightarrow \infty$, so actually both terms of the Hamiltonian are diverging over time.
