Consider the following linear MPC problem with lower and upper bounds on state and inputs, and a terminal cost term:
$$ \begin{align} \text{minimize} \ & x_N^T P x_N + \sum_{i=0}^{N-1} x_i^T Q x_i + u_i^T R u_i \\
\text{subject to} \ & x_0 = \color{#d22d33}{x} \\
& x_{i+1} = Ax_i + Bu_i \\
& \underline{x} \leq x_i \leq \bar{x} \\
& \underline{u} \leq u_i \leq \bar{u}
\end{align} $$
This problem is parametric in the initial state \(\color{#d22d33}{x}\), and the first input \(u_0\) is typically applied to the system after a solution has been obtained. The following code generates a function that takes \(-Ax\) as a calling argument and returns \(u_0\), which can then be applied to the system: