One (and the simplest) way of getting to equations of motion in Hamiltonian mechanics is through calculus of variation. It is illuminating enough to consider 1-D senario at the beggining, since the higher dimensional case merely needs adding summation. First, we know that by definition:
where is generalized coordinates and is the corresponding generalized momentum. Of course, is our old friend Lagrangian .
Then, if we consider imposing variation on and so that, if we denote the true path of them as:
We can impose also a variation on them:
Here, we can thing of 's as the difference between true path and altered path at any given time. If our system begins at and ends at , we naturally have:
Because the two ends are known to us. Now, by definition of action, we see that the variation of action is:
Now we want to move the partial derivative into the integration because the variation we impose is not related to time, also bear in mind that the partial derivative would hit term and we will be using chain rule here:
Then if we apply inverse by parts to the second term in the bracket, we see that:
The term evaluated to zero because we know the ends have no variation.
Plug this result back into the original integration we see that:
If we re-group terms with the same partial derivative together, and argue that since those derivative are not necessariley zero, we must have:
Which is what we wanted.