TExcelTAD
Home › Control Systems & Automation › Optimal Control: An Introduction to LQR

Optimal Control: An Introduction to LQR

Pole placement asks you where the closed-loop poles should go. For the three-state motor that was manageable; for the two-zone plate with integral action (four states, two heaters) you would have to invent four pole positions and the eight gains then follow without any say about how hard each heater works. Engineers usually know something different: how large an error is acceptable, how much current or power the actuator may use. The linear-quadratic regulator (LQR) turns exactly that knowledge into a gain matrix: it minimises a cost that weighs state errors against control effort, and the poles come out of the optimisation.

The cost and the answer

For x˙=Ax+Bu\dot{x} = Ax + Bu, find the feedback that minimises

J=∫0∞(xTQ x+uTR u)dt,Q⪰0, R≻0J = \int_0^\infty \left( x^T Q\, x + u^T R\, u \right) dt, \qquad Q \succeq 0,\ R \succ 0

QQ penalises state deviations, RR penalises control effort. The minimiser is a constant state feedback,

u=−Kx,K=R−1BTP,ATP+PA−PBR−1BTP+Q=0u = -Kx, \qquad K = R^{-1}B^T P, \qquad A^T P + P A - P B R^{-1} B^T P + Q = 0

where PP is the positive-definite solution of the algebraic Riccati equation. You never solve it by hand: K, P, E = ct.lqr(A, B, Q, R) returns the gain, the Riccati solution and the closed-loop eigenvalues. The closed loop is guaranteed stable if (A,B)(A, B) is stabilisable and every unstable mode shows up in QQ (detectability of (A,Q1/2)(A, Q^{1/2})).

Bryson's rule gives a first choice of weights: make each term 1 when its variable reaches its largest acceptable value,

Q=diag⁡ ⁣(1x1,max⁡2,… ),R=diag⁡ ⁣(1u1,max⁡2,… )Q = \operatorname{diag}\!\left(\frac{1}{x_{1,\max}^2}, \dots\right), \qquad R = \operatorname{diag}\!\left(\frac{1}{u_{1,\max}^2}, \dots\right)

then scale RR up (gentler, slower) or down (more aggressive) and simulate. Only the ratio of QQ to RR matters.

Robustness for free. For a single input, the LQR loop L(s)=K(sI−A)−1BL(s) = K(sI - A)^{-1}B always has a phase margin of at least 60∘60^\circ and a gain margin from 12\frac{1}{2} to infinity (the Nyquist plot never enters the unit circle centred at −1-1; see Stability Margins). These guarantees hold when the full state is measured. With an observer in the loop (LQG: LQR plus a Kalman filter) they can disappear, so margins must be checked again.

Worked example: the motor servo by weights

Given: the motor position model x=[ θ, ω, ia ]Tx = [\,\theta,\ \omega,\ i_a\,]^T of Pole Placement. Typical moves are 1 rad1\ \text{rad}, so take 1 rad1\ \text{rad} as the error scale; current up to 1.5 A1.5\ \text{A}; the voltage should stay within about 6 V6\ \text{V}; speed not penalised.

Find: KK by Bryson's rule, the closed-loop poles, overshoot, settling time and peak voltage for a 1 rad1\ \text{rad} step; then the effect of scaling RR.

Solve:

  • Q=diag⁡(1/12, 0, 1/1.52)=diag⁡(1, 0, 0.444)Q = \operatorname{diag}(1/1^2,\ 0,\ 1/1.5^2) = \operatorname{diag}(1,\ 0,\ 0.444), R=1/62=0.0278R = 1/6^2 = 0.0278. For a scalar position weight the first gain is about Q11/R=6 V/rad\sqrt{Q_{11}/R} = 6\ \text{V/rad}, so a 1 rad1\ \text{rad} step starts with about 6 V6\ \text{V}.
  • ct.lqr returns KK; the reference scale NN comes from the same formula as before.
  • Then repeat with RR multiplied by 0.1 and by 10.

Check:

python · try it yourself
import numpy as np
import control as ct

