{"id":115,"date":"2024-12-10T19:12:36","date_gmt":"2024-12-10T19:12:36","guid":{"rendered":"https:\/\/libraryresources.nse.org.ng\/robotics\/chapter\/model-predictive-control\/"},"modified":"2026-03-16T14:23:57","modified_gmt":"2026-03-16T14:23:57","slug":"model-predictive-control","status":"publish","type":"chapter","link":"https:\/\/libraryresources.nse.org.ng\/robotics\/chapter\/model-predictive-control\/","title":{"raw":"Model Predictive Controller","rendered":"Model Predictive Controller"},"content":{"raw":"<div>[latexpage]<\/div>\n<h1><strong>1) Overview of Model Predictive Control<\/strong><\/h1>\n<div>A Model Predicative Controller (MPC) is an optimal controller. This controller is used in practical control applications to not only control a system but also thoroughly consider the constraints of the system. MPC has been extensively used for process industries, power systems, automotive, and aerospace engineering. Interestingly, MPC also accommodates filtering the noise in a system to ensure noises would not adversely impact the system control performance.<\/div>\n<div>$\\newline$<\/div>\n<div>\n<div>A discrete state-space model for MPC is typically represented as:<\/div>\n<div>$ x(k+1) &amp;= Ax(k) + Bu(k) + B_d d(k) + w(k) $<\/div>\n<div>$ y(k) &amp;= Cx(k) + v(k) $<\/div>\n<div><\/div>\n<div>\u00a0where:<\/div>\n<\/div>\n<ul>\n \t<li>x(k) is the state vector at time step k .<\/li>\n \t<li>u(k) is the control input vector.<\/li>\n \t<li>d(k) is the disturbance vector.<\/li>\n \t<li>\n<div>y(k) is the output vector.<\/div><\/li>\n \t<li>A, B, Bd, and C are matrices that define the system dynamics.<\/li>\n \t<li>w(k) and v(k) are process and measurement noise, respectively.<\/li>\n<\/ul>\n<div>Figure 14.1 shows an example of random noises of a system.<\/div>\n<div><\/div>\n<div>\n\n[caption id=\"attachment_107\" align=\"aligncenter\" width=\"492\"]<img class=\"wp-image-107\" src=\"https:\/\/libraryresources.nse.org.ng\/wp-content\/uploads\/sites\/18\/2024\/08\/chp501-300x131.png\" alt=\"A control system with random noises.\" width=\"492\" height=\"215\"> Figure 14.1: A control system with random noises.[\/caption]\n\n<\/div>\n<div><\/div>\n<div>Such a discrete state space model is similar to the state space modeled previously. A notable difference, however, does exist. In contrast to the previous method, where the noises of the system are not explicitly considered, with this model, the noises of the system (including <em>w(k)<\/em>, the process noise, and <em>v(k)<\/em>, the measurement noise), are well considered.<\/div>\n<div>$\\newline$<\/div>\n<div>\n<div>Of these two different types of noises considered, the process noises\u00a0<em>w(k)<\/em> are the noises embedded in the system. These can be illustrated through an example. Given an autonomous driving system, the system noise refers to the road surface variation, wind gusts, sensor inaccuracies, and actuator imperfections. On the other hand, an example of the measurement noises <em>v(k) <\/em>include the random errors or disturbances that affect the accuracy of measurements taken by sensors or instruments. More specifically, the measurements noises include thermal measurement noise, quantization noise (for the digital systems when continuous signals are converted to the discrete digital values.), the shot noise (noises associated with electric charge), environmental noise (such as electromagnetic interference),\u00a0 interference from other signals (such as radio frequency interference (RFI) and electromagnetic interference (EMI)), and human error.<\/div>\n<\/div>\n<h1><strong>2) Using a Kalman Filter to Reduce Noise<\/strong><\/h1>\n<div>In order for MPC to perform optimal control, the noise of the system needs to be removed. MPC achieves noise filtering through the use of a Kalman filter or extended state observer (ESO). In our book, we will focus on the use of a Kalman filter to estimate the states and disturbances. To use a Kalman filter to extract noise from the system, the following the steps are needed.<\/div>\n<div>$\\newline$<\/div>\n<div>The MPC is an optimal controller. Therefore, it involves a few steps to solve the optimization problem.<\/div>\n<div>\n<h3>(A) Prediction Step:<\/h3>\n<div>Predict the next state: $$ \\hat{x}(k+1|k) = A \\hat{x}(k|k) + B u(k) $$<\/div>\n<div>Predict the error covariance: $$ P(k+1|k) = A P(k|k) A^T + Q $$<\/div>\n<div><\/div>\n<div>Here, P is the error covariance matrix and Q is the process noise covariance.<\/div>\n<h3>(B) Update Step:<\/h3>\n<div>Compute the Kalman gain: $$ K(k) = P(k|k-1) C^T (C P(k|k-1) C^T + R)^{-1} $$<\/div>\n<div>Update the state estimate: $$ \\hat{x}(k|k) = \\hat{x}(k|k-1) + K(k) (y(k) - C \\hat{x}(k|k-1)) $$<\/div>\n<div>Update the error covariance matrix: $$ P(k|k) = (I - K(k) C) P(k|k-1) $$<\/div>\n<div><\/div>\n<div>Here, R is the measurement noise covariance.<\/div>\n<div>$\\newline$<\/div>\n<\/div>\n<div>The key idea of the Kalman filter is to compute the Kalman gain <em>K<\/em> and update the state estimate by the introduction of the error of covariance. Following the precise estimate of the state, it becomes possible to reject the noises from the system.<\/div>\n<div><\/div>\n<div><\/div>\n<div>\n\n[caption id=\"attachment_108\" align=\"aligncenter\" width=\"483\"]<img class=\"wp-image-108\" src=\"https:\/\/libraryresources.nse.org.ng\/wp-content\/uploads\/sites\/18\/2026\/03\/chp502-300x131.png\" alt=\"The control system with random noises removed.\" width=\"483\" height=\"211\"> Figure 14.2: The control system with random noises removed. The orange curve shown in the figure represents the filtered noises from the system.[\/caption]\n\n<\/div>\n<div><\/div>\n<h1><strong style=\"font-size: 1em\">3) Model Predictive Control (MPC) Modeling<\/strong><\/h1>\n<div>In this section, let us explore MPC modeling without noise. <span style=\"font-size: 1em\">Following the use of the Kalman filter to filter out the noise from the system and estimate the true states, the MPC solver starts to solve the quadratic optimization problem in order to perform the control of the system.\u00a0<\/span><\/div>\n<div>\n<div>$\\newline$<\/div>\n<div>At each time step, MPC solves an optimization problem to determine the optimal control input <em>u(k)<\/em>. The optimization problem can be formulated with cost function <em>J<\/em>.<\/div>\n<\/div>\n<div><span style=\"text-align: initial;font-size: 1em\">$$J = \\sum_{i=0}^{N-1} \\left( (x(k+i) - x_{\\text{ref}}(k+i))^T Q (x(k+i) - x_{\\text{ref}}(k+i)) \\right) + \\lambda \\sum_{i=0}^{N-1} \\left( u(k+i)^T R u(k+i) \\right)$$<\/span><\/div>\n<div>\n\n<span style=\"text-align: initial;font-size: 1em\">subject to the following constraints:<\/span>\n\n<\/div>\n$$ x(k+i+1|k) = A x(k+i|k) + B u(k+i) $$ $$ y(k+i|k) = C x(k+i|k) $$ $$ x_{\\text{min}} \\leq x(k+i|k) \\leq x_{\\text{max}} $$ $$ u_{\\text{min}} \\leq u(k+i) \\leq u_{\\text{max}} $$ where:\n<ul>\n \t<li><em>N<\/em> is the prediction horizon<\/li>\n \t<li>$x_{\\text{ref}}$ is the reference state.<\/li>\n \t<li><em>Q<\/em> and <em>R<\/em> are the weighting matrices for the state and control input, respectively.<\/li>\n \t<li>$ x_{\\text{min}}, x_{\\text{max}}, u_{\\text{min}}, u_{\\text{max}} $ are the state constraints and the control inputs.<\/li>\n<\/ul>\n<h2><strong>(1) First Example: MPC with Kalman Filter Modeling<\/strong><\/h2>\nGiven the state space listed as follows, let's define a simple system with:\n$$\nA = \\begin{bmatrix} 1 &amp; 1 \\\\ 0 &amp; 1 \\end{bmatrix}, \\quad B = \\begin{bmatrix} 0 \\\\ 1 \\end{bmatrix}, \\quad C = \\begin{bmatrix} 1 &amp; 0 \\end{bmatrix}\n$$\n\nThe discrete state space is subject to the following constraints:\n<ul>\n \t<li>Control input constraints: \\( -2 \\leq u(k) \\leq 2 \\)<\/li>\n \t<li>State constraints: \\( -20 \\leq x(k) \\leq 20 \\)<\/li>\n<\/ul>\nWith the above parameters, we will solve the problem using MPC. Note that this example does not consider noises.\n\nWith the above formulation, we need to use a Kalman filter to estimate the state \\( \\hat{x}(k) \\) by observing the values of the measurements \\( y(k) \\).\n\nThe above equation can be easily formulated through the following MPC formulation:\n\n$\nJ = \\sum_{i=0}^{N-1} \\left( (\\hat{x}(k+i) - x_{\\text{ref}}(k+i))^T Q (\\hat{x}(k+i) - x_{\\text{ref}}(k+i)) \\right) + \\lambda \\sum_{i=0}^{N-1} \\left( u(k+i)^T R u(k+i) \\right)\n$\n\nThe solution steps:\n<ol>\n \t<li><strong>Initialization:<\/strong> start with the initial state $x(0)$ and the initial state estimate $\\hat{x}(0)$<\/li>\n \t<li><strong>Kalman Filter Update:<\/strong> For each step, at each time step <em>k<\/em>, update the state estimate using the Kalman filter.<\/li>\n \t<li><strong> Prediction:<\/strong> For each step, predict future states using the state-space model and the updated state estimate.<\/li>\n \t<li><strong> Optimization:<\/strong> Solve the optimization problem to find the optimal control inputs.<\/li>\n \t<li><strong> Apply Control:<\/strong> Apply the first control input $u(k)$ to the system.<\/li>\n \t<li><strong> Update:<\/strong> Update the state $x(k+1)$ using the applied control input and repeat the process.<\/li>\n<\/ol>\nThe solution code is shown below in part (3) Solutions\n<h2><strong>(2) Second Example: MPC with Kalman Filter Modeling<\/strong><\/h2>\nGiven the same system with noises, the state space can presented as follows.\n\n$$\nA = \\begin{bmatrix} 1 &amp; 1 \\\\ 0 &amp; 1 \\end{bmatrix}, \\quad B = \\begin{bmatrix} 0 \\\\ 1 \\end{bmatrix}, \\quad C = \\begin{bmatrix} 1 &amp; 0 \\end{bmatrix}\n$$\nWe'll define the process noise covariance \\( Q_kf \\) and measurement noise covariance \\( R_kf \\) as follows:\n$$\nQ_kf = \\begin{bmatrix} 0.01 &amp; 0 \\\\ 0 &amp; 0.01 \\end{bmatrix}, \\quad R_kf = \\begin{bmatrix} 0.01 \\end{bmatrix}\n$$\n\nFor the noisy system condition, once the process noises and measurement noises are in existence in the system, the cost function should consider it as well. The noises are the process noise covariance \\( Q_kf \\) and measurement noise covariance \\( R_kf \\) as follows:\n\n$\nJ = \\sum_{i=0}^{N-1} \\left( (x(k+i) - x_{\\text{ref}}(k+i))^T Q (x(k+i) - x_{\\text{ref}}(k+i)) + \\text{tr}(Q_kf) \\right) +... \\\\ ...+\\lambda \\sum_{i=0}^{N-1} \\left( u(k+i)^T R u(k+i) + \\text{tr}(R_kf) \\right)\n$\n\nwhere:\n<ul>\n \t<li>$x(k+i)$ is the predicted state at time step $k+i$<\/li>\n \t<li>$ x_{\\text{ref}}(k+i) $ is the reference state at time step $k+i$<\/li>\n \t<li><i>Q<\/i> is the state weighting matrix<\/li>\n \t<li>$u(k+i)$ is the control input at time step $k+i$<\/li>\n \t<li><i>R <\/i>is the control input weighting matrix<\/li>\n \t<li>$\\lambda$ is a weighting factor for the control effort<\/li>\n \t<li>$\\text{tr}(Q_{kf})$ is the trace of the process noise covariance matrix<\/li>\n \t<li>$\\text{tr}(R_{kf}) $ is the trace of the measurement noise covariance matrix<\/li>\n<\/ul>\nThe solution code is shown below in part (3) Solutions\n<h2><strong>(3) Solutions<\/strong><\/h2>\n<strong>Solution to first example:<\/strong> The code used to implement the MPC system without noises is shown in below. More specifically, the given state space without noises can be presented as follows.\n\n[caption id=\"attachment_104\" align=\"aligncenter\" width=\"667\"]<img class=\"wp-image-109\" src=\"https:\/\/libraryresources.nse.org.ng\/wp-content\/uploads\/sites\/18\/2026\/03\/chp506-300x68.png\" alt=\"The cost function of the noiseless MPC system.\" width=\"667\" height=\"151\"> Figure 14.3: The cost function of the noiseless MPC system.[\/caption]\n\n<div><\/div>\n<div><strong>Solution to second example:\u00a0 <\/strong>The noises are added to the system as shown below, both process noise and measurement noise.\u00a0 Note that, the process noise is added to the state equation since the process noise is more related to the physical conditions (states) of the system. However, the measurement noise is added to the output of the state space, since it is not state-related.<\/div>\n<div><\/div>\n\n[caption id=\"attachment_104\" align=\"aligncenter\" width=\"606\"]<img class=\"wp-image-110\" src=\"https:\/\/libraryresources.nse.org.ng\/wp-content\/uploads\/sites\/18\/2026\/03\/chp504-300x94.png\" alt=\"The cost function of the noisy MPC system.\" width=\"606\" height=\"190\"> Figure 14.4: The cost function of the noisy MPC system.[\/caption]\n\nNow that the system has noises, a Kalman filter is needed to filter the noises from the system.\u00a0 The Kalman filter predicates the states, updates the error covariance matrix, and calculates the Kalman gain.\n\n[caption id=\"attachment_104\" align=\"aligncenter\" width=\"493\"]<img class=\"wp-image-111\" src=\"https:\/\/libraryresources.nse.org.ng\/wp-content\/uploads\/sites\/18\/2026\/03\/chp505-300x162.png\" alt=\"Kalman filter to filter the noises\" width=\"493\" height=\"266\"> Figure 14.5: Kalman filter to filter the noises[\/caption]\n\nThe new cost function considering the noises is then used to replace the original cost function.\u00a0 Note that, the new cost function includes <em>Q,<\/em> the process noise covariance, and <em>R,<\/em> the measurement noise covariance.\n<div><\/div>\n\n[caption id=\"attachment_104\" align=\"aligncenter\" width=\"710\"]<img class=\"wp-image-109\" src=\"https:\/\/libraryresources.nse.org.ng\/wp-content\/uploads\/sites\/18\/2026\/03\/chp506-300x68.png\" alt=\"The cost function including the added process and measurement noises.\" width=\"710\" height=\"161\"> Figure 14.6: The cost function including the added process and measurement noises.[\/caption]\n\nFollowing the process above, we can obtain the following state results. The two results correspond to the two different conditions where either no noises exist (Figure 14.7), or process and measurement noises are in existence (Figure 14.8).\n\nEvidently, the results of the states and the input have both satisfied the constraints. Therefore, we can confidently say that the proposed MPC is effective in solving the state space while considering both process and measurement noises in the system.\n\n[caption id=\"attachment_104\" align=\"aligncenter\" width=\"402\"]<img class=\"wp-image-112\" src=\"https:\/\/libraryresources.nse.org.ng\/wp-content\/uploads\/sites\/18\/2026\/03\/chp507-300x223.png\" alt=\"The states of the noiseless system, X1 and X2.\" width=\"402\" height=\"299\"> Figure 14.7: The states of the noiseless system, X1 and X2.[\/caption]\n\n&nbsp;\n\n[caption id=\"attachment_104\" align=\"aligncenter\" width=\"381\"]<img class=\"wp-image-113\" src=\"https:\/\/libraryresources.nse.org.ng\/wp-content\/uploads\/sites\/18\/2026\/03\/chp508-300x225.png\" alt=\"The states of the noisy system, X1 and X2, and noises of the system.\" width=\"381\" height=\"286\"> Figure 14.8: The states of the noisy system, X1 and X2, and noises of the system.[\/caption]\n\n&nbsp;\n\n[caption id=\"attachment_104\" align=\"aligncenter\" width=\"400\"]<img class=\"wp-image-114\" src=\"https:\/\/libraryresources.nse.org.ng\/wp-content\/uploads\/sites\/18\/2026\/03\/chp509-300x222.png\" alt=\"The control input u of the system.\" width=\"400\" height=\"296\"> Figure 14.9: The control input u of the system.[\/caption]\n\n&nbsp;\n\n&nbsp;\n<h1><strong>4) Class Engagement<\/strong><\/h1>\nNow you have learned the MPC theory, lets make summarization of the nature of the MPC controller. <span style=\"text-align: initial;font-size: 1em\">The key thing to implement MPC controller is to have optimal controller to solve the cost function a shown in below.\u00a0 \u00a0Specifically, we use python package cvxpy to minimize the cost function.<\/span>\n\n<span style=\"text-align: initial;font-size: 1em\">$$J = \\sum_{i=0}^{N-1} \\left( (x(k+i) - x_{\\text{ref}}(k+i))^T Q (x(k+i) - x_{\\text{ref}}(k+i)) \\right) + \\lambda \\sum_{i=0}^{N-1} \\left( u(k+i)^T R u(k+i) \\right)$$<\/span>\n\n&nbsp;\n\nPython Code:\n\n<strong>cost += cvxpy.quad_form(x[:, t + 1], Q)<\/strong>\n<strong>cost += cvxpy.quad_form(u[:, t], R)<\/strong>\n\n&nbsp;\n<div class=\"page\" role=\"region\" data-page-number=\"3\" aria-label=\"Page 3\" data-loaded=\"true\">\n<div class=\"textLayer\">\n\nThe constrains needed to be satisfied is\n\n<\/div>\n$$ x(k+i+1|k) = A x(k+i|k) + B u(k+i) $$\n<div class=\"textLayer\">\n\n[x[:, t + 1] == A @ x[:, t] + B @ u[:, t]]\n\n<\/div>\n<\/div>\n&nbsp;\n<div class=\"page\" role=\"region\" data-page-number=\"3\" aria-label=\"Page 3\" data-loaded=\"true\">\n<div class=\"textLayer\">\n\n--------------- The Key Code of MPC in Python --------------\n\n<\/div>\n<\/div>\n<div>\n\nfor t in range(T):\ncost += cvxpy.quad_form(x[:, t + 1], Q)\ncost += cvxpy.quad_form(u[:, t], R)\nconstr += [x[:, t + 1] == A @ x[:, t] + B @ u[:, t]]\n\n<\/div>\nOther Constains\n<div>\n\n$$ x_{\\text{min}} \\leq x(k+i|k) \\leq x_{\\text{max}} $$ $$ u_{\\text{min}} \\leq u(k+i) \\leq u_{\\text{max}} $$ where:\n\nPython Code:\n\n<strong>constr += [x[:, 0] == x0[:, 0]]<\/strong>\n\nThe below is to solve it with cvxpy.Minimize function\n\n<\/div>\n<div>\n\n<strong>prob = cvxpy.Problem(cvxpy.Minimize(cost), constr)<\/strong>\n\n<\/div>\n&nbsp;\n\nWe will use the same class engagment example as shown in the LQR chapter.\u00a0 For this problem, an inverted pendulum sits on a cart moving along the x-axis direction shown in Figure 14.10.\u00a0 The inverted pendulum can move along the X and Y direction. It means the inverted pendulum can swing back and forth shown.\n\n&nbsp;\n\n<img class=\"size-medium wp-image-103 aligncenter\" src=\"https:\/\/libraryresources.nse.org.ng\/wp-content\/uploads\/sites\/18\/2026\/03\/cart-300x222.png\" alt=\"\" width=\"300\" height=\"222\">\n<p style=\"text-align: center\">Figure 14.10 : Example of using Python to compute the LQR control gain K.<\/p>\nIn order to control the inverted pendulum using LQR , we need to model the system.\u00a0 The followings show the process of the modeling\n\n<a href=\"https:\/\/mtmailmtsu-my.sharepoint.com\/:b:\/g\/personal\/hbzhang_mtsu_edu\/Ee-RhYhDtIRJqXFsQKpTBWoBq0Z9ZpVDy6PuTrdSbQzXYg?e=MPBRwJ\">https:\/\/mtmailmtsu-my.sharepoint.com\/:b:\/g\/personal\/hbzhang_mtsu_edu\/Ee-RhYhDtIRJqXFsQKpTBWoBq0Z9ZpVDy6PuTrdSbQzXYg?e=MPBRwJ<\/a>\n\n&nbsp;\n\nThrough the derivation above, you can find the system state space as the following.\n\n\\[\n\\begin{bmatrix}\n\\dot{x} \\\\\n\\ddot{x} \\\\\n\\dot{\\theta} \\\\\n\\ddot{\\theta}\n\\end{bmatrix}\n=\n\\begin{bmatrix}\n0 &amp; 1 &amp; 0 &amp; 0 \\\\\n0 &amp; 0 &amp; -\\frac{gm}{M} &amp; 0 \\\\\n0 &amp; 0 &amp; 0 &amp; 1 \\\\\n0 &amp; 0 &amp; \\frac{g(M+m)}{lM} &amp; 0\n\\end{bmatrix}\n\\begin{bmatrix}\nx \\\\\n\\dot{x} \\\\\n\\theta \\\\\n\\dot{\\theta}\n\\end{bmatrix}\n+\n\\begin{bmatrix}\n0 \\\\\n\\frac{1}{M} \\\\\n0 \\\\\n\\frac{1}{lM}\n\\end{bmatrix}\nF\n\\]\n\nFollowing running the code, results show that the use of LQR is able to stabilize the cart motion as shown in Figure 14.11. Specifically, when the cart is moving from one side to another, the inverted pendulum stays upright.\n\n<img class=\"wp-image-104 aligncenter\" src=\"https:\/\/libraryresources.nse.org.ng\/wp-content\/uploads\/sites\/18\/2026\/03\/cartanimation-300x81.png\" alt=\"\" width=\"619\" height=\"167\">\n<p style=\"text-align: center\">Figure 14.11:\u00a0 The results of running MPC python code showing that MPC is able to stablize the inverted pendulum on the moving cart.<\/p>\n&nbsp;\n\n&nbsp;\n\n&nbsp;\n\n&nbsp;","rendered":"<div>[latexpage]<\/div>\n<h1><strong>1) Overview of Model Predictive Control<\/strong><\/h1>\n<div>A Model Predicative Controller (MPC) is an optimal controller. This controller is used in practical control applications to not only control a system but also thoroughly consider the constraints of the system. MPC has been extensively used for process industries, power systems, automotive, and aerospace engineering. Interestingly, MPC also accommodates filtering the noise in a system to ensure noises would not adversely impact the system control performance.<\/div>\n<div>$\\newline$<\/div>\n<div>\n<div>A discrete state-space model for MPC is typically represented as:<\/div>\n<div>$ x(k+1) &amp;= Ax(k) + Bu(k) + B_d d(k) + w(k) $<\/div>\n<div>$ y(k) &amp;= Cx(k) + v(k) $<\/div>\n<div><\/div>\n<div>\u00a0where:<\/div>\n<\/div>\n<ul>\n<li>x(k) is the state vector at time step k .<\/li>\n<li>u(k) is the control input vector.<\/li>\n<li>d(k) is the disturbance vector.<\/li>\n<li>\n<div>y(k) is the output vector.<\/div>\n<\/li>\n<li>A, B, Bd, and C are matrices that define the system dynamics.<\/li>\n<li>w(k) and v(k) are process and measurement noise, respectively.<\/li>\n<\/ul>\n<div>Figure 14.1 shows an example of random noises of a system.<\/div>\n<div><\/div>\n<div>\n<figure id=\"attachment_107\" aria-describedby=\"caption-attachment-107\" style=\"width: 492px\" class=\"wp-caption aligncenter\"><img decoding=\"async\" class=\"wp-image-107\" src=\"https:\/\/libraryresources.nse.org.ng\/wp-content\/uploads\/sites\/18\/2024\/08\/chp501-300x131.png\" alt=\"A control system with random noises.\" width=\"492\" height=\"215\" srcset=\"https:\/\/libraryresources.nse.org.ng\/robotics\/wp-content\/uploads\/sites\/18\/2024\/08\/chp501-300x131.png 300w, https:\/\/libraryresources.nse.org.ng\/robotics\/wp-content\/uploads\/sites\/18\/2024\/08\/chp501-1024x447.png 1024w, https:\/\/libraryresources.nse.org.ng\/robotics\/wp-content\/uploads\/sites\/18\/2024\/08\/chp501-768x336.png 768w, https:\/\/libraryresources.nse.org.ng\/robotics\/wp-content\/uploads\/sites\/18\/2024\/08\/chp501-65x28.png 65w, https:\/\/libraryresources.nse.org.ng\/robotics\/wp-content\/uploads\/sites\/18\/2024\/08\/chp501-225x98.png 225w, https:\/\/libraryresources.nse.org.ng\/robotics\/wp-content\/uploads\/sites\/18\/2024\/08\/chp501-350x153.png 350w, https:\/\/libraryresources.nse.org.ng\/robotics\/wp-content\/uploads\/sites\/18\/2024\/08\/chp501.png 1126w\" sizes=\"(max-width: 492px) 100vw, 492px\" \/><figcaption id=\"caption-attachment-107\" class=\"wp-caption-text\">Figure 14.1: A control system with random noises.<\/figcaption><\/figure>\n<\/div>\n<div><\/div>\n<div>Such a discrete state space model is similar to the state space modeled previously. A notable difference, however, does exist. In contrast to the previous method, where the noises of the system are not explicitly considered, with this model, the noises of the system (including <em>w(k)<\/em>, the process noise, and <em>v(k)<\/em>, the measurement noise), are well considered.<\/div>\n<div>$\\newline$<\/div>\n<div>\n<div>Of these two different types of noises considered, the process noises\u00a0<em>w(k)<\/em> are the noises embedded in the system. These can be illustrated through an example. Given an autonomous driving system, the system noise refers to the road surface variation, wind gusts, sensor inaccuracies, and actuator imperfections. On the other hand, an example of the measurement noises <em>v(k) <\/em>include the random errors or disturbances that affect the accuracy of measurements taken by sensors or instruments. More specifically, the measurements noises include thermal measurement noise, quantization noise (for the digital systems when continuous signals are converted to the discrete digital values.), the shot noise (noises associated with electric charge), environmental noise (such as electromagnetic interference),\u00a0 interference from other signals (such as radio frequency interference (RFI) and electromagnetic interference (EMI)), and human error.<\/div>\n<\/div>\n<h1><strong>2) Using a Kalman Filter to Reduce Noise<\/strong><\/h1>\n<div>In order for MPC to perform optimal control, the noise of the system needs to be removed. MPC achieves noise filtering through the use of a Kalman filter or extended state observer (ESO). In our book, we will focus on the use of a Kalman filter to estimate the states and disturbances. To use a Kalman filter to extract noise from the system, the following the steps are needed.<\/div>\n<div>$\\newline$<\/div>\n<div>The MPC is an optimal controller. Therefore, it involves a few steps to solve the optimization problem.<\/div>\n<div>\n<h3>(A) Prediction Step:<\/h3>\n<div>Predict the next state: $$ \\hat{x}(k+1|k) = A \\hat{x}(k|k) + B u(k) $$<\/div>\n<div>Predict the error covariance: $$ P(k+1|k) = A P(k|k) A^T + Q $$<\/div>\n<div><\/div>\n<div>Here, P is the error covariance matrix and Q is the process noise covariance.<\/div>\n<h3>(B) Update Step:<\/h3>\n<div>Compute the Kalman gain: $$ K(k) = P(k|k-1) C^T (C P(k|k-1) C^T + R)^{-1} $$<\/div>\n<div>Update the state estimate: $$ \\hat{x}(k|k) = \\hat{x}(k|k-1) + K(k) (y(k) &#8211; C \\hat{x}(k|k-1)) $$<\/div>\n<div>Update the error covariance matrix: $$ P(k|k) = (I &#8211; K(k) C) P(k|k-1) $$<\/div>\n<div><\/div>\n<div>Here, R is the measurement noise covariance.<\/div>\n<div>$\\newline$<\/div>\n<\/div>\n<div>The key idea of the Kalman filter is to compute the Kalman gain <em>K<\/em> and update the state estimate by the introduction of the error of covariance. Following the precise estimate of the state, it becomes possible to reject the noises from the system.<\/div>\n<div><\/div>\n<div><\/div>\n<div>\n<figure id=\"attachment_108\" aria-describedby=\"caption-attachment-108\" style=\"width: 483px\" class=\"wp-caption aligncenter\"><img decoding=\"async\" class=\"wp-image-108\" src=\"https:\/\/libraryresources.nse.org.ng\/wp-content\/uploads\/sites\/18\/2026\/03\/chp502-300x131.png\" alt=\"The control system with random noises removed.\" width=\"483\" height=\"211\" srcset=\"https:\/\/libraryresources.nse.org.ng\/robotics\/wp-content\/uploads\/sites\/18\/2026\/03\/chp502-300x131.png 300w, https:\/\/libraryresources.nse.org.ng\/robotics\/wp-content\/uploads\/sites\/18\/2026\/03\/chp502-1024x447.png 1024w, https:\/\/libraryresources.nse.org.ng\/robotics\/wp-content\/uploads\/sites\/18\/2026\/03\/chp502-768x335.png 768w, https:\/\/libraryresources.nse.org.ng\/robotics\/wp-content\/uploads\/sites\/18\/2026\/03\/chp502-65x28.png 65w, https:\/\/libraryresources.nse.org.ng\/robotics\/wp-content\/uploads\/sites\/18\/2026\/03\/chp502-225x98.png 225w, https:\/\/libraryresources.nse.org.ng\/robotics\/wp-content\/uploads\/sites\/18\/2026\/03\/chp502-350x153.png 350w, https:\/\/libraryresources.nse.org.ng\/robotics\/wp-content\/uploads\/sites\/18\/2026\/03\/chp502.png 1122w\" sizes=\"(max-width: 483px) 100vw, 483px\" \/><figcaption id=\"caption-attachment-108\" class=\"wp-caption-text\">Figure 14.2: The control system with random noises removed. The orange curve shown in the figure represents the filtered noises from the system.<\/figcaption><\/figure>\n<\/div>\n<div><\/div>\n<h1><strong style=\"font-size: 1em\">3) Model Predictive Control (MPC) Modeling<\/strong><\/h1>\n<div>In this section, let us explore MPC modeling without noise. <span style=\"font-size: 1em\">Following the use of the Kalman filter to filter out the noise from the system and estimate the true states, the MPC solver starts to solve the quadratic optimization problem in order to perform the control of the system.\u00a0<\/span><\/div>\n<div>\n<div>$\\newline$<\/div>\n<div>At each time step, MPC solves an optimization problem to determine the optimal control input <em>u(k)<\/em>. The optimization problem can be formulated with cost function <em>J<\/em>.<\/div>\n<\/div>\n<div><span style=\"text-align: initial;font-size: 1em\">$$J = \\sum_{i=0}^{N-1} \\left( (x(k+i) &#8211; x_{\\text{ref}}(k+i))^T Q (x(k+i) &#8211; x_{\\text{ref}}(k+i)) \\right) + \\lambda \\sum_{i=0}^{N-1} \\left( u(k+i)^T R u(k+i) \\right)$$<\/span><\/div>\n<div>\n<p><span style=\"text-align: initial;font-size: 1em\">subject to the following constraints:<\/span><\/p>\n<\/div>\n<p>$$ x(k+i+1|k) = A x(k+i|k) + B u(k+i) $$ $$ y(k+i|k) = C x(k+i|k) $$ $$ x_{\\text{min}} \\leq x(k+i|k) \\leq x_{\\text{max}} $$ $$ u_{\\text{min}} \\leq u(k+i) \\leq u_{\\text{max}} $$ where:<\/p>\n<ul>\n<li><em>N<\/em> is the prediction horizon<\/li>\n<li>$x_{\\text{ref}}$ is the reference state.<\/li>\n<li><em>Q<\/em> and <em>R<\/em> are the weighting matrices for the state and control input, respectively.<\/li>\n<li>$ x_{\\text{min}}, x_{\\text{max}}, u_{\\text{min}}, u_{\\text{max}} $ are the state constraints and the control inputs.<\/li>\n<\/ul>\n<h2><strong>(1) First Example: MPC with Kalman Filter Modeling<\/strong><\/h2>\n<p>Given the state space listed as follows, let&#8217;s define a simple system with:<br \/>\n$$<br \/>\nA = \\begin{bmatrix} 1 &amp; 1 \\\\ 0 &amp; 1 \\end{bmatrix}, \\quad B = \\begin{bmatrix} 0 \\\\ 1 \\end{bmatrix}, \\quad C = \\begin{bmatrix} 1 &amp; 0 \\end{bmatrix}<br \/>\n$$<\/p>\n<p>The discrete state space is subject to the following constraints:<\/p>\n<ul>\n<li>Control input constraints: \\( -2 \\leq u(k) \\leq 2 \\)<\/li>\n<li>State constraints: \\( -20 \\leq x(k) \\leq 20 \\)<\/li>\n<\/ul>\n<p>With the above parameters, we will solve the problem using MPC. Note that this example does not consider noises.<\/p>\n<p>With the above formulation, we need to use a Kalman filter to estimate the state \\( \\hat{x}(k) \\) by observing the values of the measurements \\( y(k) \\).<\/p>\n<p>The above equation can be easily formulated through the following MPC formulation:<\/p>\n<p>$<br \/>\nJ = \\sum_{i=0}^{N-1} \\left( (\\hat{x}(k+i) &#8211; x_{\\text{ref}}(k+i))^T Q (\\hat{x}(k+i) &#8211; x_{\\text{ref}}(k+i)) \\right) + \\lambda \\sum_{i=0}^{N-1} \\left( u(k+i)^T R u(k+i) \\right)<br \/>\n$<\/p>\n<p>The solution steps:<\/p>\n<ol>\n<li><strong>Initialization:<\/strong> start with the initial state $x(0)$ and the initial state estimate $\\hat{x}(0)$<\/li>\n<li><strong>Kalman Filter Update:<\/strong> For each step, at each time step <em>k<\/em>, update the state estimate using the Kalman filter.<\/li>\n<li><strong> Prediction:<\/strong> For each step, predict future states using the state-space model and the updated state estimate.<\/li>\n<li><strong> Optimization:<\/strong> Solve the optimization problem to find the optimal control inputs.<\/li>\n<li><strong> Apply Control:<\/strong> Apply the first control input $u(k)$ to the system.<\/li>\n<li><strong> Update:<\/strong> Update the state $x(k+1)$ using the applied control input and repeat the process.<\/li>\n<\/ol>\n<p>The solution code is shown below in part (3) Solutions<\/p>\n<h2><strong>(2) Second Example: MPC with Kalman Filter Modeling<\/strong><\/h2>\n<p>Given the same system with noises, the state space can presented as follows.<\/p>\n<p>$$<br \/>\nA = \\begin{bmatrix} 1 &amp; 1 \\\\ 0 &amp; 1 \\end{bmatrix}, \\quad B = \\begin{bmatrix} 0 \\\\ 1 \\end{bmatrix}, \\quad C = \\begin{bmatrix} 1 &amp; 0 \\end{bmatrix}<br \/>\n$$<br \/>\nWe&#8217;ll define the process noise covariance \\( Q_kf \\) and measurement noise covariance \\( R_kf \\) as follows:<br \/>\n$$<br \/>\nQ_kf = \\begin{bmatrix} 0.01 &amp; 0 \\\\ 0 &amp; 0.01 \\end{bmatrix}, \\quad R_kf = \\begin{bmatrix} 0.01 \\end{bmatrix}<br \/>\n$$<\/p>\n<p>For the noisy system condition, once the process noises and measurement noises are in existence in the system, the cost function should consider it as well. The noises are the process noise covariance \\( Q_kf \\) and measurement noise covariance \\( R_kf \\) as follows:<\/p>\n<p>$<br \/>\nJ = \\sum_{i=0}^{N-1} \\left( (x(k+i) &#8211; x_{\\text{ref}}(k+i))^T Q (x(k+i) &#8211; x_{\\text{ref}}(k+i)) + \\text{tr}(Q_kf) \\right) +&#8230; \\\\ &#8230;+\\lambda \\sum_{i=0}^{N-1} \\left( u(k+i)^T R u(k+i) + \\text{tr}(R_kf) \\right)<br \/>\n$<\/p>\n<p>where:<\/p>\n<ul>\n<li>$x(k+i)$ is the predicted state at time step $k+i$<\/li>\n<li>$ x_{\\text{ref}}(k+i) $ is the reference state at time step $k+i$<\/li>\n<li><i>Q<\/i> is the state weighting matrix<\/li>\n<li>$u(k+i)$ is the control input at time step $k+i$<\/li>\n<li><i>R <\/i>is the control input weighting matrix<\/li>\n<li>$\\lambda$ is a weighting factor for the control effort<\/li>\n<li>$\\text{tr}(Q_{kf})$ is the trace of the process noise covariance matrix<\/li>\n<li>$\\text{tr}(R_{kf}) $ is the trace of the measurement noise covariance matrix<\/li>\n<\/ul>\n<p>The solution code is shown below in part (3) Solutions<\/p>\n<h2><strong>(3) Solutions<\/strong><\/h2>\n<p><strong>Solution to first example:<\/strong> The code used to implement the MPC system without noises is shown in below. More specifically, the given state space without noises can be presented as follows.<\/p>\n<figure id=\"attachment_104\" aria-describedby=\"caption-attachment-104\" style=\"width: 667px\" class=\"wp-caption aligncenter\"><img decoding=\"async\" class=\"wp-image-109\" src=\"https:\/\/libraryresources.nse.org.ng\/wp-content\/uploads\/sites\/18\/2026\/03\/chp506-300x68.png\" alt=\"The cost function of the noiseless MPC system.\" width=\"667\" height=\"151\" srcset=\"https:\/\/libraryresources.nse.org.ng\/robotics\/wp-content\/uploads\/sites\/18\/2026\/03\/chp506-300x68.png 300w, https:\/\/libraryresources.nse.org.ng\/robotics\/wp-content\/uploads\/sites\/18\/2026\/03\/chp506-1024x231.png 1024w, https:\/\/libraryresources.nse.org.ng\/robotics\/wp-content\/uploads\/sites\/18\/2026\/03\/chp506-768x173.png 768w, https:\/\/libraryresources.nse.org.ng\/robotics\/wp-content\/uploads\/sites\/18\/2026\/03\/chp506-1536x346.png 1536w, https:\/\/libraryresources.nse.org.ng\/robotics\/wp-content\/uploads\/sites\/18\/2026\/03\/chp506-65x15.png 65w, https:\/\/libraryresources.nse.org.ng\/robotics\/wp-content\/uploads\/sites\/18\/2026\/03\/chp506-225x51.png 225w, https:\/\/libraryresources.nse.org.ng\/robotics\/wp-content\/uploads\/sites\/18\/2026\/03\/chp506-350x79.png 350w, https:\/\/libraryresources.nse.org.ng\/robotics\/wp-content\/uploads\/sites\/18\/2026\/03\/chp506.png 1670w\" sizes=\"(max-width: 667px) 100vw, 667px\" \/><figcaption id=\"caption-attachment-104\" class=\"wp-caption-text\">Figure 14.3: The cost function of the noiseless MPC system.<\/figcaption><\/figure>\n<div><\/div>\n<div><strong>Solution to second example:\u00a0 <\/strong>The noises are added to the system as shown below, both process noise and measurement noise.\u00a0 Note that, the process noise is added to the state equation since the process noise is more related to the physical conditions (states) of the system. However, the measurement noise is added to the output of the state space, since it is not state-related.<\/div>\n<div><\/div>\n<figure id=\"attachment_104\" aria-describedby=\"caption-attachment-104\" style=\"width: 606px\" class=\"wp-caption aligncenter\"><img decoding=\"async\" class=\"wp-image-110\" src=\"https:\/\/libraryresources.nse.org.ng\/wp-content\/uploads\/sites\/18\/2026\/03\/chp504-300x94.png\" alt=\"The cost function of the noisy MPC system.\" width=\"606\" height=\"190\" srcset=\"https:\/\/libraryresources.nse.org.ng\/robotics\/wp-content\/uploads\/sites\/18\/2026\/03\/chp504-300x94.png 300w, https:\/\/libraryresources.nse.org.ng\/robotics\/wp-content\/uploads\/sites\/18\/2026\/03\/chp504-1024x321.png 1024w, https:\/\/libraryresources.nse.org.ng\/robotics\/wp-content\/uploads\/sites\/18\/2026\/03\/chp504-768x241.png 768w, https:\/\/libraryresources.nse.org.ng\/robotics\/wp-content\/uploads\/sites\/18\/2026\/03\/chp504-65x20.png 65w, https:\/\/libraryresources.nse.org.ng\/robotics\/wp-content\/uploads\/sites\/18\/2026\/03\/chp504-225x71.png 225w, https:\/\/libraryresources.nse.org.ng\/robotics\/wp-content\/uploads\/sites\/18\/2026\/03\/chp504-350x110.png 350w, https:\/\/libraryresources.nse.org.ng\/robotics\/wp-content\/uploads\/sites\/18\/2026\/03\/chp504.png 1186w\" sizes=\"(max-width: 606px) 100vw, 606px\" \/><figcaption id=\"caption-attachment-104\" class=\"wp-caption-text\">Figure 14.4: The cost function of the noisy MPC system.<\/figcaption><\/figure>\n<p>Now that the system has noises, a Kalman filter is needed to filter the noises from the system.\u00a0 The Kalman filter predicates the states, updates the error covariance matrix, and calculates the Kalman gain.<\/p>\n<figure id=\"attachment_104\" aria-describedby=\"caption-attachment-104\" style=\"width: 493px\" class=\"wp-caption aligncenter\"><img decoding=\"async\" class=\"wp-image-111\" src=\"https:\/\/libraryresources.nse.org.ng\/wp-content\/uploads\/sites\/18\/2026\/03\/chp505-300x162.png\" alt=\"Kalman filter to filter the noises\" width=\"493\" height=\"266\" srcset=\"https:\/\/libraryresources.nse.org.ng\/robotics\/wp-content\/uploads\/sites\/18\/2026\/03\/chp505-300x162.png 300w, https:\/\/libraryresources.nse.org.ng\/robotics\/wp-content\/uploads\/sites\/18\/2026\/03\/chp505-768x416.png 768w, https:\/\/libraryresources.nse.org.ng\/robotics\/wp-content\/uploads\/sites\/18\/2026\/03\/chp505-65x35.png 65w, https:\/\/libraryresources.nse.org.ng\/robotics\/wp-content\/uploads\/sites\/18\/2026\/03\/chp505-225x122.png 225w, https:\/\/libraryresources.nse.org.ng\/robotics\/wp-content\/uploads\/sites\/18\/2026\/03\/chp505-350x189.png 350w, https:\/\/libraryresources.nse.org.ng\/robotics\/wp-content\/uploads\/sites\/18\/2026\/03\/chp505.png 972w\" sizes=\"(max-width: 493px) 100vw, 493px\" \/><figcaption id=\"caption-attachment-104\" class=\"wp-caption-text\">Figure 14.5: Kalman filter to filter the noises<\/figcaption><\/figure>\n<p>The new cost function considering the noises is then used to replace the original cost function.\u00a0 Note that, the new cost function includes <em>Q,<\/em> the process noise covariance, and <em>R,<\/em> the measurement noise covariance.<\/p>\n<div><\/div>\n<figure id=\"attachment_104\" aria-describedby=\"caption-attachment-104\" style=\"width: 710px\" class=\"wp-caption aligncenter\"><img decoding=\"async\" class=\"wp-image-109\" src=\"https:\/\/libraryresources.nse.org.ng\/wp-content\/uploads\/sites\/18\/2026\/03\/chp506-300x68.png\" alt=\"The cost function including the added process and measurement noises.\" width=\"710\" height=\"161\" srcset=\"https:\/\/libraryresources.nse.org.ng\/robotics\/wp-content\/uploads\/sites\/18\/2026\/03\/chp506-300x68.png 300w, https:\/\/libraryresources.nse.org.ng\/robotics\/wp-content\/uploads\/sites\/18\/2026\/03\/chp506-1024x231.png 1024w, https:\/\/libraryresources.nse.org.ng\/robotics\/wp-content\/uploads\/sites\/18\/2026\/03\/chp506-768x173.png 768w, https:\/\/libraryresources.nse.org.ng\/robotics\/wp-content\/uploads\/sites\/18\/2026\/03\/chp506-1536x346.png 1536w, https:\/\/libraryresources.nse.org.ng\/robotics\/wp-content\/uploads\/sites\/18\/2026\/03\/chp506-65x15.png 65w, https:\/\/libraryresources.nse.org.ng\/robotics\/wp-content\/uploads\/sites\/18\/2026\/03\/chp506-225x51.png 225w, https:\/\/libraryresources.nse.org.ng\/robotics\/wp-content\/uploads\/sites\/18\/2026\/03\/chp506-350x79.png 350w, https:\/\/libraryresources.nse.org.ng\/robotics\/wp-content\/uploads\/sites\/18\/2026\/03\/chp506.png 1670w\" sizes=\"(max-width: 710px) 100vw, 710px\" \/><figcaption id=\"caption-attachment-104\" class=\"wp-caption-text\">Figure 14.6: The cost function including the added process and measurement noises.<\/figcaption><\/figure>\n<p>Following the process above, we can obtain the following state results. The two results correspond to the two different conditions where either no noises exist (Figure 14.7), or process and measurement noises are in existence (Figure 14.8).<\/p>\n<p>Evidently, the results of the states and the input have both satisfied the constraints. Therefore, we can confidently say that the proposed MPC is effective in solving the state space while considering both process and measurement noises in the system.<\/p>\n<figure id=\"attachment_104\" aria-describedby=\"caption-attachment-104\" style=\"width: 402px\" class=\"wp-caption aligncenter\"><img decoding=\"async\" class=\"wp-image-112\" src=\"https:\/\/libraryresources.nse.org.ng\/wp-content\/uploads\/sites\/18\/2026\/03\/chp507-300x223.png\" alt=\"The states of the noiseless system, X1 and X2.\" width=\"402\" height=\"299\" srcset=\"https:\/\/libraryresources.nse.org.ng\/robotics\/wp-content\/uploads\/sites\/18\/2026\/03\/chp507-300x223.png 300w, https:\/\/libraryresources.nse.org.ng\/robotics\/wp-content\/uploads\/sites\/18\/2026\/03\/chp507-768x572.png 768w, https:\/\/libraryresources.nse.org.ng\/robotics\/wp-content\/uploads\/sites\/18\/2026\/03\/chp507-65x48.png 65w, https:\/\/libraryresources.nse.org.ng\/robotics\/wp-content\/uploads\/sites\/18\/2026\/03\/chp507-225x167.png 225w, https:\/\/libraryresources.nse.org.ng\/robotics\/wp-content\/uploads\/sites\/18\/2026\/03\/chp507-350x260.png 350w, https:\/\/libraryresources.nse.org.ng\/robotics\/wp-content\/uploads\/sites\/18\/2026\/03\/chp507.png 860w\" sizes=\"(max-width: 402px) 100vw, 402px\" \/><figcaption id=\"caption-attachment-104\" class=\"wp-caption-text\">Figure 14.7: The states of the noiseless system, X1 and X2.<\/figcaption><\/figure>\n<p>&nbsp;<\/p>\n<figure id=\"attachment_104\" aria-describedby=\"caption-attachment-104\" style=\"width: 381px\" class=\"wp-caption aligncenter\"><img decoding=\"async\" class=\"wp-image-113\" src=\"https:\/\/libraryresources.nse.org.ng\/wp-content\/uploads\/sites\/18\/2026\/03\/chp508-300x225.png\" alt=\"The states of the noisy system, X1 and X2, and noises of the system.\" width=\"381\" height=\"286\" srcset=\"https:\/\/libraryresources.nse.org.ng\/robotics\/wp-content\/uploads\/sites\/18\/2026\/03\/chp508-300x225.png 300w, https:\/\/libraryresources.nse.org.ng\/robotics\/wp-content\/uploads\/sites\/18\/2026\/03\/chp508-768x577.png 768w, https:\/\/libraryresources.nse.org.ng\/robotics\/wp-content\/uploads\/sites\/18\/2026\/03\/chp508-65x49.png 65w, https:\/\/libraryresources.nse.org.ng\/robotics\/wp-content\/uploads\/sites\/18\/2026\/03\/chp508-225x169.png 225w, https:\/\/libraryresources.nse.org.ng\/robotics\/wp-content\/uploads\/sites\/18\/2026\/03\/chp508-350x263.png 350w, https:\/\/libraryresources.nse.org.ng\/robotics\/wp-content\/uploads\/sites\/18\/2026\/03\/chp508.png 860w\" sizes=\"(max-width: 381px) 100vw, 381px\" \/><figcaption id=\"caption-attachment-104\" class=\"wp-caption-text\">Figure 14.8: The states of the noisy system, X1 and X2, and noises of the system.<\/figcaption><\/figure>\n<p>&nbsp;<\/p>\n<figure id=\"attachment_104\" aria-describedby=\"caption-attachment-104\" style=\"width: 400px\" class=\"wp-caption aligncenter\"><img decoding=\"async\" class=\"wp-image-114\" src=\"https:\/\/libraryresources.nse.org.ng\/wp-content\/uploads\/sites\/18\/2026\/03\/chp509-300x222.png\" alt=\"The control input u of the system.\" width=\"400\" height=\"296\" srcset=\"https:\/\/libraryresources.nse.org.ng\/robotics\/wp-content\/uploads\/sites\/18\/2026\/03\/chp509-300x222.png 300w, https:\/\/libraryresources.nse.org.ng\/robotics\/wp-content\/uploads\/sites\/18\/2026\/03\/chp509-768x569.png 768w, https:\/\/libraryresources.nse.org.ng\/robotics\/wp-content\/uploads\/sites\/18\/2026\/03\/chp509-65x48.png 65w, https:\/\/libraryresources.nse.org.ng\/robotics\/wp-content\/uploads\/sites\/18\/2026\/03\/chp509-225x167.png 225w, https:\/\/libraryresources.nse.org.ng\/robotics\/wp-content\/uploads\/sites\/18\/2026\/03\/chp509-350x259.png 350w, https:\/\/libraryresources.nse.org.ng\/robotics\/wp-content\/uploads\/sites\/18\/2026\/03\/chp509.png 856w\" sizes=\"(max-width: 400px) 100vw, 400px\" \/><figcaption id=\"caption-attachment-104\" class=\"wp-caption-text\">Figure 14.9: The control input u of the system.<\/figcaption><\/figure>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<h1><strong>4) Class Engagement<\/strong><\/h1>\n<p>Now you have learned the MPC theory, lets make summarization of the nature of the MPC controller. <span style=\"text-align: initial;font-size: 1em\">The key thing to implement MPC controller is to have optimal controller to solve the cost function a shown in below.\u00a0 \u00a0Specifically, we use python package cvxpy to minimize the cost function.<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">$$J = \\sum_{i=0}^{N-1} \\left( (x(k+i) &#8211; x_{\\text{ref}}(k+i))^T Q (x(k+i) &#8211; x_{\\text{ref}}(k+i)) \\right) + \\lambda \\sum_{i=0}^{N-1} \\left( u(k+i)^T R u(k+i) \\right)$$<\/span><\/p>\n<p>&nbsp;<\/p>\n<p>Python Code:<\/p>\n<p><strong>cost += cvxpy.quad_form(x[:, t + 1], Q)<\/strong><br \/>\n<strong>cost += cvxpy.quad_form(u[:, t], R)<\/strong><\/p>\n<p>&nbsp;<\/p>\n<div class=\"page\" role=\"region\" data-page-number=\"3\" aria-label=\"Page 3\" data-loaded=\"true\">\n<div class=\"textLayer\">\n<p>The constrains needed to be satisfied is<\/p>\n<\/div>\n<p>$$ x(k+i+1|k) = A x(k+i|k) + B u(k+i) $$<\/p>\n<div class=\"textLayer\">\n<p>[x[:, t + 1] == A @ x[:, t] + B @ u[:, t]]<\/p>\n<\/div>\n<\/div>\n<p>&nbsp;<\/p>\n<div class=\"page\" role=\"region\" data-page-number=\"3\" aria-label=\"Page 3\" data-loaded=\"true\">\n<div class=\"textLayer\">\n<p>&#8212;&#8212;&#8212;&#8212;&#8212; The Key Code of MPC in Python &#8212;&#8212;&#8212;&#8212;&#8211;<\/p>\n<\/div>\n<\/div>\n<div>\n<p>for t in range(T):<br \/>\ncost += cvxpy.quad_form(x[:, t + 1], Q)<br \/>\ncost += cvxpy.quad_form(u[:, t], R)<br \/>\nconstr += [x[:, t + 1] == A @ x[:, t] + B @ u[:, t]]<\/p>\n<\/div>\n<p>Other Constains<\/p>\n<div>\n<p>$$ x_{\\text{min}} \\leq x(k+i|k) \\leq x_{\\text{max}} $$ $$ u_{\\text{min}} \\leq u(k+i) \\leq u_{\\text{max}} $$ where:<\/p>\n<p>Python Code:<\/p>\n<p><strong>constr += [x[:, 0] == x0[:, 0]]<\/strong><\/p>\n<p>The below is to solve it with cvxpy.Minimize function<\/p>\n<\/div>\n<div>\n<p><strong>prob = cvxpy.Problem(cvxpy.Minimize(cost), constr)<\/strong><\/p>\n<\/div>\n<p>&nbsp;<\/p>\n<p>We will use the same class engagment example as shown in the LQR chapter.\u00a0 For this problem, an inverted pendulum sits on a cart moving along the x-axis direction shown in Figure 14.10.\u00a0 The inverted pendulum can move along the X and Y direction. It means the inverted pendulum can swing back and forth shown.<\/p>\n<p>&nbsp;<\/p>\n<p><img decoding=\"async\" class=\"size-medium wp-image-103 aligncenter\" src=\"https:\/\/libraryresources.nse.org.ng\/wp-content\/uploads\/sites\/18\/2026\/03\/cart-300x222.png\" alt=\"\" width=\"300\" height=\"222\" srcset=\"https:\/\/libraryresources.nse.org.ng\/robotics\/wp-content\/uploads\/sites\/18\/2026\/03\/cart-300x222.png 300w, https:\/\/libraryresources.nse.org.ng\/robotics\/wp-content\/uploads\/sites\/18\/2026\/03\/cart-768x568.png 768w, https:\/\/libraryresources.nse.org.ng\/robotics\/wp-content\/uploads\/sites\/18\/2026\/03\/cart-65x48.png 65w, https:\/\/libraryresources.nse.org.ng\/robotics\/wp-content\/uploads\/sites\/18\/2026\/03\/cart-225x166.png 225w, https:\/\/libraryresources.nse.org.ng\/robotics\/wp-content\/uploads\/sites\/18\/2026\/03\/cart-350x259.png 350w, https:\/\/libraryresources.nse.org.ng\/robotics\/wp-content\/uploads\/sites\/18\/2026\/03\/cart.png 836w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/p>\n<p style=\"text-align: center\">Figure 14.10 : Example of using Python to compute the LQR control gain K.<\/p>\n<p>In order to control the inverted pendulum using LQR , we need to model the system.\u00a0 The followings show the process of the modeling<\/p>\n<p><a href=\"https:\/\/mtmailmtsu-my.sharepoint.com\/:b:\/g\/personal\/hbzhang_mtsu_edu\/Ee-RhYhDtIRJqXFsQKpTBWoBq0Z9ZpVDy6PuTrdSbQzXYg?e=MPBRwJ\">https:\/\/mtmailmtsu-my.sharepoint.com\/:b:\/g\/personal\/hbzhang_mtsu_edu\/Ee-RhYhDtIRJqXFsQKpTBWoBq0Z9ZpVDy6PuTrdSbQzXYg?e=MPBRwJ<\/a><\/p>\n<p>&nbsp;<\/p>\n<p>Through the derivation above, you can find the system state space as the following.<\/p>\n<p>\\[<br \/>\n\\begin{bmatrix}<br \/>\n\\dot{x} \\\\<br \/>\n\\ddot{x} \\\\<br \/>\n\\dot{\\theta} \\\\<br \/>\n\\ddot{\\theta}<br \/>\n\\end{bmatrix}<br \/>\n=<br \/>\n\\begin{bmatrix}<br \/>\n0 &amp; 1 &amp; 0 &amp; 0 \\\\<br \/>\n0 &amp; 0 &amp; -\\frac{gm}{M} &amp; 0 \\\\<br \/>\n0 &amp; 0 &amp; 0 &amp; 1 \\\\<br \/>\n0 &amp; 0 &amp; \\frac{g(M+m)}{lM} &amp; 0<br \/>\n\\end{bmatrix}<br \/>\n\\begin{bmatrix}<br \/>\nx \\\\<br \/>\n\\dot{x} \\\\<br \/>\n\\theta \\\\<br \/>\n\\dot{\\theta}<br \/>\n\\end{bmatrix}<br \/>\n+<br \/>\n\\begin{bmatrix}<br \/>\n0 \\\\<br \/>\n\\frac{1}{M} \\\\<br \/>\n0 \\\\<br \/>\n\\frac{1}{lM}<br \/>\n\\end{bmatrix}<br \/>\nF<br \/>\n\\]<\/p>\n<p>Following running the code, results show that the use of LQR is able to stabilize the cart motion as shown in Figure 14.11. Specifically, when the cart is moving from one side to another, the inverted pendulum stays upright.<\/p>\n<p><img decoding=\"async\" class=\"wp-image-104 aligncenter\" src=\"https:\/\/libraryresources.nse.org.ng\/wp-content\/uploads\/sites\/18\/2026\/03\/cartanimation-300x81.png\" alt=\"\" width=\"619\" height=\"167\" srcset=\"https:\/\/libraryresources.nse.org.ng\/robotics\/wp-content\/uploads\/sites\/18\/2026\/03\/cartanimation-300x81.png 300w, https:\/\/libraryresources.nse.org.ng\/robotics\/wp-content\/uploads\/sites\/18\/2026\/03\/cartanimation-1024x277.png 1024w, https:\/\/libraryresources.nse.org.ng\/robotics\/wp-content\/uploads\/sites\/18\/2026\/03\/cartanimation-768x207.png 768w, https:\/\/libraryresources.nse.org.ng\/robotics\/wp-content\/uploads\/sites\/18\/2026\/03\/cartanimation-1536x415.png 1536w, https:\/\/libraryresources.nse.org.ng\/robotics\/wp-content\/uploads\/sites\/18\/2026\/03\/cartanimation-65x18.png 65w, https:\/\/libraryresources.nse.org.ng\/robotics\/wp-content\/uploads\/sites\/18\/2026\/03\/cartanimation-225x61.png 225w, https:\/\/libraryresources.nse.org.ng\/robotics\/wp-content\/uploads\/sites\/18\/2026\/03\/cartanimation-350x95.png 350w, https:\/\/libraryresources.nse.org.ng\/robotics\/wp-content\/uploads\/sites\/18\/2026\/03\/cartanimation.png 1918w\" sizes=\"(max-width: 619px) 100vw, 619px\" \/><\/p>\n<p style=\"text-align: center\">Figure 14.11:\u00a0 The results of running MPC python code showing that MPC is able to stablize the inverted pendulum on the moving cart.<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"author":1,"menu_order":14,"template":"","meta":{"pb_show_title":"","pb_short_title":"Ch. 14: Model Predictive Control","pb_subtitle":"","pb_authors":["hbzhang"],"pb_section_license":""},"chapter-type":[],"contributor":[61],"license":[],"class_list":["post-115","chapter","type-chapter","status-publish","hentry","contributor-hbzhang"],"part":66,"_links":{"self":[{"href":"https:\/\/libraryresources.nse.org.ng\/robotics\/wp-json\/pressbooks\/v2\/chapters\/115","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/libraryresources.nse.org.ng\/robotics\/wp-json\/pressbooks\/v2\/chapters"}],"about":[{"href":"https:\/\/libraryresources.nse.org.ng\/robotics\/wp-json\/wp\/v2\/types\/chapter"}],"author":[{"embeddable":true,"href":"https:\/\/libraryresources.nse.org.ng\/robotics\/wp-json\/wp\/v2\/users\/1"}],"version-history":[{"count":1,"href":"https:\/\/libraryresources.nse.org.ng\/robotics\/wp-json\/pressbooks\/v2\/chapters\/115\/revisions"}],"predecessor-version":[{"id":116,"href":"https:\/\/libraryresources.nse.org.ng\/robotics\/wp-json\/pressbooks\/v2\/chapters\/115\/revisions\/116"}],"part":[{"href":"https:\/\/libraryresources.nse.org.ng\/robotics\/wp-json\/pressbooks\/v2\/parts\/66"}],"metadata":[{"href":"https:\/\/libraryresources.nse.org.ng\/robotics\/wp-json\/pressbooks\/v2\/chapters\/115\/metadata\/"}],"wp:attachment":[{"href":"https:\/\/libraryresources.nse.org.ng\/robotics\/wp-json\/wp\/v2\/media?parent=115"}],"wp:term":[{"taxonomy":"chapter-type","embeddable":true,"href":"https:\/\/libraryresources.nse.org.ng\/robotics\/wp-json\/pressbooks\/v2\/chapter-type?post=115"},{"taxonomy":"contributor","embeddable":true,"href":"https:\/\/libraryresources.nse.org.ng\/robotics\/wp-json\/wp\/v2\/contributor?post=115"},{"taxonomy":"license","embeddable":true,"href":"https:\/\/libraryresources.nse.org.ng\/robotics\/wp-json\/wp\/v2\/license?post=115"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}