Solution (source code)

= Solution

The <Euler-Lagrange equation> for $\theta$ is
$$
l\ddot\theta+\ddot x\cos\theta+g\sin\theta=0.
$$
For small <angles>, set $\cos\theta\simeq1$ and $\sin\theta\simeq\theta$. With $x=A\cos\omega t$ and $\omega_0^2=g/l$, the <harmonic forcing of a horizontally driven pendulum> obeys
$$
\ddot\theta+\omega_0^2\theta=\frac{A\omega^2}{l}\cos\omega t.
$$
For $\omega\ne\omega_0$, its general solution is
$$
\boxed{\theta(t)=C\cos\omega_0t+D\sin\omega_0t+\frac{A\omega^2}{l(\omega_0^2-\omega^2)}\cos\omega t.}
$$
The constants $C,D$ set the initial <angle> and <angular velocity>. If the bob starts vertical with zero angular <velocity> at $t=0$, this reduces to
$$
\theta(t)=\frac{A\omega^2}{l(\omega_0^2-\omega^2)}\bigl(\cos\omega t-\cos\omega_0t\bigr).
$$
The solution remains an approximation only while the resulting <angle> is small.