<aside> š Legendre Transformation: Consider a function $f(x)$ (it can also depend on other variables). We define the Legendre transformation in two steps:
After applying the definition of $g(x^)$ it is considered only a function of $x^$ not $x$
</aside>
For a system with one degree of freedom, $q$, and its velocity, $\dot q$, the Lagrangian is $L(q,\dot q,t)$ which means that
$$ \text dL=\frac{\partial L}{\partial q}\text dq+\frac{\partial L}{\partial \dot q}\text d\dot q+\frac{\partial L}{\partial t}\text dt $$
We want to find a function $H(q,p,t)$ where $p=\partial L/\partial \dot q$ which we want to think of as depending only on $q$ and $p$ but not on $\dot q$ this would mean
$$ \text dH=\frac{\partial H}{\partial q}\text dq +\frac{\partial H}{\partial p}\text dp+\frac{\partial H}{\partial t}\text dt $$
Using Legendreās transformation ($p$ is the Legendre conjugate to $\dot q$ and $H$ the Legendre transformation of $L$)
$$ H(q,p,t)=p\dot q-L(q,\dot q,t) $$
$H$ is called the Hamiltonian of the system
We write a general change in $H$ as
$$ \text{d H}=p\,\text d\dot q+\dot q\, \text dp-\text dL $$
Using $\text dL$ we can write
$$ \text{d}H=p\,\text d\dot q+\dot q\, \text dp-\frac{\partial L}{\partial q}\text dq-\frac{\partial L}{\partial \dot q}\text d\dot q-\frac{\partial L}{\partial t}\text dt $$
We can replace the $\partial L/\partial \dot q$ by $p$ so that the first and fourth terms cancel.
In the third term we use Lagrangeās equation
$$ \frac{\partial L}{\partial q}=\frac{\text d}{\text dt}\left ( \frac{\partial L}{\partial \dot q}\right )=\frac{\text d}{\text d t}(p)=\dot p $$
Subbing in we get
$$ \text dH=\dot q\, \text dp-\dot p \,\text dq-\frac{\partial L}{\partial t}\,\text dt $$
If we compare this equation and the current we get the following relations
$$ \begin{aligned} \frac{\partial H}{\partial P}&=\dot q \\ \frac{\partial H}{\partial q}&=-\dot p \\ \frac{\partial H}{\partial t}&=-\frac{\partial L}{\partial t}
\end{aligned} $$
These are Hamiltonās equations
š Conclusion: The general definition for a system with $N$ degrees of freedom $\{q_i\}$ and Lagrangian $L(\{q_i\},\{\dot q_i\},t)$ is
$$ \boxed{H(\{q_i\},\{p_i\},t)=\sum_ip_i\dot q_i-L(\{q_i\},\{\dot q_i\},t) \quad \text{with}\; p_u=\frac{\partial L}{\partial \dot q_i}} $$
With Hamiltonās equations
$$ \boxed{\frac{\partial H}{\partial p_i}=\dot q_i \quad \frac{\partial H}{\partial q_i}=-\dot p_i \quad \frac{\partial H}{\partial t}=-\frac{\partial L}{\partial t}} $$
šļø Note: that in Lagrangeās technique we have $N$ second order differential equation while in Hamiltonās we have $2N$ first order differential equations
š Example: Particle in a conical vase
The Lagrangian was
$$ L=\frac 12 m \dot r^2+\frac 12 m r^2 \sin^2(\theta)\,\dot \phi^2-mgr\cos\theta $$
There are two degrees of freedom $r$ and $\phi$, with corresponding momenta
$$ p_r=\frac{\partial L}{\partial \dot r}=m\dot r $$
The component of linear momentum in the direction up the slope of the vase, and
$$ p_\phi=\frac{\partial L}{\partial \dot \phi}=mr^2 \sin^2 (\theta)\,\dot \phi $$
the angular momentum about the axis of the vase
These can be inverted
$$ \begin{aligned} \dot r&=\frac 1mp_r \\ \dot \phi&=\frac{1}{mr^2 \sin^2 \theta}p_\phi
\end{aligned} $$
The Lagrangian can be rewritten
$$ L=\frac{p^2_r}{2m}+\frac{p^2_\phi}{2mr^2 \sin^2 \theta}-mgr \cos\theta $$
The Hamiltonian is given by
$$ \begin{aligned} H&=\sum_{i=r,\phi}p_i\dot q_i-L=p_r \dot r+p_\phi \dot \phi -L \\ &=\frac{p^2_r}{m}+\frac{p^2_\phi}{mr^2 \sin^2 \theta}-\left ( \frac{p^2_r}{2m}+\frac{p^2_\phi}{2mr^2\sin^2\theta}-mgr\cos\theta\right ) \\ &=\frac{p^2_r}{2m}+\frac{p^2_\phi}{2mr^2\sin^2\theta}+mgr\cos\theta
\end{aligned} $$
šļø Note: in this case it is equal to the total energy of the system