PhysSandbox
Classical MechanicsWaves & SoundElectricity & MagnetismOptics & LightGravity & OrbitsLabs
🌙Astronomy & The Sky🌡️Thermodynamics🌍Biophysics, Fluids & Geoscience📐Math Visualization🔧Engineering🧪Chemistry

Related simulators

Similar topics nearby (including related fields) — or browse all 48 in Engineering.

View category →
NewUniversity / research

MPC Pendulum Swing-Up (MPPI)

Launch Simulator

Sampling-based Model Predictive Control: K candidate torque rollouts over horizon H, MPPI cost-weighted update, bounded torque |u|≤u_max — swing up an inverted pendulum live and watch the planner replan.

NewUniversity / research

PID Controller (1D)

Launch Simulator

Cart on a track: Kp, Ki, Kd and random velocity impulses toward set-point x = 0.

NewUniversity / research

Root Locus: 1 + K·G(s) = 0

Launch Simulator

Same pole–zero plant editor as the Bode/Nyquist map: trace closed-loop poles as K sweeps log-spaced, asymptotes and real-axis locus sketch, Durand–Kerner roots at the current K; characteristic D(s)+K g₀ N₀(s)=0.

NewUniversity / research

Cart & Inverted Pendulum

Launch Simulator

Horizontal force on a cart with a hinged rod: coupled (M+m)ẍ and mL²θ̈ integrated for manual balancing.

NewSchool

Finite State Machine

Launch Simulator

Traffic-light Moore machine: timed green–yellow–red cycle or manual step; state graph.

NewUniversity / research

Holonomic 2D Hovercraft MPC (MPPI)

Launch Simulator

Planar double integrator with ‖u‖₂ thrust cap: sampling-based MPPI steers to a draggable goal while soft-penalizing circular obstacles — rollout fan and best predicted path drawn live.

PhysSandbox

Interactive physics, chemistry, and engineering simulators for students, teachers, and curious minds.

Physics

  • Classical Mechanics
  • Waves & Sound
  • Electricity & Magnetism

Science

  • Optics & Light
  • Gravity & Orbits
  • Astronomy & The Sky

More

  • Thermodynamics
  • Biophysics, Fluids & Geoscience
  • Math Visualization
  • Engineering
  • Chemistry

© 2026 PhysSandbox. Free interactive science simulators.

PrivacyTermsContact
Home/Engineering/Cart–Pole: LQR vs MPC vs PID

Cart–Pole: LQR vs MPC vs PID

Nonlinear cart inverted pendulum: linearize about upright φ = θ, discrete-time LQR from DARE (Q, R → K), finite-horizon LQ MPC with DARE terminal cost, and hand-tuned PID — same force limit, kicks, and plant as the mechanics cart-pole.

Plant & control

Controller

1.2 kg
0.22 kg
0.55 m
32 N
0.02 s

LQR / MPC weights (diag Q, scalar R)

12
8
96
6
0.35
28

Shortcuts

  • •Pause resets nothing; use Reset pose for φ ≈ 0; Kick adds random ẋ / θ̇ impulse

Measured values

ModeLQR
LQR KK1=-5.02 K2=-8.20 K3=-62.49 K4=-14.37

About this model

This simulator couples the same nonlinear planar cart–pole used elsewhere on the site (horizontal force on the cart, point mass on a rigid rod) with three controller families for upright stabilization near θ = 0. The teaching model linearizes about the unstable equilibrium using φ = θ and a four-dimensional state (x, ẋ, φ, φ̇). A zero-order-hold discretization with step Δt yields (A_d, B_d). LQR solves the discrete-time algebraic Riccati equation (DARE) for the cost Σ (xᵀQx + uᵀRu) with diagonal Q and scalar R, producing a constant gain u = −Kx. MPC here is deliberately chosen as finite-horizon linear quadratic regulation with the same Q and R and a terminal cost matrix taken from the converged infinite-horizon P_∞; backward Riccati recursion over H steps changes the first gain K₀(H) relative to infinite-horizon K_∞, illustrating how horizon length shifts the initial policy even on the same linear model. PID is a transparent saturated law acting on the same measured variables for qualitative comparison. All controllers share the same force saturation and act on the nonlinear plant, so large deviations expose mismatch with the linear design model.

Who it's for: Undergraduate students in classical control, linear systems, or introductory robotics who have seen pole placement or LQR on paper and want a nonlinear sandbox with side-by-side MPC and PID baselines.

Key terms

  • Linear quadratic regulator (LQR)
  • Discrete-time Riccati equation (DARE)
  • Model predictive control (MPC)
  • Finite-horizon LQR
  • Inverted pendulum
  • Cart–pole system
  • Linearization
  • PID control

How it works

Nonlinear cart + point-mass pendulum (same force-coupled model as the mechanics cart-pole). Around the unstable upright θ = 0 we linearize in φ = θ and build a discrete plant (A_d, B_d) with sampling Δt. LQR solves the discrete-time algebraic Riccati equation (DARE) for P and constant gain K with stage cost xᵀQx + uᵀRu. MPC here is finite-horizon LQ with the same Q, R and a terminal cost P_∞ from the DARE, then the first row of gains K₀(H) (receding horizon, linear model). PID is a transparent hand-tuned law on (x, ẋ, φ, φ̇) with clamped integral on φ for comparison. All three saturate |F| ≤ F_max against the same nonlinear plant — tune Q/R and compare overshoot on x when rejecting kicks.

Key equations

Linearization about upright: φ = θ, state x_s = [x, ẋ, φ, φ̇]ᵀ, u = horizontal force
ẋ₁ = ẋ₂, ẋ₂ = u/M − (m/M)g φ, ẋ₃ = ẋ₄, ẋ₄ = −u/(M L) + ((M+m)/(M L)) g φ
DARE: P = Q + A_dᵀ P A_d − A_dᵀ P B_d (R + B_dᵀ P B_d)⁻¹ B_dᵀ P A_d → u = −K x_s

Frequently asked questions

Why can LQR still work if the real plant is nonlinear?
LQR is computed for a linear approximation valid when φ, ẋ, and φ̇ are small. Near the upright target the nonlinear dynamics are close to that tangent model, so a well-tuned Q/R pair often stabilizes a neighborhood of the equilibrium. Far from linearization, gains may be too aggressive or wrong in sign effect; saturation and unmodeled coupling then dominate.
What is the difference between the “MPC” mode here and the MPPI pendulum simulator?
The MPPI pendulum page uses sampling-based trajectory optimization on a nonlinear swing-up task with a stochastic update. Here MPC means finite-horizon LQ optimal control on the linearized discrete dynamics—deterministic, quadratic, and small enough to solve by Riccati recursion. Both are legitimately called MPC in the broad sense, but the mechanisms and homework questions differ.
How should I read the discrete-time Riccati iteration?
Starting from a positive semidefinite Q, repeated P ← Q + AᵀPA − AᵀPB(R + BᵀPB)⁻¹BᵀPA converges to P_∞ when (A_d, B_d) is stabilizable and mild conditions hold. The associated K = (R + BᵀPB)⁻¹BᵀPA is the steady-state LQR gain. The simulator hides the matrix algebra but exposes Q, R, and the resulting K so you can correlate cost weights with cart travel and control effort.
Why does integral action on φ use clamping in PID mode?
A raw integrator on angle error can wind up under saturation, producing huge corrective forces once the actuator finally unsaturates. Clamping the integral state is a simple anti-windup sketch that keeps the demo stable when students crank Ki.