Model Predictive Control Simulator Back
Control Engineering Simulator

Model Predictive Control Simulator — Finite-Horizon Optimization

Discretize a first-order process and compute, at every step, the optimal input that minimizes the predicted output error over a finite horizon. Compare MPC with a baseline PID controller.

Parameters
Prediction horizon N_p
step
Control horizon N_c
step
Input penalty λ
Setpoint r

Process: K = 2, T = 5 s, dt = 0.1 s (fixed). Simulation horizon 15 s. The same plant is also controlled by a PID (Kp = 2, Ki = 0.4) for comparison.

Results
Rise time (0→90%)
Settling time (±2%)
Overshoot
Initial input u(0)
Output y(t), Setpoint r, and Input u(t)

Top: y (blue), r (dashed red), PID response (gray), predicted horizon (dotted blue). Bottom: u (green), Nc optimized inputs (yellow dots).

Theory & Key Formulas

Discretize the first-order process $G_p(s) = K/(T s + 1)$ with a forward Euler step to obtain $x_{k+1} = a\,x_k + b\,u_k$ where $a = e^{-dt/T}$ and $b = K(1-a)$.

Prediction over $N_p$ steps:

$$y_{k+i} = a^{i}\,x_k + \sum_{j=0}^{i-1} a^{i-1-j}\,b\,u_{k+j}$$

Cost (output error + Δu penalty for offset-free tracking):

$$J = \sum_{i=1}^{N_p} (y_{k+i} - r)^2 + \lambda \sum_{j=0}^{N_c - 1} (\Delta u_{k+j})^2$$

Closed-form solution of the unconstrained QP ($\partial J / \partial U = 0$):

$$U = \bigl(H^\top H + \lambda\, L^\top L\bigr)^{-1} \bigl(H^\top (R - F\,x_k) + \lambda\,u_{k-1}\,\mathbf{e}_1\bigr)$$

$H$ ($N_p \times N_c$) and $F$ ($N_p \times 1$) are built from $a, b$, and $L$ is the first-difference operator. Only the first entry $u_k = U[0]$ is applied to the plant (receding horizon) and the optimization is repeated next step.

What is the Model Predictive Control Simulator

🙋
I have heard of "model predictive control", but how is it different from PID? What is wrong with ordinary feedback?
🎓
Roughly speaking, PID only looks at the "current error", but MPC looks at the "error over the next ten steps" and computes the input that minimizes it. It carries an internal model of the process, $x_{k+1} = a x_k + b u_k$, and predicts the future. In the simulator above compare the blue MPC curve with the gray PID curve. On the same plant, MPC has far less wasted overshoot.
🙋
When I push "prediction horizon Np" up to 30, the response gets a bit smoother. What about the computational cost?
🎓
Good question. Raising Np makes the matrix $H$ a fat $N_p \times N_c$ block. This tool uses the closed-form unconstrained-QP solution $U = (H^\top H + \lambda L^\top L)^{-1}(...)$ so only an Nc×Nc inverse is needed. But real constrained MPC runs a QP solver every step, so doubling Np really shoots up the computation. In practice Np ≈ 80–100 percent of the settling time, and Nc = 1 to 3 is usually enough.
🙋
When I push "input penalty λ" up to 10, the response becomes painfully slow. Why do we need that knob at all?
🎓
λ is the penalty for "do not move the input too violently". With λ = 0 the MPC chases the setpoint as fast as possible with huge input swings — on a real plant the valve or the motor cannot keep up. Increasing λ says "take your time, keep the input smooth". Look at the bottom u(t) panel: u(0) differs by an order of magnitude between λ = 0.1 and λ = 10.
🙋
What are those yellow dots in the bottom panel? There are three of them.
🎓
That is the essence of MPC: those are the Nc inputs $u_k, u_{k+1}, u_{k+2}$ computed by the first optimization. But MPC uses the receding-horizon trick — only the first $u_k$ is actually applied to the plant. At the next step a fresh measurement $x_{k+1}$ comes in and Nc new inputs are computed. "Optimize including the future, execute one at a time" — that is what makes MPC robust against disturbances.

Frequently Asked Questions

