LQR Controller (Linear Quadratic Regulator)

March 21, 2026 · View on GitHub

Overview & Motivation

When a state-space model of the plant is available, the Linear Quadratic Regulator provides the mathematically optimal state-feedback gain. Instead of manually tuning three knobs (as with PID), LQR asks a more principled question: given how much I care about state deviation versus control effort, what is the cheapest way to drive the state to zero?

The answer is a constant gain matrix KK such that the control law u[k]=Kx[k]u[k] = -Kx[k] minimizes an infinite-horizon quadratic cost. The elegance of LQR is that this problem has a closed-form solution through the Discrete Algebraic Riccati Equation (DARE).

For embedded systems, the gain KK can be computed offline and stored as a constant — the runtime cost is then a single matrix-vector multiplication per control step.

Mathematical Theory

Discrete-Time State-Space System

x[k+1]=Ax[k]+Bu[k]x[k+1] = Ax[k] + Bu[k]

where xRnx \in \mathbb{R}^n is the state, uRmu \in \mathbb{R}^m is the control input, AA is the state transition matrix, and BB is the input matrix.

Cost Function

The LQR minimizes the infinite-horizon quadratic cost:

J=k=0(x[k]TQx[k]+u[k]TRu[k])J = \sum_{k=0}^{\infty} \left( x[k]^T Q \, x[k] + u[k]^T R \, u[k] \right)

where:

  • Q0Q \succeq 0 (positive semi-definite) penalizes state deviation.
  • R0R \succ 0 (positive definite) penalizes control effort.

Optimal Gain via DARE

The solution requires finding the matrix PP that satisfies the Discrete Algebraic Riccati Equation:

P=ATPAATPB(R+BTPB)1BTPA+QP = A^T P A - A^T P B \left(R + B^T P B\right)^{-1} B^T P A + Q

The optimal gain is then:

K=(R+BTPB)1BTPAK = \left(R + B^T P B\right)^{-1} B^T P A

and the optimal control law is u[k]=Kx[k]u[k] = -Kx[k].

Existence and Uniqueness

A unique stabilizing solution PP exists when:

  1. (A,B)(A, B) is stabilizable (all unstable modes are controllable).
  2. (A,C)(A, C) is detectable (where Q=CTCQ = C^T C).

Bryson's Rule (Initial Weight Selection)

Qii=1(max acceptable xi)2,Rjj=1(max acceptable uj)2Q_{ii} = \frac{1}{(\text{max acceptable } x_i)^2}, \qquad R_{jj} = \frac{1}{(\text{max acceptable } u_j)^2}

Complexity Analysis

PhaseTimeSpaceNotes
DARE solve (offline)O(In3)O(I \cdot n^3)O(n2)O(n^2)II iterations, each involving n×nn \times n matrix operations
Control step (online)O(nm)O(n \cdot m)O(nm)O(n \cdot m)Single matrix-vector multiply u=Kxu = -Kx

The DARE iteration is the expensive part, but it is done once (offline or at initialization). The per-sample cost is just a matrix-vector product.

Step-by-Step Walkthrough

System: Double integrator with Δt=0.01\Delta t = 0.01

A=[10.0101],B=[0.000050.01]A = \begin{bmatrix} 1 & 0.01 \\ 0 & 1 \end{bmatrix}, \quad B = \begin{bmatrix} 0.00005 \\ 0.01 \end{bmatrix}

Q=[100001],R=[1]Q = \begin{bmatrix} 100 & 0 \\ 0 & 1 \end{bmatrix}, \quad R = [1]

Step 1 — Initialize P0=QP_0 = Q

Step 2 — DARE iteration (showing first iteration):

  1. S=R+BTP0B=1+[0.00005  0.01][100001][0.000050.01]=1.0001S = R + B^T P_0 B = 1 + [0.00005\; 0.01] \begin{bmatrix}100 & 0\\0 & 1\end{bmatrix} \begin{bmatrix}0.00005\\0.01\end{bmatrix} = 1.0001
  2. Solve SKpart=BTP0AS \cdot K_{\text{part}} = B^T P_0 A via Gaussian elimination
  3. Update P1=ATP0AATP0BKpart+QP_1 = A^T P_0 A - A^T P_0 B \cdot K_{\text{part}} + Q
  4. Check convergence: P1P0\|P_1 - P_0\|

Step 3 — Repeat until Pk+1Pk<ε\|P_{k+1} - P_k\| < \varepsilon (typically 10–30 iterations).

Step 4 — Extract gain K=(R+BTPB)1BTPAK = (R + B^T P B)^{-1} B^T P A

Runtime: u[k]=Kx[k]u[k] = -K x[k] — a $1 \times 2 times \2 \times 1$ multiply per sample.

Pitfalls & Edge Cases

  • Uncontrollable modes. If (A,B)(A, B) is not stabilizable, the DARE has no stabilizing solution. Verify controllability before calling the solver.
  • Ill-conditioned RR. If RR is near-singular, the inversion in KK becomes numerically unstable. Ensure RR is strictly positive definite.
  • Q/R scaling. Only the ratio of QQ to RR matters. Scaling both by the same factor does not change KK.
  • Full state required. LQR assumes all states are measured. If only partial measurements are available, combine LQR with a Kalman Filter to form an LQG controller.
  • Fixed-point limitations. The DARE involves matrix inversions and multiplications that can overflow Q15/Q31 ranges. Prefer floating-point for the offline solve; use the pre-computed KK constructor for fixed-point runtime.

Variants & Generalizations

VariantKey Difference
Continuous-time LQRSolves the Continuous ARE instead of DARE; used for continuous-time plant models
LQG (Linear Quadratic Gaussian)Combines LQR with Kalman filter for systems with noisy, partial observations
LQR with integral actionAugments the state with integral of tracking error to eliminate steady-state offset
Finite-horizon LQRTime-varying gain K[k]K[k] for finite-duration tasks
Robust LQR (H∞)Accounts for model uncertainty by optimizing the worst-case cost
Pre-computed gainKK is computed offline (e.g., in MATLAB with dlqr) and hard-coded for embedded targets

Applications

  • Cart-pole balancing — Classic underactuated system; LQR linearized around the upright equilibrium.
  • Quadrotor attitude control — Regulating roll, pitch, yaw with motor thrust as input.
  • Satellite station-keeping — Maintaining orbital position with minimal thruster fuel.
  • Industrial process control — Multi-input multi-output (MIMO) systems where PID struggles.
  • Autonomous vehicles — Lateral and longitudinal control using a linearized vehicle model.

Connections to Other Algorithms

graph LR
    LQR["LQR Controller"]
    DARE["DARE Solver"]
    GE["Gaussian Elimination"]
    KF["Kalman Filter"]
    PID["PID Controller"]
    DARE --> LQR
    GE --> DARE
    KF -->|"state estimate"| LQR
    PID -.->|"model-free alternative"| LQR
AlgorithmRelationship
DARE SolverComputes the cost-to-go matrix PP from which KK is derived
Gaussian EliminationUsed inside DARE iteration to solve linear sub-systems
Kalman FilterProvides state estimates when direct measurement is unavailable (forming LQG)
PID ControllerModel-free alternative; simpler to deploy but not optimal

References & Further Reading

  • Anderson, B.D.O. and Moore, J.B., Optimal Control: Linear Quadratic Methods, Prentice Hall, 1990.
  • Franklin, G.F., Powell, J.D. and Emami-Naeini, A., Feedback Control of Dynamic Systems, 8th ed., Pearson, 2019 — Chapter 9.
  • Bryson, A.E. and Ho, Y.-C., Applied Optimal Control, Hemisphere, 1975.