This is a fundamental question about Linear Quadratic Regulation (LQR), which is a core topic in optimal control theory. Let me provide a comprehensive explanation covering:
- The LQR cost function definition
- The tradeoff between state error and control effort
- How to solve the Riccati equation
1. LQR Cost Function Definition
The standard discrete-time LQR cost function is:
J = Σ(k=0 to N-1) [x_k^T Q x_k + u_k^T R u_k] + x_N^T Q_f x_N
or for infinite horizon:
J = Σ(k=0 to ∞) [x_k^T Q x_k + u_k^T R u_k]
For continuous-time:
J = ∫₀^∞ [x(t)^T Q x(t) + u(t)^T R u(t)] dt
Where:
- x_k is the state at time k
- u_k is the control input at time k
- Q is a positive semi-definite matrix penalizing state deviations (dimensions: n×n)
- R is a positive definite matrix penalizing control effort (dimensions: m×m)
- Q_f is the terminal cost matrix (for finite horizon)
Subject to the linear system dynamics:
x_{k+1} = A x_k + B u_k (discrete-time)
or
ẋ(t) = A x(t) + B u(t) (continuous-time)
2. The Tradeoff Between State Error and Control Effort
This is a classic optimization tradeoff:
-
Q matrix: Penalizes deviations of the state from the desired value (typically zero). Larger Q values mean we want states to be close to target. If Q is very large, the controller will aggressively drive states toward zero, potentially using large control inputs.
-
R matrix: Penalizes control effort. Larger R values mean we care more about keeping control inputs small. If R is very large, the controller will use minimal control effort, even if it means states deviate more from desired values.
The designer must choose Q and R to balance these competing objectives:
- High Q, low R: Aggressive control, tight state tracking, potentially large actuator demands
- Low Q, high R: Conservative control, smoother states permitted, lower actuator wear
3. Solving the Riccati Equation
The optimal control law is: u_k = -K_k x_k = -R^{-1} B^T P
_k x_k
where P_k is determined by solving a Riccati equation. For infinite horizon problems, I use the Discrete-Time Algebraic Riccati Equation (DARE):
P = A^T P A - A^T P B (R + B^T P B)^{-1} B^T P A + Q
For finite horizon problems, I solve the Discrete-Time Dynamic Riccati Equation (DRE) backwards in time:
P_k = A^T P_{k+1} A - A^T P_{k+1} B (R + B^T P_{k+1} B)^{-1} B^T P_{k+1} A + Q
with terminal condition P_N = Q_f
In continuous time with infinite horizon, the Continuous-Time Algebraic Riccati Equation (CARE) governs P:
0 = A^T P + P A - P B R^{-1} B^T P + Q
To solve these equations, I can use direct methods like eigenvalue decomposition or the Hamiltonian matrix approach for smaller systems, iterative approaches such as fixed-point iteration or Newton-Raphson for convergence, or practical numerical solvers like MATLAB's lqr(), dare(), and care() functions which typically employ Hamiltonian eigenvalue decomposition.
Linear Quadratic Regulation (LQR)
1. LQR Cost Function
Standard Form
Discrete-time (infinite horizon):
J=∑k=0∞[x
Continuous-time:
J=∫0∞[x(t)
Subject to:
xk+1=Axk+
Key matrices:
| Matrix | Dimension | Purpose |
|---|
| Q | n × n | Penalizes state deviation (positive semi-definite) |
| R | m × m | Penalizes control effort (positive definite) |
2. State Error vs. Control Effort Tradeoff
Physical Interpretation
Q (State Penalty)
↗ ↖
/ \
Tight Large
Tracking → Control
Aggressive Effort
R (Control Penalty)
↗ ↖
/ \
Smooth Large
Control → State
Conservative Deviation
Practical Implications
| High Q, Low R | Balanced | Low Q, High R |
|---|
| Effect: Aggressive regulation | Effect: Moderate control | Effect: Soft control |
| Tight state tracking | Optimal balance | Large state deviations allowed |
| Large control inputs | Reasonable actuator demands | Minimal control effort |
| Fast but energy-intensive | Engineering tradeoff | Conservative, gradual |
| Risk of actuator saturation | Practical choice | Reduced system responsiveness |
Tuning Strategy
# Start with normalized values
Q = I # or np.eye(n)
R = I # or np.eye(m)
# Adjust based on objectives:
# - Increase Q[i,i] to tighten tracking of state i
# - Increase R[j,j] to reduce control input j
# - Use relative scaling: R = r*I, Q = q*I, then tune q/r ratio
3. Solving the Riccati Equation
Optimal Feedback Gain
The optimal control law is:
uk=−Kkx
where P (n × n, positive semi-definite) satisfies the Riccati equation.
Discrete-Time Algebraic Riccati Equation (DARE)
P=ATPA−ATPB(R+
Continuous-Time Algebraic Riccati Equation (CARE)
ATP+PA−PBR−1B
Solution Methods
Method 1: Finite-Horizon Backward Recursion (Practical & Stable)
import numpy as np
def lqr_finite_horizon(A, B, Q, R, N):
"""
Solve finite-horizon LQR backward in time
N: number of time steps
Returns: gains K[k] for k = 0,...,N-1
"""
n, m = B.shape
P = np.zeros((N+1, n, n))
K = np.zeros((N, m, n))
P[N] = Q # Terminal condition
# Backward recursion
for k in range(N-1, -1, -1):
# Cost-to-go Riccati update
H = R + B.T @ P[k+1] @ B # Hessian
P[k] = Q + A.T @ P[k+1] @ A - \
A.T @ P[k+1] @ B @ np.linalg.inv(H) @ B.T @ P[k+1] @ A
# Gain computation
K[k] = np.linalg.inv(H) @ B.T @ P[k+1] @ A
return K, P[:-1]
Method 2: Eigenvalue Decomposition (CARE)
For continuous-time, solve via Hamiltonian matrix:
def lqr_continuous(A, B, Q, R):
"""
Solve continuous-time LQR via eigenvalue method
"""
n = A.shape[0]
# Hamiltonian matrix
H = np.block([
[A, -B @ np.linalg.inv(R) @ B.T],
[-Q, -A.T]
])
# Eigenvalue decomposition
eigenvalues, eigenvectors = np.linalg.eig(H)
# Extract stable eigenvectors (negative eigenvalues)
idx = np.argsort(eigenvalues.real)[:n]
X = eigenvectors[:n, idx].real
Y = eigenvectors[n:, idx].real
# Solve for P
P = Y @ np.linalg.inv(X)
K = np.linalg.inv(R) @ B.T @ P
return K, P
Method 3: Using Standard Libraries (Recommended)
from scipy.linalg import solve_continuous_are, solve_discrete_are
# Discrete-time
P_discrete = solve_discrete_are(A, B, Q, R)
K_discrete = np.linalg.inv(R + B.T @ P_discrete @ B) @ B.T @ P_discrete @ A
# Continuous-time
P_continuous = solve_continuous_are(A, B, Q, R)
K_continuous = np.linalg.inv(R) @ B.T @ P_continuous
# MATLAB/Python Control Toolbox
from control import lqr
K, S, E = lqr(A, B, Q, R) # S is Riccati solution P
Complete Example
import numpy as np
from scipy.linalg import solve_discrete_are
import matplotlib.pyplot as plt
# System: mass-spring-damper
A = np.array([[1.0, 0.1],
[0.0, 0.9]])
B = np.array([[0.0],
[0.1]])
# Cost matrices (tune this!)
Q = np.diag([10.0, 1.0]) # Penalize position more
R = np.array([[1.0]])
# Solve DARE
P = solve_discrete_are(A, B, Q, R)
K = np.linalg.inv(R) @ B.T @ P
# Closed-loop simulation
x = np.array([1.0, 0.0]) # Initial state
x_hist, u_hist = [x.copy()], []
for _ in range(50):
u = -K @ x
u_hist.append(u[0])
x = A @ x + B @ u
x_hist.append(x.copy())
x_hist = np.array(x_hist)
plt.figure(figsize=(12, 4))
plt.subplot(1,2,1)
plt.plot(x_hist[:, 0], label='Position'); plt.plot(x_hist[:, 1], label='Velocity')
plt.legend(); plt.ylabel('State'); plt.xlabel('Time'); plt.title('State Trajectory')
plt.subplot(1,2,2)
plt.plot(u_hist)
plt.ylabel('Control Input'); plt.xlabel('Time'); plt.title('Control Effort')
plt.tight_layout()
Key Insights
| Aspect | Key Point |
|---|
| Optimality | LQR is proven optimal for linear systems with quadratic cost |
| Stability | Optimal gain guarantees closed-loop stability if (A,B) controllable |
| Decoupling | Q and R independently control state/effort tradeoff |
| Convergence | Riccati solution converges if system is stabilizable |
| Numerical | Always use standard libraries; direct Riccati is numerically sensitive |