An inverted pendulum is not stable. Nevertheless, as pointed out by Pyotr Kapitza (1894--1984) in 1951, if you oscillate vertically the suspension point at a sufficientlyfast frequency Ω, you can stabilise it. A similar phenomenon occurs in arotating saddle (see video), and is also used to create radio-frequencyionic traps.

Let us start considering a very familiar one-dimensional system: a planar pendulum made of a massless rod of length ℓ ending with a point mass m. In the familiar swing, the driving occurs in different ways: if you “drive” the swing yourself, you do it by effectively modifyingthe position of your “center-of-mass”, hence the effective length ℓ(t) of the “pendulum”. If you are pushed by someone else, then you have a pendulum with a “periodic external force”. We use the generalize coordinate q = θ that denotes the angle formed with the vertical (θ = 0 being the downward position), and y0(t) denotes the position of its suspension point, we can derive the equations of motion from the Lagrangian formalism. In a short while we will assume that y0(t) = Acos Ωt, where A is the amplitude of the driving and Ω the driving frequency, but for the time being, let us proceed by keeping y0(t to be general. In a system of reference with the y-axis oriented upwards and the x-axis horizontally, the position x(t) and y(t) of the massm is:

\[ \begin{cases} x(t) &= \ell \,\sin\theta (t) , \\ y(t) &= y_0 (t) - \ell\,\cos \theta (t) , \end{cases} \qquad \Longrightarrow \qquad \begin{cases} \dot{x}(t) &= \ell \dot{\theta} \cos\theta , \\ \dot{y}(t) &= \dot{y}_0 + \ell \dot{\theta} \sin\theta . \end{cases} \]
The Lagrangean is given by
\begin{align*} {\cal L}\left( \theta , \dot{\theta} , t \right) &= \frac{m}{2} \left( \dot{x}^2 + \dot{y}^2 \right) - mgy \\ &= \frac{m}{2} \,\ell^2 \dot{\theta}^2 + m\ell \dot{y}_0 \dot{\theta} \sin\theta + mg\ell\,\cos\theta , \end{align*}
where we dropped the term \( \frac{m}{2}\,\dot{y}_0^2 - mgy_0 \) because it would not enter in the Euler--Lagrange equations due to pure dependence on time t. The associated momentum is given by:
\[ p_{\theta} = \frac{\partial {\cal L}}{\partial \theta} = m\ell^2 \dot{\theta} + m\ell \dot{y}_0 \sin\theta \qquad \Longrightarrow \qquad \dot{\theta} = \frac{p_{\theta}}{m\ell^2} - \frac{\dot{y}_0}{\ell} \,\sin\theta . \]
The Hamilton’s equationsread:
\[ \begin{cases} \dot{\theta} &= \frac{\partial H}{\partial p_{\theta}} = \frac{p_{\theta}}{m\ell^2} , \\ \dot{p}_{\theta} &= - \frac{\partial H}{\partial \theta} = -m \left[ g - A \Omega^2 \cos\Omega t \right] \sin \theta . \end{cases} \]
Hence, transforming it into a second-order equation:
\begin{equation} \label{EqI.1} \frac{{\text d}^2 \theta}{{\text d} t^2} + \frac{1}{\ell} \left[ g - A\Omega^2 \cos (\Omega t) \right] \sin \theta = 0 , \end{equation}
where \( \omega_0^2 = g/\ell \) is the square of the frequency of the unperturbed pendulum in the linear regime, Ω is the driving frequency, A is the amplitude of the driving, which we model as y0(t) = Acos(Ωt).

Example 1: If a rod or a pencil is held upright on a table, and then released, it will fall onto the ground. We consider a simple case when a rigid rod of length 2ℓ is fixed to a horizontal table by a smooth hinge at one end while another end is free to fall.

      Falling pencil
rod = Graphics[{LightGray, Polygon[{{0.1, 0}, {0, 0.1}, {2.0, 2.1}, {2.1, 2.0}}]}];
ar = Graphics[{Black, Thickness[0.01], Arrowheads[0.08], Arrow[{{-0.2, 0}, {2.5, 0}}]}];
ar2 = Graphics[{Black, Dashed, Thickness[0.01], Arrowheads[0.08], Arrow[{{0, -0.2}, {0, 2.2}}]}];
ar3 = Graphics[{Black, Dashed, Thickness[0.01], Arrowheads[0.08], Arrow[{{1, 1}, {1, 0.4}}]}];
tl = Graphics[{Black, Text[Style["\[ScriptL]", 18, FontFamily -> "Mathematica1"], {1.5, 1.75}]}];
tx = Graphics[{Black, Text[Style["x", 18], {2.4, 0.2}]}];
t1 = Graphics[{Black, Text[Style["C", 18], {1.0, 1.2}]}];
t2 = Graphics[{Black, Text[Style["\[Theta]", 18], {0.15, 0.4}]}];
t3 = Graphics[{Black, Text[Style["mg", 18], {1.05, 0.3}]}];
Show[rod, ar, ar2, ar3, tx, tl, t1, t2, t3]
       Falling pencil.            Mathematica code

Since the bottom end is hinged, there is no horizontal move of it. The kinetic energy, K, consiosts of the kinetic energy of the motion of the center of mass plus the kinetic energy of rotation about theh bottom end of the pencil:
\[ \mbox{K} = \frac{m}{2}\,v_{cm}^2 + \frac{1}{2}\, I_0 \dot{\theta}^2 \]
Inverted pendulum, plotted with Chebfun.
   ■