The two big differences are "future prediction" and "explicit handling of constraints". PID only uses the instantaneous error, its integral and its derivative, while MPC predicts Np steps ahead with a process model and can put input bounds, output safety limits and similar constraints directly into the optimization. It also handles MIMO interactions naturally, which is why it is the de facto standard for refining and chemical plants with dozens of variables. The downsides are high computation cost and the need for an accurate model, so for a simple SISO loop a PID is usually more practical.
Thanks to the receding-horizon scheme, the optimization is redone at every step with a fresh measurement, so moderate model errors are tolerated. If the model and the plant diverge a lot, however, steady-state offset or instability can appear. In practice an "integrating disturbance model" is added to guarantee offset-free tracking, or robust MPC (Tube MPC and friends) is used to handle model uncertainty explicitly. This simulator uses an exact internal model, which is why the tracking looks ideal.
There are two main reasons: reducing the cost of the QP and damping the input. Setting Nc equal to Np blows up the optimization variable count to Np and produces violent input oscillations as Np inputs try to track Np outputs exactly. Picking Nc as small as 1 to 3, with the remaining inputs frozen at $u_{k+N_c-1}$, gives a smooth and easy-to-implement input sequence. This tool builds the $H$ matrix exactly with that "blocking" structure.
In refining and chemicals the leaders are AspenTech's DMCplus (a commercial Dynamic Matrix Control), Shell's SMOC and Honeywell's Profit Controller. They handle MIMO processes with tens to hundreds of inputs and outputs and are typically arranged as a hierarchy with an economic optimization (LP/QP) sitting above the MPC layer. In the automotive industry MPC is used for engine control and autonomous driving, and in medicine for closed-loop insulin delivery in artificial-pancreas systems. MATLAB's Model Predictive Control Toolbox and the Python packages do-mpc and CasADi are also widely used for research and teaching.

Real-World Applications

Refining and chemical plants — supervisory control: This is where MPC is most widespread. On 30- to 100-variable MIMO processes (distillation columns, FCC units, reformers) it simultaneously keeps product purity, temperature and pressure inside target ranges while economically optimizing energy use and feedstock cost. A typical architecture has MPC setting the setpoints of an underlying PID layer, and most of the world's refining capacity runs under MPC supervision.

Automotive engine and powertrain control: MPC controls airflow on turbocharged engines, energy management on hybrid vehicles, and trajectory tracking and collision avoidance on autonomous vehicles. For vehicle dynamics it can blend steering, throttle, braking and torque vectoring at sample times of about 100 ms while explicitly handling tyre friction limits as constraints.

Power systems and renewables: MPC is applied to microgrid demand-supply balancing, hybrid wind/solar/battery systems and HVDC interconnections. Weather forecasts can be folded into the prediction horizon for an optimal operation that uses hour-ahead generation and load forecasts. Building HVAC control with weather-aware MPC is another active area with proven energy savings.

Medical and biological: Closed-loop insulin delivery for diabetes (the artificial pancreas) has reached real-world deployment based on MPC. A blood-glucose model predicts dozens of minutes ahead, and the insulin dose that keeps the patient in range while avoiding hypoglycemia is computed. FDA-approved products such as Tandem Control-IQ are representative and have meaningfully improved patient quality of life.

Common Misconceptions and Cautions

The most common misconception is to assume that "a longer prediction horizon Np always gives better control". In practice, an Np that is too long lets distant prediction errors dominate the current decision, leading to myopic responses or numerically ill-conditioned QPs. The rule of thumb is to size Np at 80 to 100 percent of the open-loop settling time (the time to reach 95 percent of steady state, divided by dt). In this tool, pushing Np to 30 barely improves anything because Np = 10 already covers 80 percent of the settling time.

The second common error is to assume that λ = 0 gives the fastest possible response. It is true that λ = 0 minimizes only the output error and is in some sense fastest, but the initial input u(0) blows up and a real actuator will saturate or wear out fast. Worse, with λ = 0 the controller becomes hypersensitive to model errors and measurement noise, and the input rings violently. A working rule is to start λ around the squared process gain ($K^2$) and tune downward while watching the input trace. Set λ to 0 in this tool and you can clearly see u(0) jump.

Finally, this tool simulates an idealized case where the internal model and the plant match exactly. Real plants always have unmodelled high-order dynamics, time-varying parameters, disturbances and measurement noise, so the clean tracking and tiny overshoot shown here depend on that idealization. A production implementation must add (1) a state estimator (Kalman filter) to reconstruct the state, (2) an integrating disturbance model for offset-free tracking, and (3) a constrained QP solver that respects safety limits. MPC is a powerful framework, but it demands considerably more "design and tuning know-how" than a PID.