code.nb Mathematica notebook.

## Using LQR to stabilize an Inverted pendulum

January 30, 2024   Compiled on January 30, 2024 at 6:53am

### 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

The response of the system using this state feedback gain from an initial position is plotted using

Finally, this shows system coming to equilibrium

We see from above that both the x position of the cart and the upright pendulum position have been brought back to the equilibrium position.