Skip to content
Tutorials Center
My learning

Topics

Lesson 25 of 36 ยท Control Design

State Feedback, Observers and LQR in Simulink

Design state-feedback controllers with pole placement and LQR, add a state observer or Kalman filter, handle reference tracking and build the structure with Simulink blocks.

A PID controller uses only the output error. When you have a model of the plant in state-space form, you can do better: feed back all the states with a gain matrix K, chosen to place the poles or to minimize a cost (LQR). If the states are not measured, an observer or Kalman filter estimates them. This lesson shows the theory briefly, the Simulink building blocks and the MATLAB commands, with NumPy stand-ins you can run in the browser.

The MathWorks products (Control System Toolbox, Simulink) cannot run on this site; the code blocks labelled stand-in are our own calculations.

State-space and state feedback

A linear plant is x' = A x + B u, y = C x. State feedback chooses the input u = -K x (plus a reference term). The closed loop becomes x' = (A - B K) x, so the poles are the eigenvalues of A - B K. If the pair (A, B) is controllable, the gain K can place the poles anywhere.

In MATLAB with Control System Toolbox:

MATLAB
sys = ss(A, B, C, 0);
ctrb_rank = rank(ctrb(A, B));      % equals the number of states if controllable
K = place(A, B, [-3 -4]);          % pole placement
[K, S, e] = lqr(A, B, Q, R);       % LQR: K, Riccati solution S, closed-loop poles e

In Simulink the structure is: a State-Space block (parameters A, B, C, D, initial condition) for the plant, a Gain block with -K (matrix multiplication set to Matrix(K*u)) and a Sum to add the reference term. A Mux or Bus Selector gathers the states when they come out of separate blocks.

flowchart LR
    R["Reference r"] --> N["Nr (feedforward gain)"]
    N --> S(("Sum"))
    X["State x"] --> K["-K"]
    K --> S
    S -->|"u"| P["Plant x' = Ax + Bu"]
    P --> X

State feedback with a feedforward gain Nr that sets the steady-state gain to 1.

LQR: choosing K by cost

The linear-quadratic regulator minimizes J = integral(x' Q x + u' R u) dt. Q penalizes state error, R penalizes control effort. Bigger Q relative to R gives faster, more aggressive control. The solution K = R^-1 B' S uses the solution S of the algebraic Riccati equation. Properties: guaranteed stability for a controllable and detectable system, good robustness margins (at least 60 degrees of phase margin and infinite upward gain margin in the ideal case), and a method that scales to many inputs.

Tuning advice: start with Q = diag(1/xmax^2) and R = 1/umax^2 (Bryson's rule), where xmax and umax are acceptable maxima, then scale until the response is right.

Stand-in: solve LQR with NumPy and simulate

The stand-in solves the discrete Riccati equation by iteration for a double integrator (a mass pushed by a force), then simulates the regulation from an initial position.

Python Stand-in: discrete LQR for a double integrator

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

You can see the trade-off: with a cheap input the controller pushes harder (larger u, faster settling), with an expensive input it is gentle and slower. All closed-loop poles have magnitude below 1, so the discrete loop is stable.

Reference tracking

Pure state feedback regulates to zero. To track a reference you have three common options:

  1. Feedforward gain Nr so that the steady-state output equals the reference: u = -K x + Nr r. Simple but sensitive to model error.
  2. Integral action: augment the state with the integral of the tracking error, xi' = r - y, and design K for the augmented system. This removes steady-state error even with model errors. In Simulink add an Integrator on the error and feed xi through its own gain.
  3. Servo LQR (lqi in MATLAB) which builds the integrator augmentation for you.

Observers: estimating the states

Usually not all states are measured. A Luenberger observer runs a copy of the plant and corrects it with the output error:

TEXT
xhat' = A xhat + B u + L (y - C xhat)

The error x - xhat decays with the poles of A - L C, which you place faster than the controller poles (about 2 to 5 times), but not so fast that measurement noise dominates. The separation principle says you can design K and L independently for a linear system, and the combination is stable.

In Simulink:

  • Build the observer from the State-Space block with A - L*C, inputs [B L] and an input vector [u; y] (a common compact pattern), or
  • Use the Kalman Filter block (Control System Toolbox) which needs Q (process noise), R (measurement noise) and the plant model. It is the optimal observer when the noise is Gaussian, and the pair LQR plus Kalman filter is called LQG.
  • For nonlinear systems the Extended Kalman Filter and Unscented Kalman Filter blocks (Control System Toolbox) take your state-transition and measurement functions.

Stand-in: a Luenberger observer

Python Stand-in: observer error shrinks when poles are faster

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The faster observer converges sooner. In a real system measurement noise would limit how large you can make L, which is what the Kalman filter's noise covariances express.

  • Use matrix signals and set Gain > Multiplication > Matrix (K*u) for K*x; vector inputs through a Mux or a bus with all states.
  • Put actuator Saturation in the loop and test with it; state feedback ignores limits unless you add anti-windup.
  • When a controller is designed in discrete time for a microcontroller, use c2d or design in discrete time directly (dlqr, lqrd) and run the controller in a Discrete subsystem at the sample time; see code generation.
  • After design, simulate the full nonlinear model (with noise, delays and limits) and compare with the linear one.

Common mistakes

  • Assuming controllability without checking rank(ctrb(A,B)).
  • Observer poles too slow or too fast: slow gives poor estimates, too fast amplifies noise.
  • Forgetting reference scaling or integral action, leaving a steady-state error.
  • Sign errors: u = -K x; the block must contain -K or a negative sum input.
  • Ignoring saturation and sample time in the nonlinear model.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What are the closed-loop poles with u = -K x?

    Substituting u = -Kx gives x' = (A - BK)x.
  2. 2. In LQR, what does increasing R relative to Q do?

    R weights the input; a larger R means the optimizer avoids large inputs.
  3. 3. What does the separation principle say?

    K and L can be designed separately and the combined system keeps both sets of poles.
  4. 4. How do you remove steady-state error with state feedback despite model error?

    Augmenting the state with the integral of the error forces zero steady-state error.

Summary

  • State feedback u = -Kx puts the closed-loop poles at the eigenvalues of A - BK if (A, B) is controllable; use place or lqr.
  • LQR picks K by trading state error (Q) against control effort (R); start with Bryson's rule.
  • Add a feedforward gain or integral action for reference tracking.
  • Observers and Kalman filters estimate unmeasured states; the Kalman filter block and LQG combine both.
  • Validate the design on the nonlinear model with saturation, noise and sample time.

Next: Code generation workflow and Embedded Coder