One of the old Wolfram blog posts that I use frequently is this demonstration of the LQR gains computation.


I've been able to trust this since version 8. It's just one of those things I "go to" without thinking too much about it when starting a controls project.

It's now broken in ways I describe below, and despite a few hours of poking deep, I haven't found the root cause nor a workaround. This has me doubting not only Mathematica version 12 but my own sanity. I cannot trust the entire infrastructure this demo stands on.

I downloaded a fresh copy of the CDF this morning. The notebook no longer qualitatively matches the posted videos, and it certainly used to match in earlier versions of Mathematica.

In particular, the last two simulations are badly broken. In the penultimate demonstration, the cart does not return to center after the second bump, but drifts off to the right forever. That behavior is contrary to the video and contrary to the behavior I have observed in live notebooks since version 8.

Something changed in version 12!

The last demonstration fails even more dramatically. The cart does not move at all while being bumped hard. The video shows the expected behavior.

Anyone have any insight to this? Is NDSolve broken? Could it be related to broken eigenvalues? Eigenvalues broken in Version 12.0

  • $\begingroup$ There is another demo here if you like to try it meanwhile. LinearQuadraticRegulatorControlOfAnInvertedPendulumWithFrict/ was written using version 8? (may 2012). Did not try to see if it still works in V12. $\endgroup$
    – Nasser
    Sep 20, 2019 at 21:28
  • $\begingroup$ btw, have you tried to contact the author Andrew Moylan about this? I would think he would be the best person to figure what went wrong if any since he would be the most familiar with his code. $\endgroup$
    – Nasser
    Sep 20, 2019 at 21:37
  • $\begingroup$ I have contacted Moylan in the past, not about this, but to see whether he ever completed the 3D version of the same demo. He said he hadn't and also (cordially) that he wasn't able to spare the time to work on this topic since he left Wolfram Research. $\endgroup$
    – Reb.Cabin
    Sep 21, 2019 at 0:58
  • $\begingroup$ The cited demo (...WithFrict) does appear to work in V12. I will try to add some bumping to it and thereby repro Moylan's blog by this alternative approach! It also occurred to me to play around with options in NDSolve in the original blog. It's possible some of the defaults changed and that the breakage isn't as catastrophic as it seems at first glance. $\endgroup$
    – Reb.Cabin
    Sep 21, 2019 at 1:03

1 Answer 1


Actually the code is already broken in v11.3, the underlying issue is, "CatchMachineUnderflow" option is removed in this version.

To fix the code, just turn to arbitrary precision and choose a high enough WorkingPrecision:

SimulatePendulum[force_] := 
    SetPrecision[#, Infinity] &@{eqns /. 
       f[t] -> force, {x[0] == x'[0] == θ'[0] == 
        0, θ[0] == π/2 - 0.1}}, {x, θ}, {t, 0, 30}, 
    WorkingPrecision -> 16]]]

Finally an illustration of the fixed last demonstration:

enter image description here


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.