# Real-Time Model Predictive Control of Human Bodyweight Based on Energy Intake
## 2 Materials and Methods
### Model Predictive Control (MPC)  
After selecting in the previous steps the best time-invariant parameters and time-variant parameters models, a model predictive control (MPC) system is developed. An MPC solves an optimization problem to advise the dependent variable or input needed to the system in order to achieve an output or independent variable, according to the model included in the controller, the closest to the desired or reference output. The standard cost function is generally described as function of four terms, each one focusing on a particular aspect of the controller performance [48],  
\[J(z_{k})=J_{y}(z_{k})+J_{u}(z_{k})+J_{\Delta u}(z_{k})+J_{e}(z_{k}) \tag{13}\]  
where \(J_{y}(z_{k})\) is related to the output reference tracking performance,  
\[J_{y}(z_{k})=\sum_{j=1}^{n_{y}}\sum_{i=1}^{p}\left(\frac{w_{i,j}^{y}}{s_{j}^{u }}\left[r_{j}(k+i|k)-y_{j}(k+i|k)\right]\right)^{2} \tag{14}\]  
where \(k\) is the current control interval, \(p\) the prediction horizon, \(n_{y}\) the number of outputs, \(z_{k}\) is the suggested input, given by \(z_{k}^{T}=\left[u(k|k)^{T}\;\;u(k+1|k)^{T}\;\cdots\;u(k+p-1|k)^{T}\;\epsilon_{ k}\right]\); \(y_{j}(k+i|k)\) and \(r_{j}(k+i|k)\) is the predicted \(j\)-th output value and the reference value, respectively, at the \(i\)-th prediction horizon step. Finally, \(w_{i,j}^{y}\) and \(s_{j}^{y}\) are the tuning weight and scale factor, respectively, for the predicted \(j\)-th output at the \(i\)-th prediction horizon step. Also, \(J_{u}(z_{k})\) is related to the dependent variable tracking performance and is given by,  
\[J_{u}(z_{k})=\sum_{j=1}^{n_{u}}\sum_{i=0}^{p-1}\left\{\frac{w_{i,j}^{\Delta u }}{s_{j}^{u}}\left[u_{j}(k+i|k)-u_{j}(k+i-1|k)\right]\right\}^{2} \tag{15}\]  
where \(k,p\), and \(z_{k}\) are defined as before. \(n_{u}\) the number of inputs, \(u_{j}(k+i|k)\) and \(u_{j,target}(k+i|k)\) is the predicted \(j\)-th input and target input, respectively, at the \(i\)-th prediction horizon step. Finally, \(w_{i,j}^{u}\) and \(s_{j}^{u}\) are the tuning weight and scale factor, respectively, for the predicted \(j\)-th input at the \(i\)-th prediction horizon step. Besides, \(J_{\Delta u}(z_{k})\) accounts for the changes in the dependent variable in consecutive realisations, given by,  
\[J_{\Delta u}(z_{k})=\sum_{j=1}^{n_{u}}\sum_{i=0}^{p-1}\left\{\frac{w_{i,j}^{u}}{s _{j}^{u}}\Big{[}u_{j}(k+\ i|k)-u_{j,target}(k+\ i|k)\Big{]}\right\}^{2} \tag{16}\]  
where \(k\), \(p\), \(n_{u}\), and \(z_{k}\) are defined as before. \(u_{j}(k+\ i|k)\) and \(u_{j,target}(k+\ i|k)\) is the predicted \(j\)-th input and target input, respectively, at the \(i\)-th prediction horizon step. Finally, \(w_{i,j}^{\Delta u}\) is the tuning weight for the predicted \(j\)-th input movement at the \(i\)-th prediction horizon step and \(s_{j}^{u}\) the scale factor for the \(j\)-th input. Finally, \(J_{y}(z_{k})\) addresses the violation of constraints when these are included in the controller,  
\[J_{e}(z_{k})=\rho_{e}\epsilon_{k}^{2} \tag{17}\]  
where \(z_{k}\) is defined as before. \(\rho_{e}\) is the constraint violation penalty weight and \(\epsilon_{k}^{2}\) the slack variable at control interval \(k\). In this work, the input variable is the energy intake and the output the bodyweight. This MPC approach is tested when using time-invariant parameters and time-variants parameters models using constrains and without them. The constraints simply account for the minimum and maximum values allowed for bodyweight and energy intake. The minimum and maximum bodyweight are, \(BW_{min}=52.2\) kg and \(BW_{max}=69.8\) kg, respectively. In addition, the minimum and maximum energy intake are, \(El_{min}=1223\) kcal day\({}^{-1}\) and \(El_{max}=3556\) kcal day\({}^{-1}\), respectively. The controller's performance and robustness are tested. Firstly, the bodyweight measured curve from each test participant in the Minnesota starvation experiment is used as reference curve and the energy intake suggested by the MPC is compared with the energy intake actually measured to evaluate the performance.