@MatinSAR, I'm jumping into the thread a bit late in the day, but would like to add this…
In your post #21, I agree with your EL equation for ##\theta##:
##-mgl \sin \theta -ml\ddot x \cos \theta - ml^2 \ddot \theta=0##
(but would drop the minus signs).
But you may have made a mistake with the EL equation for ##x##.
Using your expression for ##L## (which I agree with), and at risk of being accused of doing too much:
##L=\dfrac {M+m}{2}\dot x^2+ml\dot x \dot \theta \cos \theta + \dfrac 1 2 m l^2 \dot \theta^2 +mgl\cos \theta##
##\dfrac {\partial L} {\partial x}=0##
##\dfrac {\partial L} {\partial \dot x}= (M+m)\dot x +ml\dot \theta \cos \theta##
##\dfrac {d}{dt} \dfrac {\partial L}{\partial \dot x}=(M+m)\ddot x
+ml(\ddot \theta \cos \theta - \dot \theta^2 \sin \theta)##
(Slightly fiddly; note you need to use both product and chain rules to differentiate the ##\dot \theta \cos \theta## term.)
##\dfrac {d}{dt} \dfrac {\partial L}{\partial \dot x} - \dfrac {\partial L} {\partial x} =0 \implies (M+m)\ddot x +ml(\ddot \theta \cos \theta - \dot \theta^2 \sin \theta) = 0##
Then, for ##\theta \ll 1##, you can use ##\sin \theta \approx \theta## to get rid of ##\sin##. It may be OK to use ##\cos \theta \approx 1## rather than ##\cos \theta \approx 1 - \frac{\theta^2}2## but that could be contentious.