Search

Search papers and lessons

Optimal Control

Level 3 · 9 min read

Optimal control chooses the input to a dynamic system that minimises a total cost over time, and for linear systems with quadratic cost the best input is a simple feedback law.

On this page

  • Colour = topic mix
  • Calculus
  • Discrete math
  • Optimization
  • Sign in to see your progress

TL;DR

A system changes over time, and you can push it with an input. Optimal control asks which input makes a total cost as small as possible, where the cost adds up a running penalty and a final penalty. For linear dynamics and quadratic costs, the answer is a feedback law: the input is a fixed matrix times the current state. That is the linear-quadratic regulator (LQR).

Where you'll see it: flight and robot controllers, rocket landing, and the theory under model predictive control and the value-based methods of reinforcement learning.

Intuition

Imagine steering a boat across a river with a current. You could hold the wheel at one angle and drift, or correct constantly. The best policy depends on where you are now, not on a fixed script. If you are drifting left, push right harder. The rule "push right in proportion to how far left you are" is a feedback law.

Optimal control formalises this. There are three ingredients.

  1. State x(t)x(t), the numbers that describe the system right now, such as position and velocity.
  2. Input u(t)u(t), the choice you control, such as a motor torque.
  3. Cost, a number that says how bad a trajectory is. It is usually a running cost for each moment plus a terminal cost for where you end up.

Why does this need theory at all? Trying every input sequence is hopeless. The state at each moment depends on all the earlier inputs, so the choices are coupled over time. The two classical tools, the maximum principle and dynamic programming, deal with that coupling in different ways. For the linear-quadratic case they meet in a single matrix equation.

flowchart LR
  C[Controller: u = -Kx] -->|input u| P[Plant: dx/dt = Ax + Bu]
  P -->|state x| S[Sensor reads x]
  S --> C

The math

The problem

The state evolves by a differential equation with input uu:

x˙(t)=f(x(t),u(t),t),x(0)=x0\dot{x}(t) = f\big(x(t), u(t), t\big), \qquad x(0) = x_0

Here x˙\dot{x} is the time derivative of xx. The cost over a fixed horizon [0,T][0, T] is

J(u)=∫0TL(x(t),u(t),t) dt+φ(x(T))J(u) = \int_0^T L\big(x(t), u(t), t\big)\, dt + \varphi\big(x(T)\big)

LL is the running cost and φ\varphi is the terminal cost. The goal is to find the input function uu that minimises JJ. Notice that uu is a whole function of time, not a single number. This is why optimal control is a calculus of variations problem: the unknown is a function, and variational methods handle that.

Dynamic programming and the value function

Define the value function V(x,t)V(x, t) as the lowest cost you can still achieve if you are in state xx at time tt:

V(x,t)=min⁡u(⋅) on [t,T][∫tTL ds+φ(x(T))]V(x, t) = \min_{u(\cdot)\,\text{on}\,[t,T]} \left[ \int_t^T L\, ds + \varphi\big(x(T)\big) \right]

Splitting the first small step off gives the Hamilton-Jacobi-Bellman equation, the continuous version of the Bellman equation:

−∂V∂t=min⁡u[L(x,u,t)+∇xV⋅f(x,u,t)],V(x,T)=φ(x)-\frac{\partial V}{\partial t} = \min_{u} \Big[ L(x, u, t) + \nabla_x V \cdot f(x, u, t) \Big], \qquad V(x, T) = \varphi(x)

Here ∇xV\nabla_x V is the gradient of VV with respect to the state. The minimising uu at each point is the optimal feedback. The equation is exact but solving it is hard, because VV is a function of the whole state. In more than a few dimensions, storing it on a grid is impossible.

The maximum principle

Pontryagin's maximum principle takes a different route. It introduces a costate λ(t)\lambda(t), a vector the same size as the state, and the Hamiltonian

H(x,u,λ,t)=L(x,u,t)+λ⊤f(x,u,t)H(x, u, \lambda, t) = L(x, u, t) + \lambda^{\top} f(x, u, t)

