%% Generate Explicit MPC Controller
% Generate an explicit MPC controller based upon a traditional MPC controller for a double-integrator plant.
% Define the double-integrator plant.

plant = tf(1,[1 0 0]);

%
% Create a traditional (implicit) MPC controller for this plant, with sample time 0.1, a prediction horizon of 10, and a control horizon of 3.

Ts = 0.1;
p = 10;
m = 3;
MPCobj = mpc(plant,Ts,p,m);

%
% To generate an explicit MPC controller, you must specify the ranges of parameters such as state values and manipulated variables. To do so, generate a range structure. Then, modify values within the structure to the desired parameter ranges.

range = generateExplicitRange(MPCobj);

%

range.State.Min(:) = [-10;-10];
range.State.Max(:) = [10;10];
range.Reference.Min = -2;
range.Reference.Max = 2;
range.ManipulatedVariable.Min = -1.1;
range.ManipulatedVariable.Max = 1.1;

%
% Use the more robust reduction method for the computation. Use generateExplicitOptions to create a default options set, and then modify the polyreduction option.

opt = generateExplicitOptions(MPCobj);
opt.polyreduction = 1;

%
% Generate the explicit MPC controller.

EMPCobj = generateExplicitMPC(MPCobj,range,opt)

% Copyright 2015 The MathWorks, Inc.