sys = ss(tf({1,1,1},{[1 .5 1],[1 1],[.7 .5 1]}));
x0 = [0 0 0 0 0]';
Ts = 0.2;
sys.InputGroup=struct('Manipulated',1,'Measured',2,'Unmeasured',3);
MV = struct('Min', 0, 'Max', 1);
Model = [];
Model.Plant = sys;
Model.Disturbance = tf(sqrt(1000), [1 0]);
p = [];
m = 3;
weights = [];
MPCobj = mpc(Model, Ts, p, m, weights, MV);
Tstop = 30;
xmpc = mpcstate(MPCobj);
x = x0;
r = 1;
[A, B, C, D] = ssdata(c2d(sys, Ts));
YY = [];
XX = [];
RR = [];
for t = 0:round(Tstop/Ts)-1
XX=[XX, x];
v = 0;
if t*Ts>=10
v=1;
end
d=0;
if t*Ts>=20
d=-0.5;
end
y = C*x+D(:,2)*v+D(:,3)*d;
YY = [YY, y];
[u, info] = mpcmove(MPCobj, xmpc, y, r, v);
info
x = A*x+B(:, 1)*u+B(:, 2)*v+B(:, 3)*d;
end
plot(0:Ts:Tstop-Ts, YY);
grid
Best Answer