% We start defining the plant to be controlled.
sys=ss(tf({1,1,1},{[1 .5 1],[1 1],[.7 .5 1]}),'min');
% Now, setup an MPC controller object.
Ts=.2; % sampling time
model=c2d(sys,Ts); % prediction model
% Define type of input signals: the first signal is a manipulated variable,
% the second signal is a measured disturbance, the third one is an unmeasured disturbance.
model=setmpcsignals(model,'MV',1,'MD',2,'UD',3);
% Define the structure of models used by the MPC controller.
clear Model
% Predictive model
Model.Plant=model;
% Disturbance model: Integrator driven by white noise with variance = 1000
Model.Disturbance=tf(sqrt(1000),[1 0]);
% Define prediction and control horizons.
p=[]; % prediction horizon (take default one)
m=3; % control horizon
% Let us assume default value for weights and build the MPC object.
MPCobj=mpc(Model,Ts,p,m);
% Define constraints on the manipulated variable.
MPCobj.MV=struct('Min',0,'Max',1,'RateMin',-10,'RateMax',10);
% Closed-loop MPC Simulation Using the Command SIM
Tstop=30; % simulation time
Tf=round(Tstop/Ts); % number of simulation steps
r=ones(Tf,1); % reference trajectory
v=[zeros(Tf/3,1);ones(2*Tf/3,1)]; % measured disturbance trajectory
% Run the closed-loop simulation and plot results.
close all
sim(MPCobj,Tf,r,v);
% We want to specify disturbance and noise signals.
% In order to do this, we create the MPC simulation object 'SimOptions'.
d=[zeros(2*Tf/3,1);-0.5*ones(Tf/3,1)]; % unmeasured disturbance trajectory
SimOptions=mpcsimopt(MPCobj);
SimOptions.Unmeas=d; % unmeasured input disturbance
SimOptions.OutputNoise=.001*(rand(Tf,1)-.5); % output measurement noise
SimOptions.InputNoise=.05*(rand(Tf,1)-.5); % noise on manipulated variables
% Run the closed-loop simulation and save the results to workspace.
[y,t,u,xp]=sim(MPCobj,Tf,r,v,SimOptions);
% Plot results.
close all
subplot(211)
plot(0:Tf-1,y,0:Tf-1,r)
title('Output');
grid
subplot(212)
plot(0:Tf-1,u)
title('Input');
grid