# Compiling RiccatiSolve LQRegulator gains

Given some specific matricies, numA, numB, numP, q and r to calculate LQR gains, one can use simply LQRegulatorGains or solve the same via RiccatiSolve:

Inverse[r].(numB\[Transpose].RiccatiSolve[{numA, numB}, {q, r, matP}] + matP\[Transpose])


Giving the stated matricies some values, gains can be calculated and present the same solution as the aformentions LQRegulatorGains

I am attempting to compile either one of these functions for external use in embeddedhardware to adaptively calculate new gains on the fly but am completely overwhelmed in its generation. As far as I've come to understand not all code can be compiled and there are few and far between examples of higher level functions being compiled.

Is this possible? How would one give Compile matrices as a variable to be updated frequently for the given function?

Test matricies:

{numA, numB, numC, numD}={{{0,1,0},{113.116,-2.45364,0.00808392},{-113.116,2.45364,-0.136792}},{{0},{-15.4569},{261.555}},{{1,0,0},{0,1,0},{0,0,1}},{{0},{0},{0}}}
matP = {{0}, {0}, {0}}
q = DiagonalMatrix[{50, 1, 0.001}];
r = 2 {{1}};


LQRegulatorGains are typically computed offline before deployment and, if need be, some kind of gain scheduling is implemented to handle parameter variations. Also stuff involving matrix operations like Kalman filters and MPC have techniques like sequential Kalman filtering and explicit computations to circumvent doing the matrix computations online.
• You can see a case of gain scheduling using Which and NonlinearStateSpaceModel here which is part of a larger example. – Suba Thomas Aug 17 at 18:52
• I don't want to write in absolutes, but as I mentioned RiccatiSolve and LQRegulatorGains are offline techniques that it is practically certain that no effort will be made to make them compilable for deployment to an embedded target. – Suba Thomas Aug 17 at 18:52