A common pitfall in deriving Hamiltonian from Lagrangian mechanics
It is well known that Hamiltonian mechanics can be obtained from Lagrangian dynamics through Legendre transformation[1], and is also a standard content in most classical mechanics courses. Despite it seems to be just some variable transformation technic, due to the fact that the free and the dependent variables are often mixed up in the expression, it is quite easy to fall on the wrong track. I hope you will find this note helpful.
Lagrangian mechanics
Starting with standard Lagrange’s equations[2] without constraints and external excitations.
Where “L” (Lagrangian) is a function of the generalized position(q), speed(q_dot), and time(t). It can be interpreted as a “cost function” in optimal control problems for control system folks. The special trajectory which leads to an extremum of action “S” will satisfy the Lagrange equation and be denoted as “on shell” or “optimal” depending on the scenario.
Legendre transformation
A short intuitive view of the mechanism of Legendre transformation will be given below, readers may refer to reference[1,3] for more. We will use the symbol ω as a shorthand of q_dot.
With a Lagrange function differentiable and convex on the ω-axis, momentum “p” can be defined through the partial derivative along ω. The relation between the momentum “p” and speed “ω” is invertible if the slope is increasing, more general cases are also possible but let’s keep it simple for now. Such requirement is fulfilled for most mechanical systems where kinematic energy is a quadratic product of ω with a positive inertia matrix (p=Mω, and the inverse relation ω=inv(M)p).
At this point, we can claim the relationship between the Lagrange (L) and generalized speed (ω) is effectively encoded by the upper left area “H” and generalized momentum (p). This prompts the following definition of Hamiltonian (H), note that the transformation between the first and the second equation requires a valid expression of “ω” by “p”.
The “pitfall”
Back to our story where we are trying to derive a new formation of the equation of motion from Lagrange mechanics. The very same Legendre transformation described above can be applied but with a slightly complicated vector notation as shown.
Then the derivation is followed by partially differentiating the Hamiltonian to each of its variables (p, q, t). During the derivation of the second equation which we partially derivative with general position (q), the blue-colored term was replaced by the Lagrange equation. As a result, the first and the third equations were fine but the second was incorrect, what could go wrong?
The fix
It turns out the naïve replacement using the Lagrange equation is what to blame for. Despite the same symbol which makes it seems harmless, they are partially differentiated under different constraint. To be clear, we may rephrase the Lagrange equation and the Hamiltonian in a more detailed form as follow.
It is now clear that they are not the same function and a direct substitution will ruin one’s day.
Since a fixed generalized momentum (p) in the last term of the second equation doesn’t imply a fixed speed (ω), to obtain a correct result, we need to fully expand the dependencies of generalized speed (ω) to position (q),
And this will be the end of this subject.
Total derivative, an alternative approach
In some literature, the derivation follows a total derivative of Hamiltonian and is compared with the transformed Lagrange equation. This approach somewhat avoids the pitfall described in this note by preventing the mixture of free and dependent variables.
Further insight
We have never assigned the actual content of Lagrangian nor Hamiltonian itself, but only the relation between them. Notice a Lagrangian setup is always related to some type of optimal problem with the “instantaneous cost function” being the Lagrange itself, and the optimal trajectory solution for such problem will obey the corresponding Hamiltonian equation of motion. This relation is analogous to the so-called Pontryagm’s Maximum Principle and can be roughly described as follows:
Dynamics system is an optimal solution of a corresponding cost function
and a more engineering point of view:
An optimal solution can be obtained by simulating such “dynamics”
References
[1] R. K. P. Zia, E. F. Redish, en S. R. McKay, “Making sense of the Legendre transform”, American Journal of Physics, vol 77, no 7, bll 614–622, 2009.
[2] L. N. Hand en J. D. Finch, Analytical Mechanics. Cambridge University Press, 1998.
[3] D. Jeltsema and J. M. A. Scherpen, “Multidomain modeling of nonlinear networks and systems,” in IEEE Control Systems Magazine, vol. 29, no. 4, pp. 28–59, Aug. 2009, doi: 10.1109/MCS.2009.932927.
[4] A. T. Fuller, “Bibliography of Pontryagm’s Maximum Principle”, Journal of Electronics and Control, vol 15, no 5, bll 513–517, 1963.