# 11.13. High-level interface: Rate Constraints¶

As in Section High-level interface: Basic example we consider the following linear MPC problem with lower and upper bounds on state and inputs, and a terminal cost term:

\begin{align*} \text{minimize} \quad & x_N^\top P x_N + \sum_{i=0}^{N-1} \left( x_i^\top Q x_i + u_i^\top R u_i \right) \\ \text{subject to} \quad & x_0 = {\color{red} x} \\ & x_{i+1} = A x_i + B u_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{red} x$$ and the first input $$u_0$$ is typically applied to the system after a solution has been obtained. For the sake of this example, we assume $$u_i \in \mathbb{R}$$ and $$x_i \in \mathbb{R}^2$$ and write

\begin{align*} A = \begin{pmatrix} A_{11} & A_{12} \\ A_{21} & A_{22} \end{pmatrix}, \quad B = \begin{pmatrix} B_1 \\ B_2 \end{pmatrix}. \end{align*}

In addition, we impose constraints on the input rate change $$\Delta u_i = u_{i+1} - u_{i}$$:

\begin{align*} \underline{\Delta u} \leq \Delta u_i \leq \overline{\Delta u}. \end{align*}

The constraints can be included by defining states

\begin{align*} z_i = \begin{pmatrix} \Delta u_i \\ u_{i} \\ x_i \end{pmatrix} \in \mathbb{R}^4. \end{align*}

\begin{align*} \text{minimize} \quad & x_N^\top P x_N + \sum_{i=0}^{N-1} \left( x_i^\top Q x_i + u_i^\top R u_i \right), \\ \text{subject to} \quad & x_0 = {\color{red} x}, \\ & E z_{i+1} = \hat{A} z_i, \\ & \underline{z} \leq z_i \leq \bar{z}. \end{align*}

with

\begin{align*} E = \begin{pmatrix} 0 & 1 & 0 & 0 \\ 0 & 0 & 1 & 0 \\ 0 & 0 & 0 & 1 \end{pmatrix}, \quad \hat{A} = \begin{pmatrix} 1 & 1 & 0 & 0 \\ 0 & B_1 & A_{11} & A_{12} \\ 0 & B_2 & A_{21} & A_{22} \end{pmatrix}, \\ \underline{z} = (\underline{\Delta u}, \underline{u}, \underline{x})^\top, \quad \overline{z} = (\overline{\Delta u}, \overline{u}, \overline{x})^\top. \end{align*}

## 11.13.1. Implementation in MATLAB¶

The Matlab code is based on the Matlab code in High-level interface: Basic example. We define a variable absrate which limits the absolute value of $$\Delta u$$.

%% system
A = [1.1 1; 0 1];
B = [1; 0.5];
[nx,nu] = size(B);

%% MPC setup
N = 10;
Q = eye(nx);
R = eye(nu);
if( exist('dlqr','file') )
[~,P] = dlqr(A,B,Q,R);
else
P = 10*Q;
end

absrate = 0.5;
umin = -0.5;     umax = 0.5;
dumin = -absrate; dumax = absrate;
xmin = [-5, -5]; xmax = [5, 5];

%% FORCESPRO multistage form
% assume variable ordering zi = [u{i+1}-u{i}; u{i}; x{i}] for i=0...N

% dimensions
model.N     = 11;   % horizon length N+1
model.nvar  = nu+nu+nx;    % number of variables
model.neq   = nu+nx;    % number of equality constraints

% objective
model.objective = @(z) z(2)*R*z(2) + [z(3);z(4)]'*Q*[z(3);z(4)];
model.objectiveN = @(z) [z(3);z(4)]'*P*[z(3);z(4)];

% equalities
model.eq = @(z) [ z(1) + z(2);
A(1,:)*[z(3);z(4)] + B(1)*z(2);
A(2,:)*[z(3);z(4)] + B(2)*z(2)];

model.E = [zeros(3,1), eye(3)];

% initial state
model.xinitidx = 3:4;

% inequalities
model.lb = [ dumin, umin,    xmin  ];
model.ub = [ dumax, umax,    xmax  ];

# system
A = np.array([[1.1, 1], [0, 1]])
B = np.array([[1], [0.5]])
nx, nu = np.shape(B)

# MPC setup
N = 10
Q = np.eye(nx)
R = np.eye(nu)
P = 10*Q
umin = -0.5
umax = 0.5
absrate = 0.05
dumin = -absrate
dumax = absrate
xmin = np.array([-5, -5])
xmax = np.array([5, 5])

# FORCESPRO multistage form
# assume variable ordering zi = [u{i+1}-ui; ui; xi] for i=0...N

# dimensions
model = forcespro.nlp.ConvexSymbolicModel(11)   # horizon length N+1
model.nvar = 4   # number of variables
model.neq = 3   # number of equality constraints

# objective
model.objective = (lambda z: z[1]*R*z[1] +
model.objectiveN = (lambda z:

# equalities
model.eq = lambda z: casadi.vertcat( z[0] + z[1],

model.E = np.concatenate([np.zeros((3, 1)), np.eye(3)], axis=1)

# initial state
model.xinitidx = [2, 3]

# inequalities
model.lb = np.concatenate([[dumin, umin], xmin])
model.ub = np.concatenate([[dumax, umax], xmax])


You can download the code of this example to try it out for yourself: MATLAB. PYTHON.

## 11.13.2. Results¶

We run the simulation for different values of absrate. The results of the simulation are presented below. The plot on the top shows the system’s states over time, the plot in the middle shows the input commands, the plot on the bottom shows the input rate change. We can see that all constraints are respected. We observe that compared to High-level interface: Basic example the behaviour does not change for absrate >= 0.1 (see Figure 11.53). If absrate = 0.05, it takes more time to steer the state to its setpoint (see Figure 11.54).

Figure 11.53 Simulation results of the states (top, in blue and red), input (middle, in blue), and input rate change (bottom, in blue) over time. The constraints are plotted in red dashed lines. The rate constraint is set to 0.5 and is not active at any moment.

Figure 11.54 Simulation results of the states (top, in blue and red), input (middle, in blue), and input rate change (bottom, in blue) over time. The constraints are plotted in red dashed lines. The rate constraint is set to 0.05 and is active at some points.