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 such that the control law 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 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
where is the state, is the control input, is the state transition matrix, and is the input matrix.
Cost Function
The LQR minimizes the infinite-horizon quadratic cost:
where:
- (positive semi-definite) penalizes state deviation.
- (positive definite) penalizes control effort.
Optimal Gain via DARE
The solution requires finding the matrix that satisfies the Discrete Algebraic Riccati Equation:
The optimal gain is then:
and the optimal control law is .
Existence and Uniqueness
A unique stabilizing solution exists when:
- is stabilizable (all unstable modes are controllable).
- is detectable (where ).
Bryson's Rule (Initial Weight Selection)
Complexity Analysis
| Phase | Time | Space | Notes |
|---|---|---|---|
| DARE solve (offline) | iterations, each involving matrix operations | ||
| Control step (online) | Single matrix-vector multiply |
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
Step 1 — Initialize
Step 2 — DARE iteration (showing first iteration):
- Solve via Gaussian elimination
- Update
- Check convergence:
Step 3 — Repeat until (typically 10–30 iterations).
Step 4 — Extract gain
Runtime: — a $1 \times 2 times \2 \times 1$ multiply per sample.
Pitfalls & Edge Cases
- Uncontrollable modes. If is not stabilizable, the DARE has no stabilizing solution. Verify controllability before calling the solver.
- Ill-conditioned . If is near-singular, the inversion in becomes numerically unstable. Ensure is strictly positive definite.
- Q/R scaling. Only the ratio of to matters. Scaling both by the same factor does not change .
- 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 constructor for fixed-point runtime.
Variants & Generalizations
| Variant | Key Difference |
|---|---|
| Continuous-time LQR | Solves 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 action | Augments the state with integral of tracking error to eliminate steady-state offset |
| Finite-horizon LQR | Time-varying gain for finite-duration tasks |
| Robust LQR (H∞) | Accounts for model uncertainty by optimizing the worst-case cost |
| Pre-computed gain | 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
| Algorithm | Relationship |
|---|---|
| DARE Solver | Computes the cost-to-go matrix from which is derived |
| Gaussian Elimination | Used inside DARE iteration to solve linear sub-systems |
| Kalman Filter | Provides state estimates when direct measurement is unavailable (forming LQG) |
| PID Controller | Model-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.