This site is version 0.0.1. Most of the code on this site is drafted by an agent and needs review. I apologize for the AI-speak, we are working on rewriting it!
viv-lang.org

Notebooks · lib/lqr

The linear-quadratic regulator, for a system with one input

Given a linear system zdot = A z + B u and a price on being away from zero and on pushing, W and r, the gains that minimize the total bill are K = B^T P / r, where P solves the algebraic Riccati equation

A^T P + P A - P B B^T P / r + W = 0.

This is a small front end on lib/are for the common case of one input, where the weight on the input is a single number. The equation itself is solved there, by the Schur method.

There is a second solver here, lqr_riccati_integrated, and it is the one to reach for never. It integrates Pdot = -(A^T P + P A - P B B^T P / r + W) backwards from zero for eighty seconds of simulated time and returns wherever it happens to have got to. On well-behaved problems it agrees with the real solver to twelve digits, which is what makes it dangerous. Hand it a system whose closed loop has a time constant of seven hundred seconds and it returns 73.783 where the answer is 414.214 — wrong by four fifths, with nothing in its output to say so. It is kept here because that failure is worth being able to run, and the last test below runs it.

Used with import lqr.

Look inside this program

  • Atlas

    The program’s map: every function and which calls which.

  • There’s no recorded run of this program yet.

Shift+Enter in a cell runs it and moves to the next. Running a cell runs every cell above it too, as one program.

[1]lines 28–167