lqr — Compute linear quadratic regulator state-feedback gains.
lqr(A,B,Q,R) and lqr(A,B,Q,R,N) solve the continuous-time algebraic Riccati equation and return the optimal gain K for the feedback law u = -K*x. lqr(sys,Q,R) and lqr(sys,Q,R,N) use the A and B matrices from an ss model and choose the continuous-time or discrete-time Riccati equation from sys.Ts.
Syntax
K = lqr(A, B, Q, R)
K = lqr(A, B, Q, R, N)
[K, S, e] = lqr(A, B, Q, R)
K = lqr(sys, Q, R)
[K, S, e] = lqr(sys, Q, R, N)- At most three output arguments are supported.
sysmust be anssobject.
How lqr works
Amust be square with one row per state.Bmust have one row per state and one column per control input.Qmust be a finite real symmetricn-by-nmatrix.Rmust be a finite real symmetric positive-definitem-by-mmatrix.N, when supplied, must be a finite realn-by-mcross-weight matrix.- The block weighting matrix
[Q N; N' R]must be positive semidefinite. - With one output,
lqrreturnsK; with two outputs, it returnsKand the Riccati solutionS; with three outputs, it also returns the closed-loop poles ofA-B*K. - Matrix-input forms are continuous-time. State-space object forms solve continuous-time models when
Ts = 0and discrete-time models whenTs > 0.
GPU memory and residency
lqr returns host-resident matrices and pole vectors.
Examples
Double-integrator regulator
A = [0 1; 0 0];
B = [0; 1];
Q = eye(2);
R = 1;
[K, S, e] = lqr(A, B, Q, R)Expected output:
K is approximately [1 1.7321], S is the stabilizing Riccati solution, and e contains stable closed-loop poles.State-space object form
sys = ss([0 1; 0 0], [0; 1], [1 0], 0);
K = lqr(sys, eye(2), 1)Expected output:
K is approximately [1 1.7321].Discrete state-space object form
sysd = ss([1 0.1; 0 1], [0.005; 0.1], [1 0], 0, 0.1);
[K, S, e] = lqr(sysd, eye(2), 1)Expected output:
K is a stabilizing discrete-time feedback gain and abs(e) is less than 1.How RunMat validates lqr
lqr validates state, input, and weighting dimensions, enforces finite real symmetric weights with positive-definite R and positive-semidefinite [Q N; N' R], solves continuous-time Riccati equations through a Hamiltonian matrix-sign method, solves discrete-time ss models through an iterative DARE path, and verifies Riccati residuals before materializing outputs.
- Implementation: `crates/runmat-runtime/src/builtins/control/lqr.rs`
See Correctness & Trust for the full methodology and coverage table.
Using lqr with coding agents
Open a RunMat example with live inputs, then ask the agent to explain how lqr changes the result.
Run a small lqr example, explain the result, then change one input and compare the output.
FAQ
Does lqr support a cross-weight matrix?⌄
Yes. Pass N as the fifth matrix argument or fourth ss-form argument.
Does lqr run on the GPU?⌄
No. It gathers numeric GPU inputs and computes the control synthesis on the host because the operation is a dense Riccati solve rather than an elementwise or provider-level array operation.
Related Control functions
damp · db · dcgain · feedback · impulse · isstable · nyquist · pole · pzmap · rlocus · ss · step · stepinfo · tf · zero
Open-source implementation
Unlike proprietary runtimes, every RunMat function is open-source. Read exactly how lqr is executed, line by line, in Rust.
- View the source for lqr in Rust on GitHub
- Learn how the RunMat runtime works
- Found a bug? Open an issue with a minimal reproduction.
About RunMat
RunMat is an open-source runtime that executes MATLAB-syntax code blazing on any GPU. It is licensed under the Apache 2.0 license.
- RunMat automatically optimizes your math for GPU execution on Apple, Nvidia, and AMD hardware. No code changes needed. Simulations that took hours now take minutes.
- Start running code in seconds. RunMat runs in the browser, on the desktop, or from the CLI. No license server, no IT ticket.