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⁻¹ 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 = x₀'P x₀).
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, ζ sqrt3/2); rel 10⁻⁹: solver converges to round-offpassed
K.0.0= 1, within 1.000000 × 10⁻⁹ relativereading not recorded on this pageK.0.1= 1.732051, within 1.000000 × 10⁻⁹ relativereading not recorded on this pageP.0.0= 1.732051, within 1.000000 × 10⁻⁹ relativereading not recorded on this pageP.0.1= 1, within 1.000000 × 10⁻⁹ relativereading not recorded on this pageP.1.1= 1.732051, within 1.000000 × 10⁻⁹ relativereading not recorded on this pagemax_pole_real_per_s-0.866025 ± 8.7 × 10⁻¹⁰-0.866025lockedunder 0.01 % of the tolerance usedmin_damping_ratio0.866025 ± 8.7 × 10⁻¹⁰0.866025lockedunder 0.01 % of the tolerance usedfastest_pole_rad_per_s1 ± 10⁻⁹1lockedunder 0.01 % of the tolerance usedcost_J1.73205 ± 1.7 × 10⁻⁹1.73205lockedunder 0.01 % of the tolerance usedriccati_residual_rel≤ 10⁻¹²4.71722 × 10⁻¹⁷lockedinside the rangecost_balance_rel≤ 10⁻⁹2.08414 × 10⁻¹⁵lockedinside the rangestable= truetruelockedexact matchdesign_ok= truetruelockedexact matchr0002scalar unstable plant a=2 b=3 q=4 r=5, closed form P = r(a+√(a²+b² q/r))/b² = 2.97036, K = bP/r, pole -√(a²+b² q/r) = -√(11.2); rel 10⁻⁹passed
P.0.0= 2.970356, within 1.000000 × 10⁻⁹ relativereading not recorded on this pageK.0.0= 1.782213, within 1.000000 × 10⁻⁹ relativereading not recorded on this pagemax_pole_real_per_s-3.34664 ± 3.3 × 10⁻⁹-3.34664lockedunder 0.01 % of the tolerance usedopen_loop_max_real_per_s2 ± 2 × 10⁻⁹2lockedunder 0.01 % of the tolerance usedriccati_residual_rel≤ 10⁻¹²1.11851 × 10⁻¹⁶lockedinside the rangestable= truetruelockedexact matchopen_loop_stable= falsefalselockedexact matchr0003undamped oscillator w₀=2 (A=[[0,1],[-4,0]]), Q=diag(9,1), R=1, hand-solved ARE: p12 = -w₀²+√(w₀⁴+q₁) = 1, p22 = √(q₂+2 p12) = sqrt3, p11 = p22 (w₀²+p12) = 5 sqrt3, wn = sqrt5, ζ = √(3/20); rel 10⁻⁹passed
P.0.0= 8.660254, within 1.000000 × 10⁻⁹ relativereading not recorded on this pageP.0.1= 1, within 1.000000 × 10⁻⁹ relativereading not recorded on this pageP.1.1= 1.732051, within 1.000000 × 10⁻⁹ relativereading not recorded on this pageK.0.0= 1, within 1.000000 × 10⁻⁹ relativereading not recorded on this pageK.0.1= 1.732051, within 1.000000 × 10⁻⁹ relativereading not recorded on this pagemin_damping_ratio0.387298 ± 3.9 × 10⁻¹⁰0.387298lockedunder 0.01 % of the tolerance usedfastest_pole_rad_per_s2.23607 ± 2.2 × 10⁻⁹2.23607lockedunder 0.01 % of the tolerance usedmax_pole_real_per_s-0.866025 ± 8.7 × 10⁻¹⁰-0.866025lockedunder 0.01 % of the tolerance usedriccati_residual_rel≤ 10⁻¹²2.09475 × 10⁻¹⁷lockedinside the rangedesign_ok= truetruelockedexact matchr0004two decoupled scalar plants, two inputs, R=diag(1,0.5) times ρ=2: scalar closed forms P11 = 2(1+sqrt1.5), P22 = sqrt7-2, off-diagonals 0 (abs 10⁻¹²), poles -sqrt1.5 and -sqrt7; rel 10⁻⁹passed
P.0.0= 4.44949, within 1.000000 × 10⁻⁹ relativereading not recorded on this pageP.1.1= 0.645751, within 1.000000 × 10⁻⁹ relativereading not recorded on this pageP.0.1= 0, within 1.000000 × 10⁻¹²reading not recorded on this pageK.0.0= 2.224745, within 1.000000 × 10⁻⁹ relativereading not recorded on this pageK.1.1= 0.645751, within 1.000000 × 10⁻⁹ relativereading not recorded on this pageK.0.1= 0, within 1.000000 × 10⁻¹²reading not recorded on this pagemax_pole_real_per_s-1.22474 ± 1.2 × 10⁻⁹-1.22474lockedunder 0.01 % of the tolerance usedfastest_pole_rad_per_s2.64575 ± 2.6 × 10⁻⁹2.64575lockedunder 0.01 % of the tolerance usedn_inputs= 22lockedexact matchriccati_residual_rel≤ 10⁻¹²1.98819 × 10⁻¹⁷lockedinside 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, ζ √(3/20), wn sqrt5); x₀ = H e₁ so cost x₀'Px0 = sqrt3; rel 10⁻⁹passed
P.0.0= 4.464102, within 1.000000 × 10⁻⁹ relativereading not recorded on this pageP.1.1= 2.464102, within 1.000000 × 10⁻⁹ relativereading not recorded on this pageP.0.2= -1.732051, within 1.000000 × 10⁻⁹ relativereading not recorded on this pagemax_pole_real_per_s-0.866025 ± 8.7 × 10⁻¹⁰-0.866025lockedunder 0.01 % of the tolerance usedmin_damping_ratio0.387298 ± 3.9 × 10⁻¹⁰0.387298lockedunder 0.01 % of the tolerance usedfastest_pole_rad_per_s2.23607 ± 2.2 × 10⁻⁹2.23607lockedunder 0.01 % of the tolerance usedcost_J1.73205 ± 1.7 × 10⁻⁹1.73205lockedunder 0.01 % of the tolerance usedcontrollability_rank= 44lockedexact matchriccati_residual_rel≤ 10⁻¹²1.26866 × 10⁻¹⁶lockedinside the rangecost_balance_rel≤ 10⁻⁹1.03511 × 10⁻¹⁴lockedinside 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⁻¹B'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 x₀: x₀'P x₀. |
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.