Ra, La, k, J, b = 2.0, 1e-3, 0.02, 2e-6, 1e-6
A = np.array([[0, 1, 0], [0, -b / J, k / J], [0, -k / La, -Ra / La]])
B = np.array([[0], [0], [1 / La]])
C = np.array([[1, 0, 0]])
Q = np.diag([1 / 1.0**2, 0.0, 1 / 1.5**2])
t = np.linspace(0, 0.3, 30001)
for scale in (0.1, 1.0, 10.0):
    R = scale * np.array([[1 / 6.0**2]])
    K, P, E = ct.lqr(A, B, Q, R)
    Acl = A - B @ K
    N = -1.0 / (C @ np.linalg.solve(Acl, B)).item()
    out = ct.forced_response(ct.ss(Acl, B * N, np.vstack([C, -K, [[0, 0, 1]]]), [[0], [N], [0]]),
                             T=t, U=np.ones_like(t)).outputs
    info = ct.step_info(ct.ss(Acl, B * N, C, 0), T=t)
    dom = E[np.argmin(np.abs(E.real))]
    print(f"R x {scale:4.1f}: K = [{K[0, 0]:6.2f} {K[0, 1]:7.4f} {K[0, 2]:6.3f}], "
          f"dominant pole {dom.real:7.1f}{dom.imag:+7.1f}j, overshoot {info['Overshoot']:4.1f} %, "
          f"settling {1e3 * info['SettlingTime']:5.1f} ms, |u|max {np.abs(out[1]).max():5.2f} V, "
          f"|i|max {np.abs(out[2]).max():4.2f} A")
R x  0.1: K = [ 18.97  0.2021 10.963], dominant pole   -86.5  +85.8j, overshoot  4.2 %, settling  49.0 ms, |u|max 18.97 V, |i|max 1.40 A
R x  1.0: K = [  6.00  0.0567  2.597], dominant pole   -85.4  +79.1j, overshoot  3.4 %, settling  50.6 ms, |u|max  6.00 V, |i|max 1.19 A
R x 10.0: K = [  1.90  0.0163  0.434], dominant pole   -78.1  +47.1j, overshoot  0.5 %, settling  47.0 ms, |u|max  1.90 V, |i|max 0.68 A

Read the table as a trade-off. Making control ten times cheaper (R×0.1R \times 0.1) triples the gains and the voltage peak (19 V19\ \text{V}, more than the 12 V12\ \text{V} supply) but hardly speeds the servo up, because the current weight keeps the current near 1.4 A1.4\ \text{A} and the current sets the acceleration. Making control ten times dearer (R×10R \times 10) cuts the peak to 1.9 V1.9\ \text{V} and almost removes the overshoot; the dominant poles move to a lower natural frequency, so the response rises more slowly, yet it reaches the 2 % band at about the same time. Bryson's choice (R×1R \times 1) meets the 6 V6\ \text{V} and 1.5 A1.5\ \text{A} limits without any pole being chosen by hand.

A multivariable design: the two-zone plate

The roadmap's multivariable project is the plate of State-Space Models: two heaters, two temperatures, strong coupling. LQR handles two inputs with the same call. Add one integral state per zone (x˙I,j=rj−Tj\dot{x}_{I,j} = r_j - T_j) so each zone reaches its setpoint exactly, and weigh the heater powers:

python · try it yourself
import numpy as np
import control as ct

Ch, g, g12 = 75.0, 1 / 8, 1 / 4
A = np.array([[-(g + g12), g12], [g12, -(g + g12)]]) / Ch
B = np.eye(2) / Ch
# augmented state [T1, T2, xI1, xI2], xI' = r - T
Aa = np.block([[A, np.zeros((2, 2))], [-np.eye(2), np.zeros((2, 2))]])
Ba = np.vstack([B, np.zeros((2, 2))])
Q = np.diag([1 / 2.0**2, 1 / 2.0**2, 1 / 200.0**2, 1 / 200.0**2])   # 2 K error, integral 200 K*s
R = np.diag([1 / 10.0**2, 1 / 10.0**2])                             # about 10 W per heater
K, P, E = ct.lqr(Aa, Ba, Q, R)
print("K (W per K and W per K*s):")
print(np.round(K, 3) + 0.0)
print("closed-loop time constants (s):", np.round(np.sort(-1 / E.real), 1))

Br = np.vstack([np.zeros((2, 2)), np.eye(2)])                       # setpoints enter the integrators
cl = ct.ss(Aa - Ba @ K, Br, np.vstack([np.hstack([np.eye(2), np.zeros((2, 2))]), -K]), 0)
t = np.linspace(0, 900, 9001)
r = np.vstack([20.0 * np.ones_like(t), 15.0 * np.ones_like(t)])    # zone 1 +20 K, zone 2 +15 K
y = ct.forced_response(cl, T=t, U=r).outputs
for ts in (60, 180, 900):
    i = ts * 10
    print(f"t = {ts:3d} s: T1 = {y[0, i]:5.2f} K, T2 = {y[1, i]:5.2f} K, P1 = {y[2, i]:5.2f} W, P2 = {y[3, i]:5.2f} W")
K (W per K and W per K*s):
[[ 5.344  0.234 -0.05   0.   ]
 [ 0.234  5.344  0.    -0.05 ]]
