ENع
←Observers
Lesson S2 · Special topics

Estimating hidden states with an observer

18 min11 steps5 questionsIntermediate
About this lesson

State feedback needs every state, but sensors rarely measure them all. Take the spring-mounted cart with only a position sensor: run a copy of its model in software, correct the copy with the gap between the measured and the predicted position, and pick the correction gain L so the estimation error dies fast. Then close the loop on the estimate and see the separation principle at work.

You'll learn to

Tick what you feel confident about. These self-checks do not affect your lesson score.

Fig S2 · Interactive
01 / 11

A state nobody measures

Pole placement fed the cart both states: position x₁ and velocity x₂. Our cart has only a position sensor. The velocity is a hidden state: it is real and it matters, but nothing measures it.

cart     ẋ = A x + B u,  y = C x
         A = [[0, 1], [-25, -2]]   B = [0, 0.5]
         C = [1, 0]   ← one sensor: position

x1  position   measured: y = x1
x2  velocity   hidden

State feedback u = -K x needs x2 too.
Loading figure…
or ← →
Read the lesson step by step
  1. A state nobody measures

    Pole placement fed the cart both states: position x₁ and velocity x₂. Our cart has only a position sensor. The velocity is a hidden state: it is real and it matters, but nothing measures it.

    cart     ẋ = A x + B u,  y = C x
             A = [[0, 1], [-25, -2]]   B = [0, 0.5]
             C = [1, 0]   ← one sensor: position
    
    x1  position   measured: y = x1
    x2  velocity   hidden
    
    State feedback u = -K x needs x2 too.
  2. Run a copy of the model

    Idea one: simulate the cart in software, fed the same push u. Its state x̂ (x-hat) is our estimate. The cart was let go from 0.1 m; the copy starts at 0, because we never saw that. Nothing corrects it.

    real cart   x(0) = [0.1, 0]   let go, u = 0
    copy        x̂(0) = [0, 0]     a guess
    
    The copy obeys the same equation as the
    cart but starts somewhere else, so it
    sits still while the cart bounces.
  3. The copy forgets too slowly

    Call the gap e = x − x̂. For the copy it obeys ė = Ae, so it fades only as fast as the cart's own poles, −1 ± j4.90. The velocity estimate is off by 0.01 m/s or more until 3.7 s.

    # e = x - x̂ obeys ė = A e (u cancels); poles -1 ± j4.90
    # if the cart were unstable, the gap would grow
    # the cart and its copy, Euler steps of 1 ms, u = 0
    x1, x2 = 0.1, 0.0      # the real cart (x2 is hidden)
    h1, h2 = 0.0, 0.0      # the estimate
    dt, last = 0.001, 0.0
    for n in range(5000):
        e = x1 - h1          # y - ŷ (the sensor reads x1)
        x1, x2 = x1 + dt*x2, x2 + dt*(-25*x1 - 2*x2)
        h1, h2 = h1 + dt*(h2 + 0*e), h2 + dt*(-25*h1 - 2*h2 + 0*e)
        if abs(x2 - h2) >= 0.01: last = (n + 1) * dt
    print(round(last, 1))   # 3.7
  4. Compare with the sensor

    The sensor gives y, the real position. The copy predicts ŷ = Cx̂ = x̂₁. Their difference, y − ŷ, is the output error: the only evidence that the copy is wrong. At the start it is 0.100 − 0 = 0.100 m.

    y   = x1        measured        0.100 m
    ŷ   = x̂1        predicted       0.000 m
    y - ŷ           output error    0.100 m
    
    If the estimate were right, y - ŷ
    would be zero forever.
  5. Nudge the copy

    Feed the output error back into the copy: x̂̇ = Ax̂ + Bu + L(y − ŷ). The column L = [l₁, l₂] says how hard each estimate is pushed. A copy corrected this way is called a Luenberger observer.

    x̂̇ = A x̂ + B u + L (y - ŷ)
          model      correction
    
    L = [l1, l2]   l1 nudges x̂1, l2 nudges x̂2
    
    The observer is just code: the
    "feedback" closes inside the computer.
  6. The gap gets its own poles

    Subtract the observer from the cart and u cancels again: ė = (A − LC)e. Now L moves the gap's poles, just as K moved the cart's. The characteristic polynomial is s² + (2 + l₁)s + (25 + 2l₁ + l₂).

    ė = (A - L C) e
    
    A - L C = [[-l1,      1],
               [-25 - l2, -2]]
    
    det(sI - (A - LC))
      = s² + (2 + l1) s + (25 + 2 l1 + l2)
  7. Choose L

    Put both error poles at −10, 2.5 times further left than the controller's −4 ± j4. Match (s + 10)² = s² + 20s + 100: 2 + l₁ = 20 and 25 + 2l₁ + l₂ = 100. So L = [18, 39].

    want  (s + 10)² = s² + 20 s + 100
    
    2 + l1           = 20   → l1 = 18
    25 + 2·18 + l2   = 100  → l2 = 39
    
    L = [18, 39]
    
    python-control: L = place_acker(A.T, C.T,
    [-10, -10]).T  (place() refuses a double pole
    with one output).
  8. The ghost catches up

    Same wrong start, now with L. The ghost cart snaps onto the real one. The velocity estimate is off by 0.24 m/s at worst, and within 0.01 m/s after 0.6 s, where the plain copy needed 3.7 s. Nobody measured velocity.

    # the cart and its copy, Euler steps of 1 ms, u = 0
    x1, x2 = 0.1, 0.0      # the real cart (x2 is hidden)
    h1, h2 = 0.0, 0.0      # the estimate
    dt, last = 0.001, 0.0
    for n in range(5000):
        e = x1 - h1          # y - ŷ (the sensor reads x1)
        x1, x2 = x1 + dt*x2, x2 + dt*(-25*x1 - 2*x2)
        h1, h2 = h1 + dt*(h2 + 18*e), h2 + dt*(-25*h1 - 2*h2 + 39*e)
        if abs(x2 - h2) >= 0.01: last = (n + 1) * dt
    print(round(last, 1))   # 0.6
  9. Feed the estimate to K

    Now close the loop on the estimate: u = −Kx̂ + N̄r, with K = [14, 12] and N̄ = 64 from pole placement, aiming at r = 0.2 m. The controller never sees x₂; it trusts the observer.

    u = -K x̂ + N̄ r
    K = [14, 12]   N̄ = 64   r = 0.2 m
    
    at t = 0   x̂ = [0, 0]   u = 12.8 N
    (fed the true x it would be 11.4 N)
    
    at t = 3 s   cart at 0.200 m
  10. Two designs, no clash

    The loop's poles are the controller's −4 ± j4 plus the observer's −10, −10. This is the separation principle: design K and L apart. The wrong start only costs 23.1 mm at t = 0.42 s, then the curves join.

    closed loop on the estimate, states (x, e):
      ẋ = (A - BK) x + BK e + B N̄ r
      ė = (A - LC) e
    
    poles = poles(A - BK) ∪ poles(A - LC)
          = {-4 ± j4} ∪ {-10, -10}
    
    biggest gap from the true-state loop:
      23.1 mm at t = 0.42 s
  11. Why not make it faster

    A faster observer means a bigger L, so it leans harder on y and copies the sensor's noise into x̂ and then into u. A rough rule: make it about twice as fast as the controller. And it needs an observable system.

    bigger L   → error dies faster
               → noise in y leaks into x̂, u
    smaller L  → smooth, but trusts the model
    
    rough rule: observer about 2× faster
    than the controller (here 10 vs 4)
    
    needs (A, C) observable: rank [C; CA] = 2

Quiz Unlocks after the last step.