lqr
Continuous-time linear-quadratic regulator for a small state-space plant x' = Ax + Bu (up to 8 states, tested to 6): stabilising Riccati solution P (Hamiltonian matrix sign + Newton-Kleinman), gain K = R^-1 B'P, closed-loop poles with damping, Riccati residual, and the response to an initial state with an exact cost balance (cost paid + x_end'P x_end = x0'P x0). Writes out/response.csv, out/poles.csv, out/gain.csv.
- Field
- Dynamics and control
- Version
- 0.1.0
- Runs as
- Compiled (Rust)
- Typical run
- 0.05 s
Compiled from source: run sh packs/install.sh once before admitting it.
Known answers it must pass
r0001double integrator Q=I R=1, closed form K=[1, sqrt3], P=[[sqrt3,1],[1,sqrt3]], poles -sqrt3/2 +- j/2 (wn 1, zeta sqrt3/2); rel 1e-9: solver converges to round-offpassed
K.0.0{"value": 1.0, "rel_tol": 1e-09}K.0.1{"value": 1.7320508075688772, "rel_tol": 1e-09}P.0.0{"value": 1.7320508075688772, "rel_tol": 1e-09}P.0.1{"value": 1.0, "rel_tol": 1e-09}P.1.1{"value": 1.7320508075688772, "rel_tol": 1e-09}max_pole_real_per_s-0.866025 ± 8.7e-10-0.866025lockedunder 0.01 % of the tolerance usedmin_damping_ratio0.866025 ± 8.7e-100.866025lockedunder 0.01 % of the tolerance usedfastest_pole_rad_per_s1 ± 1e-091lockedunder 0.01 % of the tolerance usedcost_J1.73205 ± 1.7e-091.73205lockedunder 0.01 % of the tolerance usedriccati_residual_rel≤ 1e-124.71722e-17lockedinside the rangecost_balance_rel≤ 1e-092.08414e-15lockedinside the rangestable= truetruelockedexact matchdesign_ok= truetruelockedexact matchr0002scalar unstable plant a=2 b=3 q=4 r=5, closed form P = r(a+sqrt(a^2+b^2 q/r))/b^2 = 2.97036, K = bP/r, pole -sqrt(a^2+b^2 q/r) = -sqrt(11.2); rel 1e-9passed
P.0.0{"value": 2.9703556145201677, "rel_tol": 1e-09}K.0.0{"value": 1.7822133687121007, "rel_tol": 1e-09}max_pole_real_per_s-3.34664 ± 3.3e-09-3.34664lockedunder 0.01 % of the tolerance usedopen_loop_max_real_per_s2 ± 2e-092lockedunder 0.01 % of the tolerance usedriccati_residual_rel≤ 1e-121.11851e-16lockedinside the rangestable= truetruelockedexact matchopen_loop_stable= falsefalselockedexact matchr0003undamped oscillator w0=2 (A=[[0,1],[-4,0]]), Q=diag(9,1), R=1, hand-solved ARE: p12 = -w0^2+sqrt(w0^4+q1) = 1, p22 = sqrt(q2+2 p12) = sqrt3, p11 = p22 (w0^2+p12) = 5 sqrt3, wn = sqrt5, zeta = sqrt(3/20); rel 1e-9passed
P.0.0{"value": 8.660254037844386, "rel_tol": 1e-09}P.0.1{"value": 1.0, "rel_tol": 1e-09}P.1.1{"value": 1.7320508075688772, "rel_tol": 1e-09}K.0.0{"value": 1.0, "rel_tol": 1e-09}K.0.1{"value": 1.7320508075688772, "rel_tol": 1e-09}min_damping_ratio0.387298 ± 3.9e-100.387298lockedunder 0.01 % of the tolerance usedfastest_pole_rad_per_s2.23607 ± 2.2e-092.23607lockedunder 0.01 % of the tolerance usedmax_pole_real_per_s-0.866025 ± 8.7e-10-0.866025lockedunder 0.01 % of the tolerance usedriccati_residual_rel≤ 1e-122.09475e-17lockedinside the rangedesign_ok= truetruelockedexact matchr0004two decoupled scalar plants, two inputs, R=diag(1,0.5) times rho=2: scalar closed forms P11 = 2(1+sqrt1.5), P22 = sqrt7-2, off-diagonals 0 (abs 1e-12), poles -sqrt1.5 and -sqrt7; rel 1e-9passed
P.0.0{"value": 4.449489742783178, "rel_tol": 1e-09}P.1.1{"value": 0.6457513110645907, "rel_tol": 1e-09}P.0.1{"value": 0.0, "abs_tol": 1e-12}K.0.0{"value": 2.224744871391589, "rel_tol": 1e-09}K.1.1{"value": 0.6457513110645907, "rel_tol": 1e-09}K.0.1{"value": 0.0, "abs_tol": 1e-12}max_pole_real_per_s-1.22474 ± 1.2e-09-1.22474lockedunder 0.01 % of the tolerance usedfastest_pole_rad_per_s2.64575 ± 2.6e-092.64575lockedunder 0.01 % of the tolerance usedn_inputs= 22lockedexact matchriccati_residual_rel≤ 1e-121.98819e-17lockedinside the ranger00054-state coupled plant = Hadamard rotation H(DI + oscillator)H of the two closed forms above: P' = H P H gives P11 = 2sqrt3+1, P22 = 2sqrt3-1, P13 = -sqrt3; poles invariant (-sqrt3/2, zeta sqrt(3/20), wn sqrt5); x0 = H e1 so cost x0'Px0 = sqrt3; rel 1e-9passed
P.0.0{"value": 4.464101615137754, "rel_tol": 1e-09}P.1.1{"value": 2.4641016151377544, "rel_tol": 1e-09}P.0.2{"value": -1.7320508075688772, "rel_tol": 1e-09}max_pole_real_per_s-0.866025 ± 8.7e-10-0.866025lockedunder 0.01 % of the tolerance usedmin_damping_ratio0.387298 ± 3.9e-100.387298lockedunder 0.01 % of the tolerance usedfastest_pole_rad_per_s2.23607 ± 2.2e-092.23607lockedunder 0.01 % of the tolerance usedcost_J1.73205 ± 1.7e-091.73205lockedunder 0.01 % of the tolerance usedcontrollability_rank= 44lockedexact matchriccati_residual_rel≤ 1e-121.26866e-16lockedinside the rangecost_balance_rel≤ 1e-091.03511e-14lockedinside the rangedesign_ok= truetruelockedexact match
Each line is one reading the kernel checked. The bright band is the tolerance; the needle is the value the instrument actually returned when it was admitted. A needle near the centre line used little of its tolerance.
Inputs
| Name | Unit | Default | Meaning |
|---|---|---|---|
A | 1 | State matrix, n x n rows (n <= 8). Entries in the plant's own state units per second; time is in seconds. | |
B | 1 | Input matrix, n rows by m columns; a flat list of n numbers for a single input. | |
Q | 1 | State weight, symmetric positive semi-definite n x n, or a list of n diagonal weights. Left out: identity. | |
R | 1 | Input weight, symmetric positive definite m x m, or a list of m diagonal weights. Left out: identity. | |
rho | 1 | 1.0 | Multiplies R (effort price); raise it for gentler control. |
x0 | 1 | Initial state for the response, one number per state. Left out: [1, 0, ..., 0]. | |
t_end_s | s | 0.0 | Response length; 0 means eight time constants of the slowest closed-loop pole. |
n_steps | 1 | 400 | Samples in the response (exact transition matrix, so steps only set the resolution). |
Readings
| Name | Unit | Meaning |
|---|---|---|
n_states | 1 | |
n_inputs | 1 | |
K | 1 | Gain, m rows by n columns, u = -K x (units: input per state). |
P | 1 | Stabilising solution of A'P + PA - PBR^-1B'P + Q = 0 (units of cost per state squared). |
poles | Closed-loop poles, slowest first. | |
open_loop_poles | Eigenvalues of A, slowest first. | |
open_loop_stable | 1 | |
open_loop_max_real_per_s | 1/s | |
max_pole_real_per_s | 1/s | |
min_damping_ratio | 1 | |
fastest_pole_rad_per_s | rad/s | |
slowest_time_constant_s | s | |
settling_time_s | s | Four slowest time constants (2 % band). |
riccati_residual_rel | 1 | |A'P + PA - PGP + Q| over the size of its terms (Frobenius). |
kleinman_steps | 1 | |
P_min_eig | 1 | |
pole_error_rel | 1 | First-order pole error estimate from an independent complex determinant, relative to |Acl|. |
controllability_rank | 1 | |
controllable | 1 | |
cost_J | 1 | Optimal cost from x0: x0'P x0. |
cost_paid | 1 | Cost accumulated over the response (Van Loan Gramian, independent of P). |
cost_left | 1 | x_end'P x_end. |
cost_balance_rel | 1 | |cost_J - cost_paid - cost_left| / cost_J. |
peak_control_abs | 1 | |
peak_control_max | 1 | |
x_end | 1 | |
t_end_s | s | |
dt_s | s | |
n_steps | 1 | |
rho | 1 | |
stable | 1 | |
design_ok | 1 | |
warnings | 1 |
Try the call
Change a value and the command updates. Nothing runs here; paste it into your terminal or give it to your agent.