DLQR
The discrete-time linear quadratic regulator (DLQR) calculates the optimal state-feedback gain matrix K such that the feedback law u[n] = -Kx[n] minimizes the quadratic cost function:
J = \sum_{n=0}^{\infty} (x[n]^T Q x[n] + u[n]^T R u[n] + 2x[n]^T N u[n])
This function accepts discrete-time system matrices A and B, along with weight matrices Q, R, and optionally N. It returns the optimal gain matrix K.
Excel Usage
=DLQR(A, B, Q, R, N)
A(list[list], required): The discrete-time state dynamics matrix A (NxN).B(list[list], required): The discrete-time input matrix B (NxM).Q(list[list], required): The state weighting matrix Q (NxN). Should be positive semi-definite.R(list[list], required): The input weighting matrix R (MxM). Should be positive definite.N(list[list], optional, default: null): Optional cross-weighting matrix N (NxM).
Returns (list[list]): The optimal discrete-time state-feedback gain matrix K.
Example 1: Discrete double integrator LQR
Inputs:
| A | B | Q | R | ||
|---|---|---|---|---|---|
| 1 | 1 | 0.5 | 1 | 0 | 1 |
| 0 | 1 | 1 | 0 | 1 |
Excel formula:
=DLQR({1,1;0,1}, {0.5;1}, {1,0;0,1}, {1})
Expected output:
| Result | |
|---|---|
| 0.434483 | 1.02847 |
Example 2: DLQR with explicit zero cross-weight matrix
Inputs:
| A | B | Q | R | N | ||
|---|---|---|---|---|---|---|
| 1 | 0.2 | 0 | 2 | 0 | 0.5 | 0 |
| 0 | 1 | 1 | 0 | 1 | 0 |
Excel formula:
=DLQR({1,0.2;0,1}, {0;1}, {2,0;0,1}, {0.5}, {0;0})
Expected output:
| Result | |
|---|---|
| 0.907482 | 0.975615 |
Example 3: Single-state discrete regulator
Inputs:
| A | B | Q | R |
|---|---|---|---|
| 0.95 | 1 | 1 | 0.1 |
Excel formula:
=DLQR({0.95}, {1}, {1}, {0.1})
Expected output:
0.86967
Example 4: Coupled-state discrete regulator
Inputs:
| A | B | Q | R | ||
|---|---|---|---|---|---|
| 1 | 0.1 | 0 | 3 | 0 | 1 |
| 0.05 | 0.9 | 1 | 0 | 2 |
Excel formula:
=DLQR({1,0.1;0.05,0.9}, {0;1}, {3,0;0,2}, {1})
Expected output:
| Result | |
|---|---|
| 0.912358 | 0.764562 |
Python Code
Show Code
import control as ct
import numpy as np
def dlqr(A, B, Q, R, N=None):
"""
Linear quadratic regulator design for discrete-time systems.
See: https://python-control.readthedocs.io/en/latest/generated/control.dlqr.html
This example function is provided as-is without any representation of accuracy.
Args:
A (list[list]): The discrete-time state dynamics matrix A (NxN).
B (list[list]): The discrete-time input matrix B (NxM).
Q (list[list]): The state weighting matrix Q (NxN). Should be positive semi-definite.
R (list[list]): The input weighting matrix R (MxM). Should be positive definite.
N (list[list], optional): Optional cross-weighting matrix N (NxM). Default is None.
Returns:
list[list]: The optimal discrete-time state-feedback gain matrix K.
"""
try:
def to_np(x):
if x is None:
return None
if not isinstance(x, list):
return np.array([[float(x)]])
if len(x) > 0 and not isinstance(x[0], list):
x = [x]
return np.array([[float(v) if v is not None and str(v) != "" else 0.0 for v in row] for row in x])
a_np = to_np(A)
b_np = to_np(B)
q_np = to_np(Q)
r_np = to_np(R)
n_np = to_np(N)
if n_np is None:
K, S, E = ct.dlqr(a_np, b_np, q_np, r_np)
else:
K, S, E = ct.dlqr(a_np, b_np, q_np, r_np, n_np)
return K.tolist()
except Exception as e:
return f"Error: {str(e)}"Online Calculator
The discrete-time state dynamics matrix A (NxN).
The discrete-time input matrix B (NxM).
The state weighting matrix Q (NxN). Should be positive semi-definite.
The input weighting matrix R (MxM). Should be positive definite.
Optional cross-weighting matrix N (NxM).