# Unlocking the Power of Model Predictive Control (MPC) in MATLAB

September 30, 2023
Dr. Ethan Caldwell
United Kingdom
Model Predictive Control
Dr. Ethan Caldwell is an accomplished control systems engineer with a Ph.D. in Control Theory from the University of California, Berkeley. He currently serves as a faculty member at Stanford University, where he imparts his extensive knowledge in advanced control strategies to aspiring engineers.

Model Predictive Control (MPC) is a potent control technique widely applied across industries to optimize complex systems while adhering to constraints. Its implementation in MATLAB offers an efficient platform for control system design. This blog aims to provide university students with a theoretical understanding of implementing MPC in MATLAB, enabling them to write their model predictive control assignment using MATLAB effectively for their assignments and projects.

## Understanding Model Predictive Control (MPC)

MPC is a control strategy that continually optimizes control inputs based on a predictive model of the system's behaviour over a defined time horizon. It achieves this by formulating an objective function that balances desired setpoint tracking and constraint satisfaction, making it a versatile choice for complex systems. By employing MPC, engineers can anticipate and adjust to changes in the system, ensuring efficient and robust control.

## Basic Concept of MPC

MPC is an advanced control strategy that minimizes a cost function by predicting system behaviour over a finite time horizon and optimizing control inputs accordingly. Key components include:

• Dynamic System Model: This component is fundamental in MPC as it encapsulates the mathematical representation of the system's behaviour. Typically, this representation is expressed as differential equations or state-space equations. The dynamic model captures how the system evolves, allowing MPC to predict its future behaviour based on current and past states.
• Cost Function: The cost function serves as the performance evaluator in MPC. It quantifies how well the system is performing by assigning a numerical value to it. The goal is to minimize this cost function. It incorporates terms that penalize deviations from desired setpoints, aiming to keep the system as close as possible to its target states. Additionally, it penalizes constraint violations, ensuring that system limits are not exceeded.
• Prediction Horizon: The prediction horizon sets the time span over which the system's future behaviour is anticipated. This horizon defines how far into the future MPC looks to make control decisions. A longer prediction horizon provides more extended foresight but may increase computational complexity. Engineers must strike a balance between prediction accuracy and computational efficiency when selecting the prediction horizon.
• Control Horizon: In contrast to the prediction horizon, the control horizon determines the time span over which control inputs are optimized. It is shorter than the prediction horizon and specifies the time frame during which MPC computes the best control actions to achieve the desired performance while adhering to constraints. Adjusting the control horizon allows for different trade-offs between rapid responses and stability in the control system.

MPC offers several benefits:

• Complex System Handling: MPC is well-suited for systems with multiple inputs and outputs, making it versatile for controlling complex and interconnected processes. It can simultaneously manipulate several control variables to achieve desired performance and ensure system stability.
• Constraint Handling: MPC excels at incorporating constraints on both states and inputs. This means that it can maintain the system within safe operating limits, preventing overshoots, saturation, or other undesirable behaviours. This capability is crucial in various applications, such as chemical processes, where maintaining safe operating conditions is paramount.
• Adaptability: MPC's adaptability is a significant advantage. It can adjust to changing system dynamics or setpoint variations. When the system's behaviour shifts due to external disturbances or other factors, MPC can continuously optimize control inputs to track the new setpoints, ensuring that the system remains responsive and efficient.
• Uncertainty Handling: MPC is also effective at addressing uncertainty in system models. Real-world systems often have modelling inaccuracies or unknown parameters. MPC's predictive nature allows it to cope with such uncertainty by iteratively updating its control strategy based on observed system responses. This makes MPC robust and reliable even when dealing with imperfect models.

## MATLAB for MPC Implementation

MATLAB's versatile environment offers tools for system modelling, optimization, and simulation, making it an ideal choice for students looking to gain hands-on experience with MPC. By leveraging MATLAB's capabilities, students can bridge the gap between theory and practical application, enhancing their understanding of advanced control strategies.

### Setting up the MATLAB Environment

Ensure MATLAB is installed, as it provides toolboxes and functions to simplify control algorithm implementation. MATLAB Simulink is useful for system modelling and simulation.

### System Modeling

Begin by creating a dynamic model of the system, representing how it responds to control inputs and disturbances. Choose modelling approaches like state-space or transfer function models.

### Cost Function Definition

Define the cost function, which evaluates system performance. Include terms for:

• Deviations from setpoints.
• Control effort (input magnitude and rate of change).
• Constraint violations.

The cost function typically depends on both states and control inputs.

## Steps for Implementing MPC in MATLAB

By following these systematic steps, students can effectively harness MATLAB's capabilities to design, simulate, and evaluate MPC controllers, fostering a deeper comprehension of control system engineering principles and practices. This comprehensive approach equips students with the skills necessary to excel in academic assignments and real-world control system applications.

### Discretization of the System Model

Discretize continuous-time models using MATLAB functions like c2d for compatibility with MPC.

### Predicting Future States

Use the discretized model to predict future system states over the prediction horizon. MATLAB's predict or sim functions are suitable.

### Formulating the Optimization Problem

Formulate an optimization problem that minimizes the cost function while satisfying state and control input constraints. MATLAB's optimization toolbox offers various solvers for MPC.

### Solving the Optimization Problem

Solve the optimization problem at each control time step using MATLAB's optimization functions. The solution provides optimal control inputs for the current step.

### Applying Control Inputs

Apply the computed optimal control inputs to the real system using MATLAB's lsim or custom control interfaces.

## Constraint Handling

MPC's ability to handle constraints is a core strength of this control strategy. Constraints on both states (system variables) and control inputs (manipulated variables) can be crucial in practical applications to prevent the system from operating in undesirable regions or exceeding safety limits. In MATLAB, constraints can be implemented by defining constraint matrices and vectors that describe the limits on states and control inputs. These constraints are then incorporated into the optimization problem, ensuring that the MPC controller generates control inputs that respect these limits. This capability is particularly valuable in industries where safety and system integrity are paramount, such as chemical processing, where exceeding temperature or pressure constraints can have serious consequences.

## Simulating MPC Performance

Simulating MPC performance in MATLAB is a crucial step in assessing how well the designed controller functions in a closed-loop system. During simulation, you feed the MPC controller with reference setpoints and disturbances to observe how it responds. MATLAB provides tools for modelling the entire closed-loop system, including the dynamic model, controller, and any disturbance models. By conducting simulations, you can assess how effectively the MPC controller tracks setpoints, maintains system stability, and handles disturbances. This process allows you to identify potential issues and refine the controller design before implementing it in a real system, saving time and resources.

## Tuning MPC Parameters

Optimizing MPC performance relies on fine-tuning various parameters within the controller. Key parameters to consider include the prediction horizon (how far into the future the controller looks), the control horizon (the time frame for optimizing control inputs), and the weightings in the cost function (balancing act between tracking setpoints and minimizing control effort). In MATLAB, you can perform parameter tuning using optimization algorithms or by manually adjusting these parameters based on simulation results. Tuning is an iterative process aimed at achieving the desired control performance, such as faster response times, reduced overshoot, or smoother control actions. It is essential to adapt these parameters to specific system characteristics and control objectives to optimize the MPC's effectiveness in different applications.

## Conclusion

Implementing Model Predictive Control (MPC) in MATLAB encompasses critical steps, including system modelling, cost function formulation, optimization problem setup, constraint handling, and parameter tuning. MATLAB's versatile environment equips students with the skills necessary for control system design and optimization, aiding in assignments and real-world applications. Proficiency in MPC implementation empowers students to excel in control system engineering endeavours.