LQR With a Kalman Filter
Open the Double Inverted Pendulum on a Cart app
Advanced Control Exercise 12 of 30 in Optimal Control · All exercises · Time: 15 minutes
A double inverted pendulum has two unstable modes and nobody can balance it by hand. An LQR does it easily when it knows all six states. Real rigs measure only three angles and positions with encoders; the rates have to be computed. This exercise compares differencing the encoder counts with a Kalman filter that predicts the rates from the model, under the same LQR.
Learning Outcomes
- State the separation principle and write the closed-loop poles of an LQR with a state observer.
- Explain why rates differenced from quantized encoders make the control force chatter, and why filtering them adds lag.
- Compare perfect states, differenced rates and a Kalman filter under one LQR by force chatter and rate error.
Book Chapter
Nonlinear Dynamics, Oscillations, and Stabilization (Volume 7), Chapter 8, Double Inverted Pendulum Simulator for Model-Based Balance and Computed Swing-Up. Read the chapter, open the PDF, or watch the lecture slides.
Theory in Brief
The cart position `x` and the two link angles `\theta_1, \theta_2` with their rates form the state `z`; the LQR applies `F = -K\hat z` from the Riccati solution of exercise 11. With perfect states `\hat z = z`. A rig measures `y = Cz`, three positions quantized to `q`, and needs the rates.
Differencing gives `\hat{\dot\theta}_k = (\theta_k - \theta_{k-1})/T_s`, whose quantization error is up to `q/T_s`: at `T_s` = 5 ms a step of 0.088° becomes a rate jump of 17.6 °/s. A low-pass filter on the difference trades that noise for lag. A Kalman filter predicts with the model and the force it applied, and corrects by the innovation:
$$\hat z_{k+1} = A \hat z_k + B F_k + L \left(y_k - C \hat z_k\right)$$
With an observer gain `L`, the closed loop's poles are the union of the regulator's and the observer's:
$$\mathrm{eig}\begin{bmatrix} A - BK & BK \\ 0 & A - LC \end{bmatrix} = \mathrm{eig}(A - BK) \cup \mathrm{eig}(A - LC)$$
This is the separation principle: design `K` as if the states were known, design `L` for the noise, and the pair is stable when each part is. With `L` from the Kalman filter it is the LQG controller. Separation guarantees nominal stability, not robustness.
Model: Physics-based. The linearized cart and two-link pendulum, used both by the regulator and by the Kalman filter's prediction.
Decision: The cart force every 5 ms, from the same LQR gain in all three runs; only the state estimate changes.
Learning: None. The Kalman filter's gain is computed from assumed noise covariances, not learned from data.
Exercise
Read 2 min · Predict 1 min · Run 7 min · Explain 3 min · Check your AI 2 min.
Predict. The encoders resolve 0.088° of angle, finer than the eye can see. Will differencing their readings make a visible difference to the force? Will the Kalman filter, working from the same readings, do better or worse than perfect states?
Run the Double Inverted Pendulum on a Cart app. Use the fastest speed, 2×, and press Run again to pause at the times in each step.
- Open 2 · The LQR with perfect states. Press Run and pause at 5 s. Read Force chatter and Rate error.
- Open 3 · Encoders, differences and a Kalman filter (rates differenced from encoder counts). Run to 5 s and read the same tiles.
- Press Reset, set State estimate to Kalman, run to 5 s and read the tiles again.
Explain.
- Why does differencing a 0.088° encoder make the force chatter, and why does a heavier rate filter make things worse rather than better here?
- The Kalman filter uses the same readings. What does it know that the difference does not?
Generative AI Learning
Check the answer against your run: A good answer says filtering adds lag that the fast unstable mode cannot afford, and that a Kalman filter predicts the rates from the model and the applied force. Your step 2 and step 3 chatter tiles are the test. The assistant is your tutor and reviewer; you answer for every claim you turn in.
What to Turn In
A three-row table (perfect, differenced, Kalman) with Force chatter and Rate error, your written prediction, and three sentences that answer the two questions.

With perfect states the LQR runs with 0.38 N of force chatter over the first 5 s. Working from differenced encoder counts, the chatter is 6.77 N and the rate error 5.7 °/s. The Kalman filter on the same readings brings them to 0.50 N and 0.5 °/s.
Each encoder step of 0.088° divided by a 5 ms sample is a rate spike of about 18 °/s, and the LQR multiplies rate errors by large gains, so the force jumps every time a count changes. Filtering harder smooths the spikes but delays the rates, and the unstable roots at +11.07 and +4.49 /s leave little time to spare: the app's own case text reports that a 15 ms rate filter loses the pendulum within a second. The Kalman filter knows the model and the force it just applied, so it predicts where the links are going and uses each reading only to correct that prediction. That is the separation principle at work: the same `K`, a better `\hat z`.
Measured with the app build of December 3, 2026 by a scripted replay of the steps above. Numbers that no tile shows come from the replay's record; your readings from the trends will be close. A later app release may move the last digit.
Double Pendulum models this plant in GEKKO. Linearize it at upright, compute `K` with `scipy.linalg.solve_discrete_are`, then compute a steady-state Kalman gain `L` the same way from the dual problem `(A^T, C^T)`. Verify numerically that the closed-loop eigenvalues are the union of `\mathrm{eig}(A-BK)` and `\mathrm{eig}(A-LC)`.
Related
- Check yourself: three-question quiz
- Course pages: Double Pendulum, Estimation and Control, Dynamic Control Introduction
- Exercises: previous: 11 · LQR Against MPC on a Finite Rail; next: 13 · Input Shaping Against a Predictive Controller
References
- Hedengren, J. D. (ed.), Nonlinear Dynamics, Oscillations, and Stabilization, Volume 7 of Interactive Control Engineering, Chapter 8, "Double Inverted Pendulum Simulator for Model-Based Balance and Computed Swing-Up", pp. 188–216, Advanced Process Solutions, 2026. PDF (CC BY 4.0).
- Anderson, B. D. O., Moore, J. B., Optimal Control: Linear Quadratic Methods, Prentice Hall, 1990, Chapter 8.
