% 3x1 system
%
% MV=[1], MD=[2], UD=[3]
% Y=[1]

% Prepare parameters for mpc_ex1a

% True plant & true initial state
sys=ss(tf({1,1,1},{[1 .5 1],[1 1],[.7 .5 1]}));
A=sys.a;
B=sys.b;
C=sys.c;
D=sys.d;

x0=[0 0 0 0 0]';


Ts=.2;     %Sample time
Tstop=30;  %Simulation time

% prediction model
model=c2d(sys,Ts);

p=10;
moves=3;
xhat0=0*x0;

clear MPCadd
MPCadd.mvindex=1;
MPCadd.mdindex=2;
MPCadd.x0=xhat0;

umin=0;
umax=1;
dumin=-Inf;
dumax=Inf;
ymin=-Inf;
ymax=Inf;

limits={umin,umax,dumin,dumax,ymin,ymax};
weights={[0],[.1],[1]};

% Reference and measured disturbance signals
time=0:Ts:Tstop;
trv=[time;
    sin(0:2*Ts:2*Tstop)
    cos(0:Ts:Tstop)]
   %zeros(size(0:Ts:5)),ones(size(5+Ts:Ts:Tstop))
   %zeros(size(0:Ts:10)),ones(size(10+Ts:Ts:Tstop))];

save rv trv
    
open_system('mpc_ex1a.mdl')
sim('mpc_ex1a',Tstop)