About Hamiltonian equation for a charged particle in a magnetic field I am learning physics by watching Prof. Leonard Susskind's lectures on Youtube. I met a problem when working on the homework posted at the last part of lecture 7. 
Here is the description:
The Lagrangian of a charged particle moving in a magnetic field is expressed as:
$$L =\sum_{x,y,z} \frac{1}{2}mv_i^2 + qv_iA_i$$
In deriving Euler-Lagrangian Equation, we calculate for the x component:
$$ \frac {\partial L}{\partial x} = qv_x \frac {\partial A_x}{\partial x}$$
where $\frac {\partial \sum \frac {1}{2} mv_i^2}{\partial x}$ is completely ignored since $v_i$ is independent of x .
From the  Euler-Lagrangian Equation, we found that:
$$ p_x = mv_x + qA_x$$
Then the Hamiltonian can be expressed as either:
$$ H = \sum_{x,y,z} \frac{1}{2}mv_i^2 $$ 
or
$$ H =\sum_{x,y,z} \frac {(p_i-qA_i)^2}{2m} $$
The question is that when using the second expression to calculate $ - \frac {\partial H}{\partial x} $ we can get the correct $ \frac {d p_x}{d t}$. However, when using the first expression, we get:
$$ - \frac {\partial H}{\partial x} = - \frac {\partial \sum \frac {1}{2} mv_i}{\partial x} = 0 $$
since $ v_i $ is independent of x. Then $ \frac {d p_x}{d t} = 0$ ?
 A: What's going on can be seen very easily in the Legendre transformation approach. The Hamiltonian is related to the Lagrangian by (in one dimension)
$$H = v\frac{\partial L}{\partial v} - L,$$
and so taking the differentials of this expression gives us
$$dH = vd\left(\frac{\partial L}{\partial v}\right) + \frac{\partial L}{\partial v}dv - dL,$$
and by the chain rule $dL$ is
$$dL = \frac{\partial L}{\partial v}dv + \frac{\partial L}{\partial x}dx.$$
All together, this gives us
$$dH = vd\left(\frac{\partial L}{\partial v}\right) - \frac{\partial L}{\partial x}dx.$$
And since the canonical momentum is defined as $p = \frac{\partial L}{\partial v}$, we have
$$dH = vdp - \frac{\partial L}{\partial x}dx.$$
What this means is that when you use the Hamiltonian, you are changing your variables from $(x,v)$ to $(x,p)$. So you cannot write a Hamiltonian with respect to $v$, it should always be written with respect to $p$.
In the Lagrangian formulation, $v$ is treated as depending only on time, whereas in the Hamiltonian formulation, $v$ is a function of more than just time. This change means that you need to know the form of $v$ in order to take its derivatives, and its form is given by the canonical momentum expression.
A: I believe your issue comes from the fact that you wrote the Hamiltonian in terms of velocity (first expression), when it should always be written in terms of generalized positions and momenta (second expression): i.e. $H = H(q_k, p_k, t)$ versus $L = L(q_k, \dot{q_k}, t)$. If you don't follow that rule, you are going to run into some nonsensical results like the one you derived, which is seemingly mathematically rigorous.
Every expression you apply to the Hamiltonian, e.g. $\dot{p_k} = -\frac{\partial H}{\partial q_k}$ only works if you have it expressed in terms of the right quantities, i.e. momentum and position.
A: Both Hamiltonians are equivalent and both give the same equations of motion.
Indeed, using the first Hamilton equation we obtain the relation between velocity $v_x = (dx/dt)$ and momentum
$$v_x = \frac{\partial H}{\partial p_x} = \frac {p_x-qA_x}{m}$$
which implies $v_x = v_x(p_x,x)$ and it is the reason why the Hamiltonian can be written in alternative forms
$$ H = \sum_{x,y,z} \frac{1}{2}mv_i^2 =\sum_{x,y,z} \frac {(p_i-qA_i)^2}{2m} $$
The second Hamilton equation can be obtained from either expressions for the Hamiltonian
$$ \frac {d p_x}{d t} = - \frac {\partial H}{\partial x} = - \frac {\partial \sum \frac {1}{2} mv_i^2}{\partial x} = - m v_x \frac {\partial v_x}{\partial x} = q v_x \frac {\partial A_x}{\partial x} \neq 0 $$
$$ \frac {d p_x}{d t} = - \frac {\partial H}{\partial x} = - \frac{1}{2m} \frac {\partial \sum (p_i-qA_i)^2}{\partial x} = q \left(\frac {p_x-qA_x}{m} \right) \frac {\partial A_x}{\partial x} \neq 0 $$
A: Consider a similar yet much simpler math problem:
H is a function defined over a two dimensional space x,y
$$ H = x^2 + 0y = x^2 \ \ \ (1)$$
Obviously, $\frac{\partial H}{\partial y}=0 $.
If we transform the coordinates by the matrix $\begin{bmatrix}1 & 1 \\ 0 & 1\end{bmatrix}$,
then (x,y) --> (x+y, y).
Use p = x+y denotes the new coordinates as (p,y), then H is expressed as
$$H = (p-y)^2$$
and $\frac{\partial H}{\partial y}\ne0$.
The take home message is that coordinate transformation does affect the partial derivatives of even the same function to the same variable.
