# Model predictive control problem with output graphs

4 views (last 30 days)
Mohamed on 30 Aug 2019
Edited: Mohamed on 30 Aug 2019
I am trying to designed a model predictive control for a dc dc buck converter and I write a code using the Quadratic programming optimazation but the graph out of it is not right, I will incloude the code and the simulink model for it.
Please anyone can help me to fix this preoplem thank you.
Mohamed
% A = [1.1 2; 0 0.95];
% B = [0; 0.0787];
% C = [-1 1];
% D = 0;
A=[0 -250 ; 4.54e3 -227.3];
B=[3e3;0];
C=[1 0];
D=0;
Ts = 1;
sys = ss(A,B,C,D,Ts);
x0 = [0.5;-0.5]; % initial states at [0.5 -0.5]
Qy = 1;
R = 0.01;
K_lqr = lqry(sys,Qy,R);
% t_unconstrained = 0:1:10;
% u_unconstrained = zeros(size(t_unconstrained));
% Unconstrained_LQR = tf([-1 1])*feedback(ss(A,B,eye(2),0,Ts),K_lqr);
% lsim(Unconstrained_LQR,'-',u_unconstrained,t_unconstrained,x0);
% hold on;
M = [A;A^2;A^3;A^4];
CONV = [B zeros(2,1) zeros(2,1) zeros(2,1);...
A*B B zeros(2,1) zeros(2,1);...
A^2*B A*B B zeros(2,1);...
A^3*B A^2*B A*B B];
Q=eye(2);
% Q=[1 0;0 1]
% Q = C'*C;
Q_bar = dlyap((A-B*K_lqr)', Q+K_lqr'*R*K_lqr);
Q_hat = blkdiag(Q,Q,Q,Q);
R_hat = blkdiag(R,R,R,R);
H = CONV'*Q_hat*CONV + R_hat;
F = CONV'*Q_hat*M;
K = H\F;
K_mpc = K(1,:);
% Unconstrained_MPC = tf([-1 1])*feedback(ss(A,B,eye(2),0,Ts),K_mpc);
% lsim(Unconstrained_MPC,'*',u_unconstrained,t_unconstrained,x0)
% legend show
K_lqr;
K_mpc;
%LQR Control Performance Deteriorates When Applying Constraints
x = x0;
t_constrained = 0:40;
for ct = t_constrained
uLQR(ct+1) = -K_lqr*x;
uLQR(ct+1) = max(-1,min(1,uLQR(ct+1)));
x = A*x+B*uLQR(ct+1);
yLQR(ct+1) = C*x;
end
figure
subplot(2,1,1)
plot(t_constrained,uLQR)
xlabel('time')
ylabel('u')
subplot(2,1,2)
plot(t_constrained,yLQR)
xlabel('time')
ylabel('y')
legend('Constrained LQR')
%% MPC Controller Solves QP Problem Online When Applying Constraints
Ac = -[1 0 0 0;...
-1 0 0 0;...
0 1 0 0;...
0 -1 0 0;...
0 0 1 0;...
0 0 -1 0;...
0 0 0 1;...
0 0 0 -1];
b0 = -0.3*([1;1;1;1;1;1;1;1])
L = chol(H,'lower');
Linv = L\eye(size(H,1));
x = x0;
iA = false(size(b0));
opt = mpcqpsolverOptions;
opt.IntegrityChecks = false;
for ct = t_constrained
[u, status, iA] = mpcqpsolver(Linv,F*x,Ac,b0,[],zeros(0,1),iA,opt);
[uMPC] = quadprog(H, F*x, Ac, b0);
uMPC(ct+1) = u(1);
x = A*x+B*uMPC(ct+1);
yMPC(ct+1) = C*x;
end
figure
subplot(2,1,1)
plot(t_constrained,uMPC)
xlabel('time')
ylabel('u')
subplot(2,1,2)
plot(t_constrained,yMPC)
xlabel('time')
ylabel('y')
legend('Constrained MPC')
mdl = 'mpc_customqp';
open_system(mdl)
open_system([mdl '/u_lqr'])
open_system([mdl '/y_lqr'])
open_system([mdl '/u_mpc'])
open_system([mdl '/y_mpc'])
sim(mdl)

### Categories

Find more on Code Generation in Help Center and File Exchange

### Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!