# What Is MPC? | Understanding Model Predictive Control, Part 2

From the series: Understanding Model Predictive Control

*Melda Ulusoy, MathWorks*

Learn how model predictive control (MPC) works. MPC uses a model of the plant to make predictions about future plant outputs. It solves an optimization problem at each time step in order to find the control action that drives the predicted plant output as close to the desired reference as possible.

Using a simple car example, this video provides insight into an MPC controller’s strategy for finding the optimal steering wheel angle to control the car’s longitudinal speed. At each time step, the MPC controller makes predictions about the future lateral positions of the car. To drive the predicted path of the car as close to the reference as possible, the MPC controller formulates the control problem as an optimization problem. It tries to minimize the errors between the lateral positions of the car and the reference as well as steering wheel angle increments. The cost function is represented as a weighted squared sum of these two terms. The solution to this optimization problem provides the optimal steering wheel angle sequence. At the current time step, MPC only applies the first step of this optimal sequence to the car and disregards the rest. In the next time step, it gets a new measurement of the car’s lateral position. The prediction horizon shifts by one time step, and the controller repeats the same cycle to calculate the next optimal steering wheel angle.

In this video, we’ll discuss how model predictive controllers work. In a control problem, the goal of the controller is to calculate the input to the plant such that the plant output follows a desired reference. A model predictive controller’s strategy to compute this input is to predict the future. It sounds like fortune-telling -- but let’s see what it really is.

MPC uses a model of the plant to make predictions about the future plant output behavior. It also uses an optimizer, which ensures that the predicted future plant output tracks the desired reference. The following example walks through these steps.

Let’s say that this is an autonomous car controlled by an MPC controller to keep it in the middle of this lane. MPC can handle MIMO systems like this one -- but for simplicity, we’ll assume that the accelerator is fixed, the car is traveling at a constant speed, and only the steering wheel angle is being adjusted to control the lateral position of the car. We’ll first focus on how the controller uses the car model, and later we’ll discuss the optimizer.

Let’s use this plot to show the lateral position of the car. The vertical axis reflects the current time. Anything behind it reflects the past, and values to the right of it show the future. The middle of the lane is the reference. Let’s use a green line to show it on the plot. At the current time, the MPC controller uses the car model to simulate the car’s path for the next p time steps if the steering wheel would be turned, let’s say, as seen here.

P is a measure of how far ahead MPC looks into the future and is referred to as the prediction horizon. It’s often represented by the length of time into the future or the number of future time steps. The MPC controller needs to find the best predicted path that is the closest to the reference. So, it simulates multiple future scenarios like this. However, it doesn’t do these simulations in a random order; instead, it does it in a systematic way. And this is where the optimizer comes into the picture. By solving an optimization problem, the MPC controller tries to minimize the error between the reference and predicted path of the car. It also tries to minimize the change in the steering wheel angle from one time step to the next. Because if the steering wheel is turned sharply, the ride may become uncomfortable for the passengers, especially if their stomachs are full. The cost function J of this optimization problem includes both these terms and is represented as a weighted squared sum of the predicted errors and steering wheel angle increments.

While minimizing this cost function, MPC also makes sure that the steering wheel angle and car’s position stay within prescribed limits. These are referred to as constraints. For example, there’s a limit on how far the steering wheel can be turned. Another constraint is on the car’s position. In order to prevent accidents, the car is supposed to stay within the lane without going off the road.

At the current time step, the MPC controller is solving the optimization problem over the prediction horizon while satisfying the constraints. The predicted path with the smallest J gives the optimal solution, and therefore determines the optimal steering wheel angle sequence that will get the car as close as possible to the reference. At the current time step, MPC applies only the first step of this optimal sequence to the car and disregards the rest. Based on the applied steering wheel angle, the car travels some distance. At the next time step, the controller gets a new measurement of the car’s lateral position. It might be slightly different than what the MPC controller has predicted before. This could be due to some unmeasured disturbance acting on the car. For example, it might be the wind or slippery road surface. Now the prediction horizon shifts forward by one time step and the controller repeats the same cycle of calculations to compute the optimal steering wheel angle for the next time step. Because of the forward-moving nature of the prediction horizon, MPC is also referred to as receding horizon control.

Here we’ve discussed MPC’s working principles using the car example, but in textbooks you’ll find generic terminology used for MPC. For example, the signals computed by the controller and sent to the plant are referred to as manipulated variables, whereas the plant outputs are called output variables. You may also notice a state estimator in the feedback diagram. In the car example, we’ve assumed that the lateral position of the car can be measured. But if you cannot directly measure the states of a system, they can be estimated by a state estimator and fed back to the MPC controller. To find out more on state estimators, check out the Controls Tech Talk series on Kalman filters.

In this video, we’ve used the car example to demonstrate how MPC uses the car model and an optimizer to adjust steering wheel angles such that the car follows the desired reference. In the next video, we’ll discuss MPC design parameters such as controller sample time, prediction and control horizons, constraints, and weights.

#### Related Products

#### Learn More

Select a Web Site

Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .

You can also select a web site from the following list

## How to Get Best Site Performance

Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.

### Americas

- América Latina (Español)
- Canada (English)
- United States (English)

### Europe

- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)

- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)

### Asia Pacific

- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)