A pendulum with the moving pivot I want to simulate a pendulum whose pivot moves along $x$-direction with some given trajectory $x_p(t)$. The connecting rode is rigid.

I derived the following system of equations:
$$y^2 + (x_p-x)^2 = L^2$$
$$-Mg + N\sin\theta = Mx''$$
$$N\cos\theta = My'',$$
where $M$ is the blob mass, $\theta$ is the angle between the rode and the $y$-axis, $x_p$ pivot position, $x$ and $y$ positions of the blob.
Question: how can I solve this system numerically?
 A: The easiest way to solve this type of problem is using the Lagrangian approach. The Lagrangian is the kinetic energy minus the potential energy: $$ \mathcal{L} = \frac{1}{2}m \left( \dot x^2 +\dot y^2 \right)- m g y $$ Then we can make the substitutions $x \rightarrow L \sin(\theta) + x_p$ and $y \rightarrow -L \cos(\theta)$. Making those substitutions and simplifying gives $$ \mathcal{L} = \frac{1}{2}m \left( \dot x_p^2 + L^2 \dot \theta^2 + 2 L \cos(\theta) \left( g+ \dot x_p \dot \theta \right) \right) $$ Since $x_p$ is a known input, this is a Lagrangian in only one coordinate, $\theta$. So we can immediately write the Euler Lagrange equation for $\theta$ and simplify to obtain $$\ddot \theta = -\frac{1}{R} \left( g \sin(\theta) + \ddot x_p \cos(\theta) \right)$$ With just this single equation you should be able to plug it into pretty much any numerical differential equation solver to get an answer. Note, the equation of motion for $\theta$ depends on $\ddot x_p$, not on $x_p$ or $\dot x_p$. This makes sense because you can always choose a reference frame where $x_p(0)=\dot x_p(0)=0$