The optimal trajectory satisfies three conditions. The state runs forward with x˙=∂H/∂λ\dot{x} = \partial H / \partial \lambda. The costate runs backward with λ˙=−∂H/∂x\dot{\lambda} = -\partial H / \partial x, and λ(T)=∇φ(x(T))\lambda(T) = \nabla \varphi(x(T)). The input minimises HH at each moment, u∗(t)=arg⁡min⁡uHu^*(t) = \arg\min_u H. This turns the search over functions into a two-point boundary problem, which is solved numerically or by a shooting method.

Linear-quadratic regulator

Now take linear dynamics and quadratic cost. The state equation is x˙=Ax+Bu\dot{x} = Ax + Bu, and the cost over an infinite horizon is

J=∫0∞(x⊤Qx+u⊤Ru)dtJ = \int_0^\infty \left( x^{\top} Q x + u^{\top} R u \right) dt

with Q⪰0Q \succeq 0 and R≻0R \succ 0, so the cost penalises both state error and effort. The answer is a linear feedback

u∗(t)=−Kx(t),K=R−1B⊤Pu^*(t) = -K x(t), \qquad K = R^{-1} B^{\top} P

where PP is a symmetric matrix that solves the algebraic Riccati equation

A⊤P+PA−PBR−1B⊤P+Q=0A^{\top} P + P A - P B R^{-1} B^{\top} P + Q = 0

Two facts make this useful. The feedback KK depends only on the model and the cost weights, not on the starting state. And the optimal cost from any starting state x0x_0 is J∗=x0⊤Px0J^* = x_0^{\top} P x_0, a quadratic form. Under standard conditions (the pair (A,B)(A, B) is stabilisable and QQ sees every unstable mode), the closed loop is stable.

Worked examples

Example
Scalar LQR, integrator plant

Take x˙=u\dot{x} = u with cost ∫0∞(x2+u2) dt\int_0^\infty (x^2 + u^2)\, dt. In the notation above, A=0A = 0, B=1B = 1, Q=1Q = 1, R=1R = 1.

  1. The Riccati equation is 0+0−P2+1=00 + 0 - P^2 + 1 = 0, so P2=1P^2 = 1 and P=1P = 1 (we take the positive root).
  2. The gain is K=BP/R=1⋅1/1=1K = B P / R = 1 \cdot 1 / 1 = 1.
  3. The optimal input is u=−xu = -x. The closed loop is x˙=−x\dot{x} = -x, so x(t)=x0e−tx(t) = x_0 e^{-t}.
  4. Check the cost: ∫0∞(x2+u2) dt=2∫0∞x02e−2t dt=2x02⋅12=x02\int_0^\infty (x^2 + u^2)\, dt = 2 \int_0^\infty x_0^2 e^{-2t}\, dt = 2 x_0^2 \cdot \tfrac{1}{2} = x_0^2.
  5. This equals Px02=x02P x_0^2 = x_0^2, as the theory says.

Interpretation: the plant alone would hold the state wherever it is, so the controller supplies all the decay, at rate 1, and the cost matches the value function exactly.

Example
Scalar LQR, unstable plant

Now x˙=x+u\dot{x} = x + u, same cost, so A=1A = 1, B=1B = 1, Q=1Q = 1, R=1R = 1. Without control the state grows as ete^{t}.

  1. Riccati: 2⋅1⋅P+1−P2=02 \cdot 1 \cdot P + 1 - P^2 = 0, so P2−2P−1=0P^2 - 2P - 1 = 0. The positive root is P=1+2≈2.414P = 1 + \sqrt{2} \approx 2.414.
  2. Gain: K=BP/R=2.414K = BP / R = 2.414.
  3. Closed loop: x˙=(1−2.414) x=−1.414 x\dot{x} = (1 - 2.414)\, x = -1.414\, x. The unstable plant is now stable.
  4. Check the Riccati: 2(2.414)+1−(2.414)2=4.828+1−5.828=0.0002(2.414) + 1 - (2.414)^2 = 4.828 + 1 - 5.828 = 0.000, which is consistent to rounding.
  5. Optimal cost from x0x_0: J∗=2.414 x02J^* = 2.414\, x_0^2.

