clc;
clear;
close all;
%% Time structure
t0 = 0;
tf = 10;
dt = 0.1;
time = t0:dt:tf-dt;
%% System structure ===> x(k+1) = A*x(k) + B*u(k) + G*w(k)
A = [1.02 -0.1;0.1 0.98];
B = [0.5 0;0.05 0.5];
G = [0.3 0;0 0.3];
[nx,nu] = size(B);
%% MPC parameters
Q = eye(nx);
R = eye(nu)*50;
Qf = eye(nu)*50;
N = 26;
%% Problem parameters
M = blkdiag(Q,R);
w1 = randn(1,100);
w1 = w1-mean(w1);
muw1 = mean(w1);
sigmaw1 = var(w1);
w2 = randn(1,100);
w2 = w2-mean(w2);
muw2 = mean(w2);
sigmaw2 = var(w2);
w = [w1;w2];
sigmaw = [sigmaw1 0 ;0 sigmaw2];
x = randn(2,1);
mux{1} = [-2;2];
sigmax{1} = [1,0;0,1];
h1x = [-1/sqrt(5);-2/sqrt(5)];
h2u = [-0.4/sqrt(1.16);1/sqrt(1.16)];
epsilon = 0.5;
g1 = 3;
g2 = 0.2;
c = 0;
%% Main loop
for t = t0:dt:tf-dt
c = c+1;
sigmax = sdpvar(repmat(nx,1,N+1),repmat(nu,1,N+1));
p = sdpvar(repmat(4,1,N-1),repmat(4,1,N-1));
pN = sdpvar(2,2);
F = sdpvar(repmat(2,1,N+1),repmat(2,1,N+1));
K = sdpvar(repmat(2,1,N+1),repmat(2,1,N+1));
muu = sdpvar(repmat(nu,1,N),repmat(1,1,N));
sigmau = sdpvar(repmat(2,1,N),repmat(2,1,N));
constraint = [];
objective = 0;
for k = 1:N-1
mux{k+1} = A*mux{k} + B*muu{k};
objective = objective + 1/2*(trace(M*p{k}));
constraint2 = [sigmax{k+1} (A*sigmax{k}+B*F{k}) G*sigmaw;(A*sigmax{k}+B*F{k})' sigmax{k} zeros(2);(G*sigmaw)' zeros(2) sigmaw]>=0;
constraint3 = [sigmau{k} (F{k});(F{k})' sigmax{k}]>=0;
constraint4 = h1x'*mux{k}<=(1-(0.5*epsilon))*g1-((0.95/2*epsilon*g1*(1-0.95))*h1x'*sigmax{k}*h1x);
constraint5 = h2u'*muu{k}<=(1-(0.5*epsilon))*g2-((0.95/2*epsilon*g2*(1-0.95))*h2u'*sigmau{k}*h2u);
constraint6 = [p{k} [sigmax{k};F{k}] [mux{k};muu{k}];[sigmax{k};F{k}]' sigmax{k} zeros(2,1);[mux{k};muu{k}]' zeros(1,2) ones(1)]>=0;
constraint7 = [F{k} K{k};sigmax{k} eye(2)];
constraint = [constraint,constraint2,constraint3,constraint4,constraint5,constraint6,constraint7];
end
objective = objective + (1/2*trace((trace(Q*pN))));
constraint8 = h1x'*mux{N}<=(1-(0.5*epsilon))*g1-((0.95/2*epsilon*g1*(1-0.95))*h1x'*sigmax{N}*h1x);
constraint9 = [pN-sigmax{N} mux{N};mux{N}' 1];
constraint = [constraint,constraint8,constraint9];
option = sdpsettings('solver','sedumi');
a = optimize (constraint,objective,option);
u = value(muu{1});
U1(c) = u(1,1);
U2(c) = u(2,1);
U3 = [U1;U2];
mux3 = value(mux{1});
mux1(c) = mux3(1,1);
mux2(c) = mux3(2,1);
F = value(F{1});
X = value(sigmax{1});
f1{c} = F;
X1{c} = X;
K = F*inv(X);
% K=value(K{1})
K1{c} = K;
K2 = value(K1{c});
x = value(x);
u1 = u+K2*(x-mux3);
u2{c} = u1;
x = A*x+B*u1+G*w(:,c);
mux{1} = value(mux{2});
sigmax{1} = value(sigmax{2});
ud = value(u2{c});
ud1(c) = ud(1,1);
ud2(c) = ud(2,1);
x1(c) = x(1,1);
x2(c) = x(2,1);
end
%% Plot results
figure(1)
subplot(4,1,1)
plot(time,ud1,'LineWidth',2)
xlabel('time')
ylabel('ud1')
title('Input signal')
grid on
subplot(4,1,2)
plot(time,ud2,'LineWidth',2)
xlabel('time')
ylabel('ud2')
grid on
subplot(4,1,3)
plot(time,x1,'LineWidth',2)
xlabel('time')
ylabel('x1')
title('State 1')
grid on
subplot(4,1,4)
plot(time,x2,'LineWidth',2)
xlabel('time')
ylabel('x2')
title('State 2')
grid on
figure(2)
subplot(4,1,1)
plot(time,x1,'b',time,mux1,'r','LineWidth',2)
xlabel('time')
legend({'x1','Mean of x1'},'Location','northeast')
grid on
subplot(4,1,2)
plot(time,x2,'b',time,mux2,'r','LineWidth',2)
xlabel('time')
legend({'x2','Mean of x2'},'Location','northeast')
grid on
subplot(4,1,3)
plot(mux1,mux2,'LineWidth',2)
title('State space phase plane')
xlabel('mux1')
ylabel('mux2')
grid on
subplot(4,1,4)
plot(U1,U2,'LineWidth',2)
title('Control space phase plane')
xlabel('U1')
ylabel('U2')
grid on
figure(3)
subplot(4,1,1)
plot(time,mux1,'b','LineWidth',1)
title('Mean of first state')
xlabel('Time')
ylabel('mux1')
grid on
subplot(4,1,2)
plot(time,mux2,'b','LineWidth',1)
title('Mean of second state')
xlabel('Time')
ylabel('mux2')
grid on
subplot(4,1,3)
plot(time,U1,'r','LineWidth',1)
title('First input')
xlabel('Time')
ylabel('U1')
grid on
subplot(4,1,4)
plot(time,U2,'r','LineWidth',1)
title('Second input')
xlabel('Time')
ylabel('U2')
grid on