clc
close all
clear all
yalmip('clear')
%% Model Parameters and State-space matrices
data = xlsread('09012018data.csv');
% sampling data every 5 minutes
NoM = 5;
sampling_period = NoM*60;
sampled_data = [];
for i = 1:sampling_period:length(data)
sampled_data = [sampled_data;data(i,:)];
end
Time = sampled_data(:,1);
dTsetE = sampled_data(:,2);
dFlow = sampled_data(:,3);
dTiE = sampled_data(:,4);
dTiC = sampled_data(:,5);
dToutE = sampled_data(:,6);
dPout = sampled_data(:,7);
dCOP = sampled_data(:,8);
A = [-0.055643256 -2.79E-07 0.000971282 1 0 0
10354.94655 0.223207545 356.7890554 0 1 0
-6.467478387 2.81E-05 0.022706046 0 0 1
-0.014415444 2.60E-07 0.002964755 0 0 0
-812.961162 0.022406063 -30.49280997 0 0 0
0.018877885 -5.01E-06 0.084396962 0 0 0];
B = [0.017021927 -0.001889088 -0.015300244 -0.007369483
2226.598087 -190.7771176 -348.0510377 -89.55780513
-0.030880581 -0.009586317 0.286861591 -0.025348384
-0.003255077 -0.001385605 -0.012293342 7.07E-05
827.9699533 -168.1696995 -1583.890508 -6.360500619
-0.531436147 0.050092133 0.495164075 0.004485626];
B1 = [0.017021927 -0.001889088
2226.598087 -190.7771176
-0.030880581 -0.009586317
-0.003255077 -0.001385605
827.9699533 -168.1696995
-0.531436147 0.050092133]; % corresponding to Tset and Flow
B2 = [-0.015300244 -0.007369483
-348.0510377 -89.55780513
0.286861591 -0.025348384
-0.012293342 7.07E-05
-1583.890508 -6.360500619
0.495164075 0.004485626]; % corresponding to TiE, ToC
C = [1 0 0 0 0 0;
0 1 0 0 0 0;
0 0 1 0 0 0];
Cr = [1 0 0 0 0 0;
0 1 0 0 0 0];
D = [0.973267178 0.016742346 0.035864605 0.006784957
68966.72868 -6630.599254 -66807.43608 35.43101393
-2.424358679 0.19654465 1.896918069 0.053732785];
Dr = [0.973267178 0.016742346 0.035864605 0.006784957
68966.72868 -6630.599254 -66807.43608 35.43101393];
Dr1 = [0.973267178 0.016742346
68966.72868 -6630.599254];
Dr2 = [ 0.035864605 0.006784957
-66807.43608 35.43101393];
D1 = [0.973267178 0.016742346
68966.72868 -6630.599254
-2.424358679 0.19654465 ];
D2 = [ 0.035864605 0.006784957
-66807.43608 35.43101393
1.896918069 0.053732785];
nx = length(A); % Number of states
[ny , nu] = size(D); % Number of inputs and outputs
%% MPC problem
N = 5; % MPC horizon
% output objective 'ToutEvaporator','Power','COP'
Wq = 1;
Q = Wq*eye(ny); % Input weighting matrix
% Output reference
ToEref = 281.15; % Set to be 8 C
Pref = 0; % P to reach as close as possible to 0
% Output Constraints
ToEmax = 285.15; % This is the max value in physical system (12 C)
ToEmin = 275.15; % This is the min value in physical system (2 C)
% Pmax = max(dPout); % max value of data
Pmax = 10e6;
% Pmin = min(dPout); % min value of data
Pmin = 0; % min value of data
% Input objective weighting
Wr = 1; % state weighting gain
R = Wr*eye(nu); % state weighting matrix
% Input constraints
TsetEmax = max(dTsetE);
TsetEmin = min(dTsetE);
Flowmax = max(dFlow);
% Flowmin = min(dFlow);
Flowmin = 0;
%% Optimization problem: Decision variables, objective, constraints
% Vector of inputs including decision variables
ut = sdpvar(repmat(2,1,N),repmat(1,1,N)); % TsetE and Flow
x = sdpvar(repmat(nx,1,N+1),repmat(1,1,N+1)); % states
yt = sdpvar(repmat(2,1,N+1),repmat(1,1,N+1)); % outputs
constraints = [];
objective = 0;
for h = 1:N
% disturbance vector [Load;weather]
w = [dTiE(h);dTiC(h)];
% Minimization of the term (TsetE - ToE)
objective = objective + (ut{h}(1,:)-yt{h}(1,:))'*1*(ut{h}(1,:)-yt{h}(1,:));
% Power minimization
objective = objective + (yt{h}(2,:)-Pref)'*1*(yt{h}(2,:)-Pref);
% Minimization of Tset, mFlow
% objective = objective + (ut{h}(1,:) - ur)'*R*(ut{h}(1,:) - ur);
% System Dynamics
constraints = [constraints, x{h+1} == A*x{h} + B1*ut{h} + B2*w];
constraints = [constraints, yt{h} == Cr*x{h} + Dr1*ut{h} + Dr2*w];
constraints = [constraints, TsetEmin<= ut{h}(1,:) <= TsetEmax];
constraints = [constraints, Flowmin<= ut{h}(2,:) <= Flowmax];
constraints = [constraints, Pmin <= yt{h}(2,:) <= Pmax];
end
ops = sdpsettings('verbose',2,'solver','sdpt3'); % 'qpoases' , 'mosek'
controller = optimizer(constraints,objective,ops,x{1},[ut{:}]);
x = zeros(nx,1);
control_history = [];
state_history = [];
output_history = [];
Nsim = length(sampled_data);
for i = 1:Nsim
[U,errorcode] = controller{x};
if errorcode
yalmiperror(errorcode)
break
end
w = [dTiE(h);dTiC(h)];
% y = [yt{h};dCOP(h)];
Uf = [U(:,1);w];
x = A*x + B1*U(:,1) + B2*w;
y = C*x + D1*U(:,1) + D2*w;
control_history = [control_history,Uf];
state_history = [state_history,x];
output_history = [output_history,y];
end
value(objective)