Introduction
This post presents the model of an inverted pendulum. This device is composed of an activated trolley and a pendulum which has its center of mass above its pivot point.
In this post, we will assume the following hypotheses:

 is the mass of the trolley.

 is the mass of the pendulum.

 is the moment of inertia of the trolley.

 is the moment of inertia of the pendulum.

 is the distance between the pivot and the center of mass of the pendulum.

 is the angle of inclination of the pendulum.

 and are the coordinates of the center of mass of the trolley .

 is the orientations of the trolley.

 and are the coordinates of the center of mass of the pendulum.

 is the orientations of the pendulum.
The following notation will be used in this article to avoid too complex equations for the derivatives:
And for the second derivative:
Constraints
As the trolley and the pendulum are linked by a pivot, the following equations can be written. This equations expresses the coordinate and orientation of each body of our system. For the trolley (body 1):
$!
\begin{array}{r c l}
x_1 &=& x_1 \\
y_1 &=& 0 \\
\Theta_1 &=& 0 \\
\end{array}
$
For the pendulum (body 2):
$!
\begin{array}{r c l}
x_2 &=& x_1  L.sin(\Theta)\\
y_2 &=& y_1 + L.cos(\Theta) &=& L.cos(\Theta)\\
\Theta_2 &=& \Theta
\end{array}
$
Let’s define the vector
, the position of our system :
Let’s define the constraints on position:
$! \phi = \begin{pmatrix} y_1 \\ \Theta_1 \\ x_2 \\ y_2 \end{pmatrix} = \begin{pmatrix} 0 \\ 0 \\ x_1  L.sin(\Theta) \\ L.cos(\Theta) \end{pmatrix} $
Let’s calculate the constraints on velocity:
$! \dot{\phi} =
\begin{pmatrix} \dot{y_1} \\ \dot{\Theta_1} \\ \dot{x_2} \\ \dot{y_2} \end{pmatrix}
= \begin{pmatrix} 0 \\ 0 \\ \dot{x_1}  L.\dot{\Theta_2}.cos(\Theta_2) \\ L.\dot{\Theta_2}.sin(\Theta_2) \end{pmatrix} $
Let’s calculate the constraints on acceleration:
$! \ddot{\phi} =
\begin{pmatrix} \dot{y_1} \\ \ddot{\Theta_1} \\ \ddot{x_2} \\ \ddot{y_2} \end{pmatrix}
= \begin{pmatrix} 0 \\ 0 \\ \ddot{x_1}  L.\ddot{\Theta_2}.cos(\Theta_2) + L.\dot{\Theta_2}^2.sin(\Theta_2) \\ L.\ddot{\Theta_2}.sin(\Theta_2)  L.\dot{\Theta_2}^2.cos(\Theta_2) \end{pmatrix} $
Dynamics
$!
\begin{array}{r c l}
m_1.{a_1}^x = m_1.\ddot{x_1}&=& \ \vec{F_t} \ + \lambda_1 \\
m_1.{a_1}^y = m_1.\ddot{y_1}&=& 0 + \lambda_2 \\
I_1.\ddot{\Theta_1} &=& 0 + \lambda_3 \\
\end{array}
$
For the pendulum (body 2):
$!
\begin{array}{r c l}
m_2.{a_2}^x = m_2.\ddot{x_2} &=& 0 + \lambda_4 \\
m_2.{a_2}^y = m_2.\ddot{y_2} &=& \ \vec{F_g} \ + \lambda_5 = g.m_2 + \lambda_5\\
I_2.\ddot{\Theta_2} &=& 0 + \lambda_6 \\
\end{array}
$
Where
,
,
,
,
and
are the Lagrange’s coefficients. These coefficients represent the interaction between bodies. In the present system, this is the interaction between the pendulum and the trolley.
Interactions
We will now solve the system in order to eliminate the interactions between bodies (
). It is proven that:
Where :
$! M=\begin{pmatrix} m_1 & 0 \\ 0 & I_2 \end{pmatrix} $
$! \ddot{q}=\begin{pmatrix} \ddot{x_1} \\ \ddot{\Theta_2} \end{pmatrix} $
$! F=\begin{pmatrix} \ \vec{F_t} \ \\ 0 \end{pmatrix} $
$! \frac{d\phi}{dq} = \begin{pmatrix}
\frac{dy_1}{dx1} & \frac{dy_1}{d\Theta_2} \\
\frac{d\Theta_1}{dx1} & \frac{d\Theta_1}{d\Theta_2} \\
\frac{dx_2}{dx1} & \frac{dx_2}{d\Theta_2} \\
\frac{dy_2}{dx1} & \frac{dy_2}{d\Theta_2}
\end{pmatrix}
=
\begin{pmatrix}
0 & 0 \\
0 & 0 \\
1 & L.cos(\Theta_2) \\
0 & L.sin(\Theta_2)
\end{pmatrix}
$
$! \Lambda=\begin{pmatrix} \lambda2 \\ \lambda3 \\ \lambda4 \\ \lambda5 \end{pmatrix} $
Applied to our system, this gives:
$!
\begin{pmatrix} m_1 & 0 \\ 0 & I_2 \end{pmatrix} .
\begin{pmatrix} \ddot{x_1} \\ \ddot{\Theta_2} \end{pmatrix} =
\begin{pmatrix} \ \vec{F_t} \ \\ 0 \end{pmatrix} 
\begin{pmatrix} 0 & 0 & 1 & 0 \\ 0 & 0 & L.cos(\Theta_2) & L.sin(\Theta_2) \end{pmatrix} .
\begin{pmatrix} \lambda2 \\ \lambda3 \\ \lambda4 \\ \lambda5 \end{pmatrix}
$
and:
$!
\begin{array}{r c l}
m_1.\ddot{x_1} &=& \ \vec{F_t} \ \lambda4 \\
I_2.\ddot{\Theta_2} &=& \lambda4.L.cos(\Theta_2) + \lambda5.L.sin(\Theta_2)
\end{array}
$
Solving the system
Based on the previous equations, we can formulate
and
:
$!
\begin{array}{r c l}
\lambda_4 &=& m_2.\ddot{x_2} \\
\lambda_5 &=& m_2.\ddot{y_2} + g.m_2 \\
\end{array}
$
The previous equations become:
$!
\begin{array}{r c l}
m_1.\ddot{x_1} &=& \ \vec{F_t} \ m_2.\ddot{x_2} \\
I_2.\ddot{\Theta_2} &=& m_2.\ddot{x_2}.L.cos(\Theta_2) + (m_2.\ddot{y_2} + g.m_2).L.sin(\Theta_2)
\end{array}
$
Thanks to the previous constraints on acceleration:
$!
\begin{array}{r c l}
m_1.\ddot{x_1} &=& \ \vec{F_t} \ m_2.(\ddot{x_1}  L.\ddot{\Theta_2}.cos(\Theta_2)  L.\dot{\Theta_2}^2.sin(\Theta_2) ) \\
I_2.\ddot{\Theta_2} &=& m_2.(\ddot{x_1}  L.\ddot{\Theta_2}.cos(\Theta_2) + L.\dot{\Theta_2}^2.sin(\Theta_2) ).L.cos(\Theta_2) + (m_2.(L.\ddot{\Theta_2}.sin(\Theta_2)  L.\dot{\Theta_2}^2.cos(\Theta_2)) + g.m_2).L.sin(\Theta_2)
\end{array}
$
Rewriting the previous equation gives us:
$!
\begin{array}{r c l}
(m_1+m_2).\ddot{x_1} && m_2.L.cos(\Theta_2).\ddot{\Theta_2} &=& \ \vec{F_t} \  m_2.L.\dot{\Theta_2}^2.sin(\Theta_2) \\
m_2.L.cos(\Theta_2).\ddot{x_1} &+& (I_2+ m_2.L^2).\ddot{\Theta_2} &=& m_2.g.L.sin(\Theta_2)
\end{array}
$
And:
$!
\begin{pmatrix} m_1+m_2 &  m_2.L.cos(\Theta_2) \\
m_2.L.cos(\Theta_2) & (I_2+ m_2.L^2)
\end{pmatrix} . \ddot{q} =
\begin{pmatrix} \ \vec{F_t} \  m_2.L.\dot{\Theta_2}^2.sin(\Theta_2) \\
m_2.g.L.sin(\Theta_2)
\end{pmatrix}
$
The general equation of the system is:
$!
\ddot{q}=A^{1}.B
$
Where:
$!
A=\begin{pmatrix} m_1+m_2 &  m_2.L.cos(\Theta_2) \\
m_2.L.cos(\Theta_2) & (I_2+ m_2.L^2)
\end{pmatrix}
$
$!
B= \begin{pmatrix} \ \vec{F_t} \  m_2.L.\dot{\Theta_2}^2.sin(\Theta_2) \\
m_2.g.L.sin(\Theta_2)
\end{pmatrix}
$
State space representation
Based on the previous result, it is now possible to write the state space model of the system. Let’s define
the state of the system:
$!
X=\begin{pmatrix} x_1 \\ \Theta_2 \\ \dot{x_1} \\ \dot{\Theta_2} \end{pmatrix}
$
The state space representation is:
$!
\dot{X}=\begin{pmatrix} \dot{x_1} \\ \dot{\Theta_2} \\ \left[A^{1}.B\right] \end{pmatrix}
$
Acknowledgements
I want to thank Sébastien Lagrange from the University of Angers for his help and explanations.