code.nb Mathematica notebook.

## Using LQR to stabilize an Inverted pendulum

October 13, 2023   Compiled on October 13, 2023 at 5:09am

### 1 Introduction

This is an analysis of the dynamics of inverted bob pendulum on a moving cart. The equations of motion for the cart and the pendulum bob mass are derived using Lagrangian formulation, then state space model is derived and then LQR is used to ﬁnd the state gain vector to bring the pendulum to the upright position from an initial position.

The analysis uses Mathematica. Viscous friction is assumed to be present. This is friction between the cart itself and the rail that the cart moves on.

### 2 Derivation of the equations of motion

Let $$\nu$$ be the viscous friction coeﬃcient. The potential energy of the system is \begin {align*} \text {PE} &= m g L \sin \theta \end {align*}

And the kinetic energy is \begin {align*} \text {KE} &= \frac {1}{2} M \dot {x}^2 + \frac {1}{2} m\left ( \left ( \dot {x} - L \dot {\theta } \sin \theta \right )^2 + \left ( L \dot {\theta } \cos \theta \right )^2 \right ) \end {align*}

The Lagrangian is now found and equations of motion are found for the bob and for the cart as follows

The state space representation is found using

Now optimal state feedback gain is found using