Why did my MPC code not track the references?
I wrote code for the MPC controller to control a stable system to track to the desired value, but the system can not track the desired value, and I can not understand why the MPC controller can not track the desired value. Code I wrote as follows
t0 = 0 ;
tf = 40;
ts = 0.01;
t = t0:ts:tf ;
A = [0,0,0,0,0,1,0,0,0,0;0,0,0,0,0,0,1,0,0,0;0,0,0,0,0,0,0,1,0,0;0,0,0,0,0,0,0,0,1,0;0,0,0,0,0,0,0,0,0,1;-1967.63005780347,983.815028901734,0,0,0,-16.9768786127168,8.48843930635838,0,0,0;983.815028901734,-1967.63005780347,983.815028901734,0,0,8.48843930635838,-16.9768786127168,8.48843930635838,0,0;0,983.815028901734,-1967.63005780347,983.815028901734,0,0,8.48843930635838,-16.9768786127168,8.48843930635838,0;0,0,983.815028901734,-1967.63005780347,983.815028901734,0,0,8.48843930635838,-16.9768786127168,8.48843930635838;0,0,0,983.815028901734,-983.815028901734,0,0,0,8.48843930635838,-8.48843930635838];
B = [0,0,0,0,0;0,0,0,0,0;0,0,0,0,0;0,0,0,0,0;0,0,0,0,0;-0.00289017341040462,0,0,0,0;0,-0.00289017341040462,0,0,0;0,0,-0.00289017341040462,0,0;0,0,0,-0.00289017341040462,0;0,0,0,0,-0.00289017341040462];
C = [1,0,0,0,0,0,0,0,0,0;0,1,0,0,0,0,0,0,0,0;0,0,1,0,0,0,0,0,0,0;0,0,0,1,0,0,0,0,0,0;0,0,0,0,1,0,0,0,0,0;0,0,0,0,0,1,0,0,0,0;0,0,0,0,0,0,1,0,0,0;0,0,0,0,0,0,0,1,0,0;0,0,0,0,0,0,0,0,1,0;0,0,0,0,0,0,0,0,0,1]; ;
sys=ss(A,B,C,0);
sys1=c2d(sys,ts);
Am = sys1.a ;
Bm = sys1.b ;
Cm = sys1.c ;
n = size(Am ,1) ; % number of eigenvalues
q = size(Cm , 1) ; % number of outputs
m = size(Bm , 2) ; % number of inputs
Np =15 ; % Prediction Horizon
Nc = 10; % Control Horizon
F = zeros(q*Np , size(Am , 1)) ;
for i = 1:Np
F(q*i-q+1:q*i , 🙂 = Cm * Am^i ;
end
PHI = zeros(q * Np , m*Nc);
for i = 1:Np
for j = 1:i
PHI(q*i-q+1:q*i , m*j-m+1:m*j) = Cm * Am^(i-j) * Bm ;
end
end
PHI = PHI( : , 1:Nc*m) ;
%% Main Loop
Nt = numel(t) ;
%W = [ones(floor(Nt/4) , 1) ; 2*ones(floor(Nt/4) , 1) ; -ones(floor(Nt/4) , 1) ; zeros(floor(Nt/4+1) , 1)] ;
W = [ones(floor(Nt) , 1)]
y = zeros(q , Nt) ;
du = zeros(m , Nt) ;
u = zeros(m , Nt)
x = zeros(size(Am , 1) , Nt) ;
R = 1 ;
Fval = zeros(Nt , 1 ) ;
for i = 2:Nt-1
FreeResponse = F * x(: , i);
dU = (PHI’*PHI + R * size(PHI , 2))(PHI’ * (W(i) – FreeResponse)) ;
du(: , i) = dU(1:m) ;
x(: , i+1) = Am * x(: , i) + Bm * du(: ,i) ;
y(: , i+1) = Cm * x(: , i+1) ;
endI wrote code for the MPC controller to control a stable system to track to the desired value, but the system can not track the desired value, and I can not understand why the MPC controller can not track the desired value. Code I wrote as follows
t0 = 0 ;
tf = 40;
ts = 0.01;
t = t0:ts:tf ;
A = [0,0,0,0,0,1,0,0,0,0;0,0,0,0,0,0,1,0,0,0;0,0,0,0,0,0,0,1,0,0;0,0,0,0,0,0,0,0,1,0;0,0,0,0,0,0,0,0,0,1;-1967.63005780347,983.815028901734,0,0,0,-16.9768786127168,8.48843930635838,0,0,0;983.815028901734,-1967.63005780347,983.815028901734,0,0,8.48843930635838,-16.9768786127168,8.48843930635838,0,0;0,983.815028901734,-1967.63005780347,983.815028901734,0,0,8.48843930635838,-16.9768786127168,8.48843930635838,0;0,0,983.815028901734,-1967.63005780347,983.815028901734,0,0,8.48843930635838,-16.9768786127168,8.48843930635838;0,0,0,983.815028901734,-983.815028901734,0,0,0,8.48843930635838,-8.48843930635838];
B = [0,0,0,0,0;0,0,0,0,0;0,0,0,0,0;0,0,0,0,0;0,0,0,0,0;-0.00289017341040462,0,0,0,0;0,-0.00289017341040462,0,0,0;0,0,-0.00289017341040462,0,0;0,0,0,-0.00289017341040462,0;0,0,0,0,-0.00289017341040462];
C = [1,0,0,0,0,0,0,0,0,0;0,1,0,0,0,0,0,0,0,0;0,0,1,0,0,0,0,0,0,0;0,0,0,1,0,0,0,0,0,0;0,0,0,0,1,0,0,0,0,0;0,0,0,0,0,1,0,0,0,0;0,0,0,0,0,0,1,0,0,0;0,0,0,0,0,0,0,1,0,0;0,0,0,0,0,0,0,0,1,0;0,0,0,0,0,0,0,0,0,1]; ;
sys=ss(A,B,C,0);
sys1=c2d(sys,ts);
Am = sys1.a ;
Bm = sys1.b ;
Cm = sys1.c ;
n = size(Am ,1) ; % number of eigenvalues
q = size(Cm , 1) ; % number of outputs
m = size(Bm , 2) ; % number of inputs
Np =15 ; % Prediction Horizon
Nc = 10; % Control Horizon
F = zeros(q*Np , size(Am , 1)) ;
for i = 1:Np
F(q*i-q+1:q*i , 🙂 = Cm * Am^i ;
end
PHI = zeros(q * Np , m*Nc);
for i = 1:Np
for j = 1:i
PHI(q*i-q+1:q*i , m*j-m+1:m*j) = Cm * Am^(i-j) * Bm ;
end
end
PHI = PHI( : , 1:Nc*m) ;
%% Main Loop
Nt = numel(t) ;
%W = [ones(floor(Nt/4) , 1) ; 2*ones(floor(Nt/4) , 1) ; -ones(floor(Nt/4) , 1) ; zeros(floor(Nt/4+1) , 1)] ;
W = [ones(floor(Nt) , 1)]
y = zeros(q , Nt) ;
du = zeros(m , Nt) ;
u = zeros(m , Nt)
x = zeros(size(Am , 1) , Nt) ;
R = 1 ;
Fval = zeros(Nt , 1 ) ;
for i = 2:Nt-1
FreeResponse = F * x(: , i);
dU = (PHI’*PHI + R * size(PHI , 2))(PHI’ * (W(i) – FreeResponse)) ;
du(: , i) = dU(1:m) ;
x(: , i+1) = Am * x(: , i) + Bm * du(: ,i) ;
y(: , i+1) = Cm * x(: , i+1) ;
end I wrote code for the MPC controller to control a stable system to track to the desired value, but the system can not track the desired value, and I can not understand why the MPC controller can not track the desired value. Code I wrote as follows
t0 = 0 ;
tf = 40;
ts = 0.01;
t = t0:ts:tf ;
A = [0,0,0,0,0,1,0,0,0,0;0,0,0,0,0,0,1,0,0,0;0,0,0,0,0,0,0,1,0,0;0,0,0,0,0,0,0,0,1,0;0,0,0,0,0,0,0,0,0,1;-1967.63005780347,983.815028901734,0,0,0,-16.9768786127168,8.48843930635838,0,0,0;983.815028901734,-1967.63005780347,983.815028901734,0,0,8.48843930635838,-16.9768786127168,8.48843930635838,0,0;0,983.815028901734,-1967.63005780347,983.815028901734,0,0,8.48843930635838,-16.9768786127168,8.48843930635838,0;0,0,983.815028901734,-1967.63005780347,983.815028901734,0,0,8.48843930635838,-16.9768786127168,8.48843930635838;0,0,0,983.815028901734,-983.815028901734,0,0,0,8.48843930635838,-8.48843930635838];
B = [0,0,0,0,0;0,0,0,0,0;0,0,0,0,0;0,0,0,0,0;0,0,0,0,0;-0.00289017341040462,0,0,0,0;0,-0.00289017341040462,0,0,0;0,0,-0.00289017341040462,0,0;0,0,0,-0.00289017341040462,0;0,0,0,0,-0.00289017341040462];
C = [1,0,0,0,0,0,0,0,0,0;0,1,0,0,0,0,0,0,0,0;0,0,1,0,0,0,0,0,0,0;0,0,0,1,0,0,0,0,0,0;0,0,0,0,1,0,0,0,0,0;0,0,0,0,0,1,0,0,0,0;0,0,0,0,0,0,1,0,0,0;0,0,0,0,0,0,0,1,0,0;0,0,0,0,0,0,0,0,1,0;0,0,0,0,0,0,0,0,0,1]; ;
sys=ss(A,B,C,0);
sys1=c2d(sys,ts);
Am = sys1.a ;
Bm = sys1.b ;
Cm = sys1.c ;
n = size(Am ,1) ; % number of eigenvalues
q = size(Cm , 1) ; % number of outputs
m = size(Bm , 2) ; % number of inputs
Np =15 ; % Prediction Horizon
Nc = 10; % Control Horizon
F = zeros(q*Np , size(Am , 1)) ;
for i = 1:Np
F(q*i-q+1:q*i , 🙂 = Cm * Am^i ;
end
PHI = zeros(q * Np , m*Nc);
for i = 1:Np
for j = 1:i
PHI(q*i-q+1:q*i , m*j-m+1:m*j) = Cm * Am^(i-j) * Bm ;
end
end
PHI = PHI( : , 1:Nc*m) ;
%% Main Loop
Nt = numel(t) ;
%W = [ones(floor(Nt/4) , 1) ; 2*ones(floor(Nt/4) , 1) ; -ones(floor(Nt/4) , 1) ; zeros(floor(Nt/4+1) , 1)] ;
W = [ones(floor(Nt) , 1)]
y = zeros(q , Nt) ;
du = zeros(m , Nt) ;
u = zeros(m , Nt)
x = zeros(size(Am , 1) , Nt) ;
R = 1 ;
Fval = zeros(Nt , 1 ) ;
for i = 2:Nt-1
FreeResponse = F * x(: , i);
dU = (PHI’*PHI + R * size(PHI , 2))(PHI’ * (W(i) – FreeResponse)) ;
du(: , i) = dU(1:m) ;
x(: , i+1) = Am * x(: , i) + Bm * du(: ,i) ;
y(: , i+1) = Cm * x(: , i+1) ;
end mpc MATLAB Answers — New Questions
​