%% Constraints on Linear Combinations of Inputs and Outputs
% You can constrain linear combinations of plant input and output
% variables. For example, you can constrain a particular manipulated
% variable (MV) to be greater than a linear combination of two other MVs.

%%
% The general form of such constraints is:
%
% $$Eu\left( {k + i} \right) + Fy\left( {k + i} \right) + 
% Sv\left( {k + i} \right) \le G + {\varepsilon _k}V$$
% 
% Here:
%
% * ${\varepsilon _k}$ is the QP slack variable used for constraint
% softening. For more information, see <docid:mpc_ug#buj077i>.
% * $u\left( {k + i} \right)$ are the ${N_{mv}}$ manipulated
% variable values, in engineering units.
% * $y\left( {k + i} \right)$ are the $N_y$ predicted plant
% outputs, in engineering units.
% * $v\left( {k + i} \right)$ are the ${N_{md}}$ measured plant
% disturbance inputs, in engineering units.
% * $E$, $F$, $S$, $G$, and $V$ are constant matrices and vectors. For more
% information, see <docid:mpc_ref#butvtsz>.
%
% As with the QP cost function, output prediction using the state observer
% makes these constraints a function of the QP decision variables.
%
% To set the mixed input/output constraints of an MPC controller, use the
% <docid:mpc_ref#butvtsz> function. To obtain the existing constraints from
% a controller, use <docid:mpc_ref#butvxep>.
%
% When using mixed input/output constraints, consider the following:
%
% * Mixed input/output constraints are dimensional by default.
% * Run-time updating of mixed input/output constraints is supported at the
% command line and in Simulink(R). For more information, see
% <docid:mpc_ug#bujlmak>.
% * Using mixed input/output constraints is not supported in *MPC
% Designer*.
%

%%
% As an example, consider an MPC controller for a double-integrator plant
% with mixed input/output constraints.

%% Create Initial MPC Controller  
% The basic setup of the MPC controller includes:
%
% * A double integrator as the prediction model
% * Prediction horizon of 20
% * Control horizon of 20
% * Input constraints: $- 1 \le u\left( t \right) \le 1$
%
plant = tf(1,[1 0 0]);
Ts = 0.1;
p = 20;
m = 20;
mpcobj = mpc(plant,Ts,p,m);
mpcobj.MV = struct('Min',-1,'Max',1);

%% Define Mixed Input/Output Constraints 
% Constrain the sum of the input |u(t)| and output |y(t)| must be
% nonnegative and smaller than 1.2:
%
% $$0 \le u\left( t \right) + y\left( t \right) \le 1.2$$
%
% To impose this combined (mixed) I/O constraint, formulate it as a set of
% inequality constraints involving $u\left( t \right)$ and $y\left( t
% \right)$.
%
% $$\begin{array}{l}
% u\left( t \right) + y\left( t \right) \le 1.2\\
%  - u\left( t \right) - y\left( t \right) \le 0
% \end{array}$$
%
% To define these constraints using the |setconstraint| function, set the
% constraint constants as follows:
% 
% $$E = \left[ {\begin{array}{*{20}{c}}
% 1\\
% { - 1}
% \end{array}} \right],\;F = \left[ {\begin{array}{*{20}{c}}
% 1\\
% { - 1}
% \end{array}} \right],\;G = \left[ {\begin{array}{*{20}{c}}
% {1.2}\\
% 0
% \end{array}} \right]$$
% 
setconstraint(mpcobj,[1;-1],[1;-1],[1.2;0]);

%% Simulate Controller
% Simulate closed-loop control of the linear plant model in Simulink. The
% controller |mpcobj| is specified in the MPC Controller block.
mdl = 'mpc_mixedconstraints';
open_system(mdl)
sim(mdl)
 
%% 
% The MPC controller keeps the sum $u+y$ between 0 and 1.2 while tracking
% the reference signal, $r = 1$.

%%
bdclose(mdl)

% Copyright 2009-2014 The MathWorks, Inc.