{"id":25204824,"url":"https://github.com/leopits/linear-mpc-implementation","last_synced_at":"2025-09-12T00:04:33.821Z","repository":{"id":201247470,"uuid":"707308611","full_name":"LeoPits/Linear-MPC-implementation","owner":"LeoPits","description":"About This is the MATLAB code for a brief tutorial for Model Predictive Control (MPC) for water tank system with constrained states and inputs.","archived":false,"fork":false,"pushed_at":"2023-11-03T16:18:06.000Z","size":447,"stargazers_count":1,"open_issues_count":1,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-02-10T09:17:47.103Z","etag":null,"topics":["matlab","model-predictive-control","mpc-control"],"latest_commit_sha":null,"homepage":"","language":"MATLAB","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/LeoPits.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null}},"created_at":"2023-10-19T16:21:45.000Z","updated_at":"2024-05-17T12:30:53.000Z","dependencies_parsed_at":"2023-10-24T13:40:09.832Z","dependency_job_id":null,"html_url":"https://github.com/LeoPits/Linear-MPC-implementation","commit_stats":null,"previous_names":["leopits/mpc-water-tank-implemention"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/LeoPits%2FLinear-MPC-implementation","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/LeoPits%2FLinear-MPC-implementation/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/LeoPits%2FLinear-MPC-implementation/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/LeoPits%2FLinear-MPC-implementation/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/LeoPits","download_url":"https://codeload.github.com/LeoPits/Linear-MPC-implementation/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247271495,"owners_count":20911586,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["matlab","model-predictive-control","mpc-control"],"created_at":"2025-02-10T09:17:48.934Z","updated_at":"2025-04-05T00:45:28.880Z","avatar_url":"https://github.com/LeoPits.png","language":"MATLAB","funding_links":[],"categories":[],"sub_categories":[],"readme":"# MPC-water-tank-implemention\n\nThis tutorial shows an implementation of  a linear quadratic model predictive controller (MPC) to control a quadruple-tank model,  simulated in Matlab/Simulink.\nConsider that the plant to control is modeled as a linear time-invariant system given by\n\n\n$$\n\\begin{align}\n    x^+\u0026=Ax+Bu   \\\\  \n    y\u0026=Cx+Du \n\\end{align}\n$$\n\nwhere $x\\in R^n$   his the current state of the system, $u  \\in \t R^m $ \nis the current input, $y \t\\in \t R^p$ is the controlled output and  $x^+$ \nthe successor state.\n\n\nThis linear model is derived from the linearization of the model at an operation equilibrium\npoint given by the triplet $(x_0, u_0, y_0)$. Therefore, denoting\nas $(x(j), u(j), y (j))$ the state, inputs and outputs of the\nplant, we have that $x(j) = x(j) − x_0$, $u(j) = u(j) − u_0$\nand $y(j) = y (j) − y_0$.\n\n\n\n\n\nThe objective is to implement an MPC control law $U(j) =\n\\kappa(x(j), R)$ such that the controlled system is asymptotically\nstable and the controlled variables $y (j)$ converge to the\nreference $R$ if this is reachable.\nFor a given state of the prediction model $x = x−x_0$ and a\nreference $r = R −y_0$, the MPC control law is derived from\nthe online solution of the following optimization control\nproblem:\n\n$$\n\\begin{align}\n    V_{\\text{N}}(x,y_{sp};\\mathbf{u})\u0026= \\sum_{j=0}^{N-1}\\parallel x(j)-x_r\\parallel_{Q}^{2}  + \\parallel u(j)-u_r\\parallel_{R}^{2}  + \\parallel x(N)-x_{r}\\parallel_{P}^{2} \\\\\n    s.t.\u0026 \\quad x(j+1)=Ax(j)+Bu(j),\\\\\n     \u0026 \\quad x(0)=x, \\\\\n     \u0026 \\quad  u_{min} \\leq u \\leq u_{max}, \\\\\n     \u0026 \\quad (x_r,u_r)=M_r, \\\\\n      \u0026 \\quad  x(N)  =0 \n\\end{align}\n$$\n\n\nwhere $M$ is a suitable matrix that maps the steady state and input given by the reference. Once a solution $u^{∗}(x, r)$\nis obtained, the control law is calculated by the receding horizon technique as follows $\\kappa_N (x, r) = u^{*} (0; x, r)$. The\ncontrol law implemented in the real plant will be $u(k) =\\kappa_N (x(j) − x_O, R − y_0) + u_0$.\n\n## Lifted System Dynamics\nthe notation of a lifted system dynamics\n\n$$\n\\mathbf{x}=G_x x(j) + G_u \\mathbf{u}\n$$\n\nis used, where the whole state sequence can be determined with the aid of the input sequence $\\mathbf{u}$ for a given initial state $x(j)$. The state sequence and the input sequence are \n\n$$\n\\mathbf{x}=\\left[\\begin{array}{c}\nx_{0}\\\\\nx_{1}\\\\\n\\vdots\\\\\nx_{N}\n\\end{array}\\right],u=\\left[\\begin{array}{c}\nu_{0}\\\\\nu_{1}\\\\\n\\vdots\\\\\nu_{N-1}\n\\end{array}\\right]\n$$\n\nThe lifted system matrix and the lifted input matrix are\n\n$$\nG_{x}=\\left[\\begin{array}{c}\nI\\\\\nA\\\\\nA^{2}\\\\\n\\vdots\\\\\nA^{N}\n\\end{array}\\right],G_{u}=\\left[\\begin{array}{cccc}\n0 \u0026 0 \u0026 \\cdots \u0026 0\\\\\nB \u0026 0 \u0026 \\cdots \u0026 0\\\\\nAB \u0026 B \u0026 \\cdots \u0026 0\\\\\n\\vdots \u0026 \\vdots \u0026 \\vdots \u0026 \\vdots\\\\\nA^{N-1}B \u0026 A^{N-2}B \u0026 \\cdots \u0026 B\n\\end{array}\\right]\n$$\n\n## Quadratic Program\n\nThe implementation with the MATLAB built-in functions quadprog is shown here.\n\n$$\n\\begin{align}\n    \\underset{\\mathbf{u}}{\\min}V_{N}(x,x_{r};\\mathbf{u}) \u0026= (\\mathbf{x}-\\mathbf{x_r})^TQ(\\mathbf{x}-\\mathbf{x_r})+  (\\mathbf{u}-\\mathbf{u}_r)^TR(\\mathbf{u}-\\mathbf{u}_r)\\\\\ns.t.\u0026 \\quad \\mathbf{x}=G_xx(j)+G_u\\mathbf{u},\\\\\n  \u0026\\quad \\widetilde{G}\\mathbf{u} \\leq \\widetilde{g} \n\\end{align}\n$$\n\n\n\n$$\n\\begin{align}\n    \\underset{\\mathbf{u}}{\\min}V_{N}(x,x_{r};\\mathbf{u})\u0026 = \\mathbf{u}^T(G_u  Q  G_u + R )\\mathbf{u} + 2(  x(j)^{T}  G_{x}^{T}  Q  G_u)\\mathbf{u}-2( x_{ R}^{T}  Q  G_u - \\mathbf{u_{r}}^{T}  R )\\mathbf{u}\\\\\n    s.t.\u0026 \\quad \\mathbf{x}=G_xx(j)+G_u\\mathbf{u},\\\\\n  \u0026\\quad \\widetilde{G}\\mathbf{u} \\leq \\widetilde{g} \n\\end{align}\n$$\n\n\n$$ \n\\begin{align}\n    \\underset{\\mathbf{u}}{\\min}V_{N}(x,x_{r};\\mathbf{u}) \u0026= \\mathbf{u}^T H \\mathbf{u} + 2(F_1-F_2)\\mathbf{u}\\\\\n    s.t.\u0026 \\quad \\mathbf{x}=G_xx(j)+G_u\\mathbf{u},\\\\\n  \u0026\\quad \\widetilde{G}\\mathbf{u} \\leq \\widetilde{g} \n\\end{align}\n$$    \n\n\n## Results\n![My Image](img/result_tracking.png)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fleopits%2Flinear-mpc-implementation","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fleopits%2Flinear-mpc-implementation","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fleopits%2Flinear-mpc-implementation/lists"}