LQR

LQR Controller

Advanced
Run the live example Free · no sign-up · runs in your browser

The LQRController block is a linear-quadratic regulator. You give it a state-space model of the plant (A\mathbf{A}, B\mathbf{B}) and two weights: Q\mathbf{Q}, how much each state's deviation costs, and RR, how much control effort costs. The block computes the optimal state-feedback gain K\mathbf{K} itself and outputs u=−Kxu = -\mathbf{K}\mathbf{x}. Where a PID loop looks at one error signal, LQR uses every state at once — which is what lets it balance an inverted pendulum and bring the cart back to its place in the same move.

Wire each state of the plant to the inputs x0,x1,…x_0, x_1, \dots in the order of A\mathbf{A}; up to six states. To regulate to a setpoint instead of to zero, feed the deviation xi−xi,refx_i - x_{i,\text{ref}} into that input. The designed gains appear on the optional outputs K0…K5K_0 \dots K_5, and Valid reports whether a design could be made at all.

Mathematical Model

For the continuous design the block minimises

J=∫0∞(xTQx+Ru2)dt,x˙=Ax+BuJ = \int_0^\infty \left( \mathbf{x}^T \mathbf{Q} \mathbf{x} + R u^2 \right) dt, \qquad \dot{\mathbf{x}} = \mathbf{A}\mathbf{x} + \mathbf{B}u

The optimal gain comes from the stabilising solution X\mathbf{X} of the continuous algebraic Riccati equation:

ATX+XA−XBR−1BTX+Q=0,K=R−1BTX\mathbf{A}^T\mathbf{X} + \mathbf{X}\mathbf{A} - \mathbf{X}\mathbf{B}R^{-1}\mathbf{B}^T\mathbf{X} + \mathbf{Q} = 0, \qquad \mathbf{K} = R^{-1}\mathbf{B}^T\mathbf{X}

For the discrete design (x[k+1]=Ax[k]+Bu[k]\mathbf{x}[k+1] = \mathbf{A}\mathbf{x}[k] + \mathbf{B}u[k], cost ∑xTQx+Ru2\sum \mathbf{x}^T\mathbf{Q}\mathbf{x} + Ru^2) it solves the discrete Riccati equation:

X=ATXA−ATXB (R+BTXB)−1BTXA+Q,K=(R+BTXB)−1BTXA\mathbf{X} = \mathbf{A}^T\mathbf{X}\mathbf{A} - \mathbf{A}^T\mathbf{X}\mathbf{B}\,(R + \mathbf{B}^T\mathbf{X}\mathbf{B})^{-1}\mathbf{B}^T\mathbf{X}\mathbf{A} + \mathbf{Q}, \qquad \mathbf{K} = (R + \mathbf{B}^T\mathbf{X}\mathbf{B})^{-1}\mathbf{B}^T\mathbf{X}\mathbf{A}

The output is u=−Kxu = -\mathbf{K}\mathbf{x}. The design is accepted only when the closed loop A−BK\mathbf{A} - \mathbf{B}\mathbf{K} is stable.

Inputs & Outputs

Direction ID Label Type Status
→ In in1 x₀ number Required
→ In in2 x₁ number Optional
→ In in3 x₂ number Optional
→ In in4 x₃ number Optional
→ In in5 x₄ number Optional
→ In in6 x₅ number Optional
← Out out u number Output
← Out valid Valid number Output
← Out k_0 K₀ number Output
← Out k_1 K₁ number Output
← Out k_2 K₂ number Output
← Out k_3 K₃ number Output
← Out k_4 K₄ number Output
← Out k_5 K₅ number Output

Parameters

Parameters Label Type Default Description
design Design select continuous Continuous: dx/dt = A·x + B·u, minimising ∫(xᵀQx + R·u²)dt. Discrete: x[k+1] = A·x[k] + B·u[k], minimising Σ(xᵀQx + R·u²); use with a discrete plant
A A (n×n) matrix 0 1; 0 0 State matrix, rows separated by ;. Up to six states
B B (n×1) matrix 0; 1 Input matrix, one entry per state
Q Q matrix 1 0; 0 1 State weight: n numbers for the diagonal, or the full n×n matrix. A larger entry makes that state settle faster
R R number 1 Input weight. Larger R means gentler control effort; it must be above 0

Usage Examples

Basic LQRController

A double integrator (a cart without friction, A=[0 1; 0 0]\mathbf{A} = [0\ 1;\ 0\ 0], B=[0; 1]\mathbf{B} = [0;\ 1]) starts 1 m from the origin. With Q=I\mathbf{Q} = \mathbf{I} and R=1R = 1 the block designs K=[1, 3]\mathbf{K} = [1,\ \sqrt{3}] and brings the cart back without overshoot.

Inverted pendulum: PID versus LQR

The _Inverted Pendulum: PID vs LQR_ template balances the same pendulum twice: once with a PID on the angle alone, once with this block on all four states. The PID keeps the rod up but lets the cart run away; LQR catches the push and parks the cart at its setpoint.

Remarks & Best Practices

  • Tuning: start with Q=I\mathbf{Q} = \mathbf{I} and R=1R = 1, then raise the Q\mathbf{Q} entry of the state you care about most. Scaling Q\mathbf{Q} and RR together changes nothing.
  • When it says Valid = 0: the sizes of A\mathbf{A}, B\mathbf{B} and Q\mathbf{Q} do not fit, R≤0R \le 0, or the plant has an unstable mode the input cannot reach. No gain can stabilise such a plant, so the block outputs u=0u = 0 rather than a gain that would fail.
  • The model is yours: LQR is optimal for the A\mathbf{A} and B\mathbf{B} you type, not for the plant you wire. Use diagram variables for both so they cannot drift apart.
  • All states needed: LQR feeds back every state. When only some can be measured, estimate the rest with a KalmanFilter (together: LQG control).
  • Design is cached: K\mathbf{K} is computed once per run and recomputed only when A\mathbf{A}, B\mathbf{B}, Q\mathbf{Q} or RR change, so the block costs one dot product per step.
  • Single input: the block designs for one control input. A plant with several inputs needs one gain row per input.

Related Components