%% Evaluate Scenario at Specific Time Instant
% Define a plant model.

ts = 2;
Plant = ss(0.8,0.5,0.25,0,ts);

%
% Create a model predictive controller with constraints on both the manipulated variable and the rate of change of the manipulated variable. The prediction horizon is 10 intervals, and the control horizon is blocked.

MPCobj = mpc(Plant,ts,10,[2 3 5]);
MPCobj.MV(1).Min = -2;
MPCobj.MV(1).Max = 2;
MPCobj.MV(1).RateMin = -1;
MPCobj.MV(1).RateMax = 1;

%
% Initialize an mpcstate object for simulation from a particular state.

x = mpcstate(MPCobj);
x.Plant = 2.8;
x.LastMove = 0.85;

%
% Compute the optimal control at current time.

y = 0.25*x.Plant;
r = 1;
[u,Info] = mpcmove(MPCobj,x,y,r);

%
% Analyze the predicted optimal sequences.

[ts,us] = stairs(Info.Topt,Info.Uopt);
plot(ts,us,'r-',Info.Topt,Info.Yopt,'b--')
legend('MV','OV')

% plot ignores Info.Uopt(end) as it is NaN.
%
% Examine the optimal cost.

Info.Cost

% Copyright 2015 The MathWorks, Inc.