Xiaohei's Blog
headpicBlur image

Linear Quadratic Regulator - LQR#

  1. Linear (linear model)

Linear

  1. Quadratic (quadratic): LQR uses properties of quadratic functions to find the minimum point—that is, an energy-optimal point / an energy-optimal control strategy.

Quadratic

  1. Regulator

Regulator

  1. Pros and cons (more suitable for MIMO models)

point

Difference from PID#

PID is output-feedback control; LQR is state-feedback control, which uses multi-dimensional control information. The figure below shows the LQR control block diagram.

frame

Solving the gain matrix K#

First, define a cost function JJ, which measures whether the method is optimal. The magnitudes of Q1Q_1, Q2Q_2, Q3Q_3 (weights) determine the importance of each term in xx.

k

Riccati equation (Riccati): based on the cost function, the goal is to find the energy-optimal solution and thus compute the gain matrix KK (in MATLAB you can call the function directly).

r

What an observer is for#

We need an observer essentially because state-feedback control requires knowing each state variable being fed back. Sometimes some state variables cannot be obtained directly from sensors.

If a state variable cannot be obtained directly from sensors, we use an observer to estimate that unknown state variable. (If we only use mathematical differentiation/integration, it can introduce lots of noise and may also cause phase lag. Some data cannot be obtained from sensors directly, nor obtained simply via differentiation/integration.)

ob

LQR Simulation#

  1. Write the state equations and the state space.

sim1 sim2 sim3

  1. Build the Simulink simulation system
  • First set up the state-space equation model sim4
  • Then add the Kalman observer sim5
  • Complete system structure sim6

Tuning the QR matrices#

By tuning the Q/R matrices we obtain a new gain matrix KK, and then do closed-loop state-feedback control.

note

Example of tuning RR: uu is the control input and tt is time. So when RR increases, the control input uu becomes smaller. As a result, the control effort is smaller than before, which leads to a longer response time and slower response.

note1

How the QR matrices affect the control system: the QR matrices change the convergence speeds of the corresponding state variables (i.e., how fast they approach 0). If you want a state variable to converge faster and reach zero earlier, increase the weight of that state variable. (For example: to make X1X_1 converge faster, increase Q1Q_1; to make X2X_2 converge faster, increase Q2Q_2.)

note2

Tracking the reference input#

  1. Where do we put the reference input?
  • Suppose we apply a unit step input and want the output to track the reference input. (If you follow only the PID flow and then combine with LQR, you’ll find tracking the reference input cannot be achieved.)

  • Add an integrator before the LQR block. If the control system has an error (difference between actual value and target), it will keep accumulating until the error is eliminated. This solves the issue that pure LQR state feedback can make the output unable to track.

note3

  1. Why add output feedback additionally: since LQR itself is state feedback, to track output you need output-feedback control. Introducing an integrator can achieve this.

note4 note5

The C program corresponding to LQR in real hardware#

  • Variable initialization
  • Observer computation
  • Feedback: multiply gain matrix kk by the corresponding state variables
  • Obtain the control input uu

code

LQR Control Algorithm
https://xiaohei-blog.vercel.app/en/blog/lqr
Author 红鼻子小黑
Published at May 19, 2025
Comment seems to stuck. Try to refresh?✨