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.
  • sys must be an ss object.

How lqr works

  • A must be square with one row per state.
  • B must have one row per state and one column per control input.
  • Q must be a finite real symmetric n-by-n matrix.
  • R must be a finite real symmetric positive-definite m-by-m matrix.
  • N, when supplied, must be a finite real n-by-m cross-weight matrix.
  • The block weighting matrix [Q N; N' R] must be positive semidefinite.
  • With one output, lqr returns K; with two outputs, it returns K and the Riccati solution S; with three outputs, it also returns the closed-loop poles of A-B*K.
  • Matrix-input forms are continuous-time. State-space object forms solve continuous-time models when Ts = 0 and discrete-time models when Ts > 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.

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.

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.

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.

Getting started · Benchmarks · Pricing

Download RunMat

Download RunMat for full performance, or use RunMat in your browser for zero setup.