# Need help finding Hamiltonian for equations of motion

I have the following equation of motion: $$\ddot \theta+\dot\theta^2\theta+k^2\theta=0.\tag 1$$ This equation is from this question. I wanted to see if I could find a Hamiltonian for this equation but I'm not even sure if it is possible. As an ansatz I postulated that the conjugate momentum is $$p=\dot\theta$$. This gives the following coupled equations: \begin{align} \dot\theta&=p\tag 2\\ \dot p&=-p^2\theta-k^2\theta.\tag 3 \end{align} We expect the Hamiltons equations to hold so \begin{align} \dot\theta=\frac{\partial H}{\partial p},\quad \dot p=-\frac{\partial H}{\partial q}.\tag 4 \end{align} From (2) we get \begin{align} \frac{\partial H}{\partial p}&=p\tag 5\\ H&=\int p\,\mathrm dp+f(\theta)\tag 6\\ H&=\tfrac 1 2p^2+f(\theta).\tag 7 \end{align} From (3) we get \begin{align} -\frac{\partial H}{\partial \theta}&=-p^2\theta-k^2\theta\tag 8\\ H&=\int (p^2\theta +k^2\theta)\,\mathrm d\theta+g(p)\tag 9\\ H&=\tfrac 1 2(p^2+k^2)\theta^2+g(p).\tag{10} \end{align}

Combining these two expressions gives $$H(\theta,p)=\tfrac 1 2p^2+\tfrac 1 2(p^2+k^2)\theta^2.\tag{11}$$ But this expression doesn't give the right equations of motion back; it fails for $$\dot\theta=p+p\theta^2\neq p$$. Where did I go wrong?

1. The ansatz (2) is too optimistic: The generalized velocity $$\dot{\theta}$$ is not necessarily the canonical momentum.