# How does the Hamiltonian change if $L\to L + \frac{dF}{dt}$? [duplicate]

The Hamiltonian is defined as the Legendre transform of the Lagrangian $$H = p\dot{q} -L .$$ In the Lagrangian formalism we are free to add the total derivative of an arbitrary function $$F=F(q,t)$$ to the Lagrangian $$L \to L' = L + \frac{dF}{dt}$$ because such an additional term has no effect on the Euler-Lagrange equation.

For the momentum $$p = \frac{\partial L}{\partial \dot{q}}$$ such a shift of the Lagrangian implies $$p \to p'= p + \frac{\partial F}{\partial q} .$$

If we combine these two observations, we find the following transformation law for the Hamiltonian \begin{align} H(q,p) \to H' &= p' \dot q' - L' \\ &= \left( p + \frac{\partial F}{\partial q} \right) \dot q - \Big( L + \frac{dF}{dt} \Big) \\ &= H + \frac{\partial F}{\partial q} \dot q - \frac{dF}{dt} \end{align} Is this transformation law correct or am I missing something important? (In the literature I've only found several times the statement that the Hamiltonian like the Lagrangian is allowed at most to change be the total derivative of some function. But here it seem that there is an additional term...)

The transformation law given above is correct because by noting that $$\frac{d}{dt} F(q,t) = \frac{\partial F}{\partial q} \frac{d q}{d t} + \frac{\partial F}{\partial t}$$ we find \begin{align} H(q,p) \to H' &= H + \frac{\partial F}{\partial q} \dot q - \frac{dF}{dt} \\ &= H+ \frac{\partial F}{\partial q} \dot q - \Big( \frac{\partial F}{\partial q} \frac{d q}{dt} + \frac{\partial F}{\partial t} \Big)\\ &= H - \frac{\partial F}{\partial t} \, . \end{align}