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 , find the feedback that minimises
penalises state deviations, penalises control effort. The minimiser is a constant state feedback,
where 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 is stabilisable and every unstable mode shows up in (detectability of ).
Bryson's rule gives a first choice of weights: make each term 1 when its variable reaches its largest acceptable value,
then scale up (gentler, slower) or down (more aggressive) and simulate. Only the ratio of to matters.
Robustness for free. For a single input, the LQR loop always has a phase margin of at least and a gain margin from to infinity (the Nyquist plot never enters the unit circle centred at ; 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 of Pole Placement. Typical moves are , so take as the error scale; current up to ; the voltage should stay within about ; speed not penalised.
Find: by Bryson's rule, the closed-loop poles, overshoot, settling time and peak voltage for a step; then the effect of scaling .
Solve:
- , . For a scalar position weight the first gain is about , so a step starts with about .
ct.lqrreturns ; the reference scale comes from the same formula as before.- Then repeat with multiplied by 0.1 and by 10.
Check:
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 () triples the gains and the voltage peak (, more than the supply) but hardly speeds the servo up, because the current weight keeps the current near and the current sets the acceleration. Making control ten times dearer () cuts the peak to 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 () meets the and 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 () so each zone reaches its setpoint exactly, and weigh the heater powers:
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 , although it loses to the room, because zone 1 feeds it 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 ( each in the reference design): if they are exceeded, raise .
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 : the first gain, and with it the voltage step, scales as : a factor of about 3.2 per decade of .
Simulation. In the plate program change the setpoints to : the powers become equal, each (the coupling then carries no heat). Then try , : zone 2's heater must hold back while zone 1 heats; a heater cannot cool, so in the simulation means the specification is infeasible on this hardware.
What you would measure. Two power resistors on one aluminium plate, each driven by a logic-level MOSFET with PWM from a current-limited supply ( limit for both), two NTC or digital temperature sensors, a microcontroller running the 2 × 4 gain matrix at . Expect the temperatures to track within the sensor accuracy ( for common digital sensors) after settling, and time constants within of the model (convection is not linear).
Hazards and safe setup. The plate reaches : mount it on stand-offs, add a thermal fuse or a software cut-off at , never leave it unattended; current-limited low-voltage supply only; check the MOSFET dissipation at (, 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 and mean nothing physically.
- A state that matters but has zero weight and is unstable: LQR needs detectability.
- Trusting the 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
- For (scalar), , : solve the Riccati equation and give and the closed-loop pole.
- Why does multiplying both and by 10 leave unchanged?
Answer
- , : , , , pole at . Heavier state weight, faster pole.
- is multiplied by 10, so the same minimises it; in the formulas scales by 10 and by .
Test yourself
In LQR, increasing while keeping fixed generally
- makes the loop faster
- makes control effort smaller and the loop slower
- destabilises the loop
- has no effect
K, P, E = ct.lqr(A, B, Q, R) returns
- gain, Riccati solution, closed-loop eigenvalues
- poles, zeros, gain
- observer gain, covariance, error
- gain margin, phase margin, crossover
For single-input full-state LQR the phase margin is at least
- it is not guaranteed
Bryson's rule sets the diagonal weights to
- 1 for every state
- the inverse squares of the largest acceptable values
- the eigenvalues of
- the squares of the sensor resolutions