Interpretation: the controller pulls the state back harder than the plant pushes it out. The feedback gain is larger than the unstable rate, so the closed loop decays at rate 1.414. The plot shows the uncontrolled and controlled trajectories from x0=1x_0 = 1.

00.511.522.530246810time tstate x(t) from x0 = 1
  • no control
  • optimal LQR
The uncontrolled plant grows as e^t. The LQR feedback turns the same plant into one that decays at rate 1.414.

Tradeoffs and failure modes

SituationWhat goes wrongBetter choice
Dynamics are linear and cost is quadraticNothing, this is the easy caseLQR. One Riccati solve gives the whole controller
Dynamics are nonlinearThe Riccati equation does not applyLinearise around a trajectory and iterate (iLQR), or use model predictive control
Hard limits on input or stateLQR may demand impossible inputsPontryagin with constraints, or model predictive control, which re-solves with the limits in place
The model is wrongThe optimal gain is optimal only for the model you gave itRobust control, or re-estimate the model online
State has many dimensionsSolving the Hamilton-Jacobi-Bellman equation on a grid is impossibleApproximate dynamic programming, or use the maximum principle numerically
Only some states are measuredThe feedback needs the full stateCombine LQR with a Kalman filter (LQG)

The main failure mode is confusing the model with the plant. A gain that is optimal for a model can be unstable on real hardware if the delay, the friction, or the sensor noise is different.

History

The calculus of variations was developed in the 1700s by Euler and Lagrange, to find curves that minimise integrals. The idea that a control input could be chosen optimally was made practical in the late 1950s. Lev Pontryagin and his colleagues stated the maximum principle around 1956 to 1958, and Richard Bellman developed dynamic programming in the same period. Rudolf Kalman's work around 1960 gave the linear-quadratic regulator and its filtering counterpart. These results were used for aerospace guidance and then for industrial control. Model predictive control grew out of this work in the process industries, starting in the 1970s, and handles constraints that LQR cannot.

Check yourself

QuestionFor the scalar plant x˙=u\dot{x} = u with Q=1Q = 1 and R=1R = 1, what is the optimal gain if you change RR to 4?Show answer

With A=0A = 0, B=1B = 1, Q=1Q = 1, R=4R = 4, the Riccati equation is −P2/4+1=0-P^2/4 + 1 = 0, so P2=4P^2 = 4 and P=2P = 2. The gain is K=BP/R=2/4=0.5K = BP/R = 2/4 = 0.5. Making effort more expensive (RR larger) lowers the gain, so the controller acts more gently.

QuestionCompute the value of 2(2.414)+1−(2.414)22(2.414)+1-(2.414)^2 and explain what it tests.Show answer

4.828+1−5.828=04.828 + 1 - 5.828 = 0, to rounding. This tests that P=1+2P = 1 + \sqrt{2} satisfies the algebraic Riccati equation for A=1A = 1, B=1B = 1, Q=1Q = 1, R=1R = 1. If the value were not close to zero, the chosen PP would be wrong and the gain would not give the optimal feedback.

QuestionWhy does the optimal feedback u=−Kxu = -Kx not depend on the starting state x0x_0, while the optimal cost does?Show answer

The feedback is the policy: it says what to do at each state, and it depends only on the dynamics and the cost weights. The cost is the value of being in a particular state, and that value depends on how far from zero the state starts. For LQR the cost is x0⊤Px0x_0^{\top} P x_0, so it grows with the square of the starting distance, while the policy is the same linear rule everywhere.

QuestionA drone's dynamics are nonlinear and it must keep its thrust between 0 and 10 newtons. Which tool from this lesson fits best, and why not plain LQR?Show answer

Model predictive control fits best. Plain LQR assumes linear dynamics and does not enforce limits, so it can ask for thrust outside 0 to 10 newtons. Model predictive control solves a finite-horizon problem at each step with the limits as constraints, and it applies only the first input before re-solving. It can handle nonlinear models, at the cost of solving an optimisation problem online.

Sources