closed-loop time constants (s): [15.1 15.2 98.9 99.6]
t =  60 s: T1 =  7.19 K, T2 =  5.40 K, P1 = 10.77 W, P2 =  7.30 W
t = 180 s: T1 = 16.17 K, T2 = 12.14 K, P1 =  5.93 W, P2 =  2.68 W
t = 900 s: T1 = 20.00 K, T2 = 15.00 K, P1 =  3.75 W, P2 =  0.63 W

In steady state zone 2 needs only 0.63 W0.63\ \text{W}, although it loses 15/8=1.88 W15/8 = 1.88\ \text{W} to the room, because zone 1 feeds it (20−15)/4=1.25 W(20 - 15)/4 = 1.25\ \text{W} through the coupling; the LQR gain matrix is full (each heater reacts to both temperatures), which is exactly what two independent PI loops could not do. Check the powers against the heater limits (14.4 W14.4\ \text{W} each in the reference design): if they are exceeded, raise RR.

Guided lab: weights on the bench

Hand calculation. For the motor, Bryson's weights from your own limits (your current limit, your supply). The table above shows how strongly the peak voltage depends on RR: the first gain, and with it the voltage step, scales as 1/R1/\sqrt{R}: a factor of about 3.2 per decade of RR.

Simulation. In the plate program change the setpoints to r1=r2=20 Kr_1 = r_2 = 20\ \text{K}: the powers become equal, 20/8=2.5 W20/8 = 2.5\ \text{W} each (the coupling then carries no heat). Then try r1=20r_1 = 20, r2=0r_2 = 0: zone 2's heater must hold back while zone 1 heats; a heater cannot cool, so P2<0P_2 < 0 in the simulation means the specification is infeasible on this hardware.

What you would measure. Two 10 Ω10\ \Omega power resistors on one aluminium plate, each driven by a logic-level MOSFET with PWM from a 12 V12\ \text{V} current-limited supply (2.5 A2.5\ \text{A} limit for both), two NTC or digital temperature sensors, a microcontroller running the 2 × 4 gain matrix at 1–10 Hz1\text{–}10\ \text{Hz}. Expect the temperatures to track within the sensor accuracy (±0.5 K\pm 0.5\ \text{K} for common digital sensors) after settling, and time constants within ±30 %\pm 30\,\% of the model (convection is not linear).

Hazards and safe setup. The plate reaches 50–70 ∘C50\text{–}70\,^\circ\text{C}: mount it on stand-offs, add a thermal fuse or a software cut-off at 80 ∘C80\,^\circ\text{C}, never leave it unattended; current-limited low-voltage supply only; check the MOSFET dissipation at 1.2 A1.2\ \text{A} (I2RDS(on)I^2 R_{DS(on)}, plus switching loss) and add a small heatsink if it runs warm.

Pitfalls

  • Weights in inconsistent units: Bryson's rule scales them; raw identity matrices for QQ and RR mean nothing physically.
  • A state that matters but has zero weight and is unstable: LQR needs detectability.
  • Trusting the 60∘60^\circ guarantee with an observer in the loop: it applies to full-state feedback only.
  • Ignoring actuator limits: LQR is linear; check peak effort in simulation, and see Anti-Windup.

Practice

  1. For x˙=u\dot{x} = u (scalar), Q=qQ = q, R=1R = 1: solve the Riccati equation and give KK and the closed-loop pole.
  2. Why does multiplying both QQ and RR by 10 leave KK unchanged?
Answer
  1. A=0A = 0, B=1B = 1: −P2+q=0-P^2 + q = 0, P=qP = \sqrt{q}, K=qK = \sqrt{q}, pole at −q-\sqrt{q}. Heavier state weight, faster pole.
  2. JJ is multiplied by 10, so the same uu minimises it; in the formulas PP scales by 10 and R−1R^{-1} by 1/101/10.

Test yourself

In LQR, increasing RR while keeping QQ fixed generally

  1. makes the loop faster
  2. makes control effort smaller and the loop slower
  3. destabilises the loop
  4. has no effect

K, P, E = ct.lqr(A, B, Q, R) returns

  1. gain, Riccati solution, closed-loop eigenvalues
  2. poles, zeros, gain
  3. observer gain, covariance, error
  4. gain margin, phase margin, crossover

For single-input full-state LQR the phase margin is at least

  1. 30∘30^\circ
  2. 45∘45^\circ
  3. 60∘60^\circ
  4. it is not guaranteed

Bryson's rule sets the diagonal weights to

  1. 1 for every state
  2. the inverse squares of the largest acceptable values
  3. the eigenvalues of AA
  4. the squares of the sensor resolutions