%TuneMPC
%Needs linear model G generated from linear analysis in simulink
clear all
startup
% genlinmod
load mpcmod
delt=5;
tfinal=1500;
% GENERATE Step response model:
% plant = ss2step(phi, gam, c, d, tfinal, delt1, delt2, nout)
A=pmod.a;
B=pmod.b;B=B(:,1:4);
C=pmod.c;C=C(1:4,:);
D=pmod.d;D=D(1:4,1:4);
pmod=ss2step(A,B,C,D,tfinal,[],delt);

Ad=dmod.a;
Bd=dmod.b;
Cd=dmod.c;Cd=Cd(1:4,:);
Dd=dmod.d;Dd=Dd(1:4,:);
dmod=ss2step(Ad,Bd,Cd,Dd,tfinal,[],delt);% disturbance model

ywt=[80 85 80 1];   
uwt=235*[1 1 1 1];
P=25; % P=1.0*3600/delt;   % OUTPUT (prediction) HORIZON
M=5;           % INPUT  HORIZON (no. of manipulated variable moves)
%Ks=smpccon(pmod,ywt,uwt,M,P);
%tend=1000;
r=[0 0 0 0];%ynom(1:3);0.5]';     % setpoints zero because they are set OUTSIDE the block
ulim=[];%zeros(1,4),[1.5 1.5 1.5 1],1e6*ones(1,4)];% INPUT  CONSTRAINTS
ylim=[];%0 0 0 0.75,1 1 1 1];% OUTPUT CONSTRAINTS
tfilter=[];

u0=[dyndata.L0 dyndata.V0 dyndata.S0 dyndata.Rl0 dyndata.D dyndata.B];   
d0=[dyndata.Rv0 dyndata.F0 dyndata.zF(1) dyndata.zF(2) dyndata.q];
% sim('Petlyukr12_mpc')
% plotstep(pmod)
% plotstep(dmod)