Hamiltonian from Lagrangian $L= \frac{m}{2}(\dot{r}^2+r^2\dot{\theta^2})+ \frac{k\cos(\theta)}{r^2}$

I'm doing the first exercises with the Lagrangians and Hamiltonians.

Let:

$$L= \frac{m}{2}(\dot{r}^2+r^2\dot{\theta^2})+ \frac{k\cos(\theta)}{r^2}$$ $$p_1=m\dot{r}$$ $$p_2=mr^2\dot{\theta}$$ $$H=\frac{p^2}{2m}-\frac{k\cos(\theta)}{r^2}$$ I do not understand why the Hamiltonian is: $$H=\frac{1}{2m}\left(p_1^2+\frac{p_2^2}{r^2}\right)-\frac{k\cos(\theta)}{r^2}$$ and not $$H=\frac{1}{2m}(p_1^2+ p_2^2)-\frac{k\cos(\theta)}{r^2}$$

The first term in the Lagrangian $L = T-V$ is the kinetic energy $T$, whereas the second term is minus the potential energy $V$. The corresponding Hamiltonian is $H = T+V$, i.e. \begin{aligned} H &= \frac{m}{2} \left({\dot r}^2 + r^2{\dot \theta}^2\right) - \frac{k\cos\theta}{r^2} \\ & = \frac{1}{2m} \left((m {\dot r})^2 + (m r {\dot \theta})^2\right) -\frac{k\cos\theta}{r^2} \\ & = \frac{1}{2m} \left({p_1}^2 + \frac{{p_2}^2}{r^2}\right) - \frac{k\cos\theta}{r^2} \, . \end{aligned} You may have made an algebra mistake replacing $(m r {\dot \theta})^2$ by ${p_2}^2$, which is wrong.

The answer given by Harry49 is correct but I think that you should also understand the more general approach to the problem. It is not always true that the Hamiltonian $H$ is equal to $T+V$; there are some features that the Lagrangian must obey and one should be careful before stating this equality. In the case above this is a true statement but in general what one will have is that

$$H := \sum_i\dot{q}_i(q_i,p_i,t)p^i - L(q_i,\dot{q}_i(q_i,p_i,t),t)\tag{1}$$

$$p_i := \frac{\partial L}{\partial \dot{q}_i}$$

So, we have the generalized coordinates $r$ and $\theta$ such that there will be generalized momentum for each

$$p_r := \frac{\partial L}{\partial \dot{r}} = m\dot{r}$$ $$p_{\theta} := \frac{\partial L}{\partial \dot{\theta}} = mr^2\dot{\theta}$$

Then we note that there are bijections between the generalized velocities and the generalized momentums, in such a way that we can wright $\dot{r} = \dot{r}(p_r)$ and $\dot{\theta} = \dot\theta(p_\theta, r)$. We then set that in $(1)$, in order to get

$$H = \frac{p_r^2}{m}+\frac{p_\theta^2}{mr^2} - \left( \frac{m}{2}\left(\frac{p_r^2}{m^2}+r^2\frac{p_\theta^2}{m^2r^4}\right)+ \frac{k\cos(\theta)}{r^2} \right)$$

$$H = \underbrace{\frac{p_r^2}{m}-\frac{p_r^2}{2m}}_{p_r²/2m} + \underbrace{\frac{p_\theta ^2}{mr^2} - \frac{p_\theta^2}{2mr^2}}_{p_\theta^2/2mr²} - \frac{k\cos(\theta)}{r^2}$$

$$H = \frac{p_r^2 }{2m}+\frac{p_\theta^2}{2mr^2} - \frac{k\cos(\theta)}{r^2}$$

So we get the $r^2$ below. There are reasons for that, when you go to generalized coordinates that are polar coordinates in order to simplify your treatment you need to maintain the dimensions of the generalized velocities, but because the $\dot\theta$ has dimensions of $s^{-1}$ you get the $r$ in front. There are also geometric reasons related to it. The fact that you get a $r^2$ below is just a consequence of changing to Hamilton mechanics but maintaining all these constrains and geometrical/physical knowledge of the movement.

The first part is indeed related to kinetic energy. This kinetic energy has a radial component and a polar component.