Professional Documents
Culture Documents
• Change the weight 𝑟𝑤 in the design to 2 and observe and describe the closed-loop behavior.
• Create your own plant using different Ap, Bp and Cp, and experiment with different prediction and control
horizons.
Solution
We have two matlab scripts, which describe a receding horizon control method implemented in the predictive control
system. We will describe both scripts to understand what they are doing:
1. We define the plant, Dp=0 because the out y(k) cannot depend on the input u(k) at the same time.
Ap=[1 1;0 1];
Bp=[0.5;1];
Cp=[1 0];
Dp=0;
2. We define Nc and Np. Nc is the control horizon, this represents “the number of parameters to capture the future
control trajectory” (Wang, 2009), and Np is the prediction horizon.
Np=20;
Nc=4;
3. We generate the gain matrices with mpcgain.m. We will see how this function generates these matrices and
after that, we will call the function.
mpcgain.m
function [Phi_Phi,Phi_F,Phi_R,A_e,B_e,C_e]=mpcgain(Ap,Bp,Cp,Nc,Np);
%This function needs the plant, control horizon and prediction horizon as
%arguments
[m1,n1]=size(Cp);
[n1,n_in]=size(Bp);
A_e=eye(n1+m1,n1+m1);
A_e(1:n1,1:n1)=Ap;
A_e(n1+1:n1+m1,1:n1)=Cp*Ap;
B_e=zeros(n1+m1,n_in);
B_e(1:n1,:)=Bp;
B_e(n1+1:n1+m1,:)=Cp*Bp;
C_e=zeros(m1,n1+m1);
C_e(:,n1+1:n1+m1)=eye(m1,m1);
1
Real Time Control Sergio Ricardo Lopez Chacon
Exercise Model Predictive Control
𝐴𝑚 0𝑇𝑚 𝐵
𝐴=[ ]; 𝐵 = [ 𝑚 ] ; 𝐶 = [0𝑚 1]
𝐶𝑚 𝐴𝑚 1 𝐶𝑚 𝐵𝑚
n=n1+m1;
h(1,:)=C_e;
F(1,:)=C_e*A_e;
for kk=2:Np
h(kk,:)=h(kk-1,:)*A_e;
F(kk,:)= F(kk-1,:)*A_e;
end
v=h*B_e;
Phi=zeros(Np,Nc); %declare the dimension of Phi
Phi(:,1)=v; % first column of Phi
for i=2:Nc
Phi(:,i)=[zeros(i-1,1);v(1:Np-i+1,1)]; %Toeplitz matrix
end
𝐶𝐴 𝐶𝐵 0 … 0
𝐶𝐴2 𝐶𝐴𝐵 𝐶𝐵 … 0
3 2 𝐶𝐴𝐵 …. 0.
𝐹 = 𝐶𝐴. ; 𝛷 = 𝐶𝐴. 𝐵 .
. . . . .
…
[𝐶𝐴𝑁𝑝 ] [𝐶𝐴𝑁𝑝 −1 𝐵 𝐶𝐴𝑁𝑝 −2 𝐵 𝐶𝐴𝑁𝑝 −𝑁𝐶 𝐵]
BarRs=ones(Np,1);
Phi_Phi= Phi'*Phi; % 𝛷𝑇 Φ
Phi_F= Phi'*F; %𝛷𝑇 𝐹
Phi_R=Phi'*BarRs; %𝛷𝑇 𝑅𝑆
Explanation 3) In order to get an optimal ΔU to minimize the error, we use this expression:
𝛥𝑈 = (𝛷𝑇 Φ + ̅̅̅
𝑅)−1 Φ𝑇 (𝑅𝑆 − 𝐹𝑥(𝑘𝑖 )) (1)
These are the expressions calculated in the code, being 𝑅̅ = 𝑟𝑤 𝐼𝑁𝑐𝑥𝑁𝑐 where 𝑟𝑤 is a tuning parameter for the
performance, also called weight factor and 𝐼 is the identity matrix. 𝑅𝑆 = [1 1 1 1 … 1]𝑇 𝑟(𝑘𝑖 ), the first term of this
expression has a length equal to Np and 𝑟(𝑘𝑖 ) is the set point signal.
4. Now, we go back to the script recept.m. we call the function mpcgain from the previous script to obtain the
gain matrices (𝛷𝑇 Φ ; 𝛷𝑇 𝐹; 𝛷𝑇 𝑅𝑆 ) and we establish the initial conditions:
𝛥𝑥 (𝑘 ) 0
𝑥(𝑘𝑖 ) = [ 𝑚 𝑖 ] = [ ]
𝑦(𝑘𝑖 ) 0
𝑢(𝑘𝑖 − 1) = 0
[Phi_Phi,Phi_F,Phi_R,A_e, B_e,C_e] = mpcgain(Ap,Bp,Cp,Nc,Np);
[n,n_in]=size(B_e);
xm=[0;0];
Xf=zeros(n,1);
2
Real Time Control Sergio Ricardo Lopez Chacon
Exercise Model Predictive Control
N_sim=100;
r=ones(N_sim,1);
u=0; % u(k-1) =0
y=0;
5. We develop the loop to create the procedure of Receding Horizon Control. Inside the loop, we followed these
steps:
Step 1) Calculate 𝛥𝑈, the code calculates this using the equation (1).
𝛥𝑈 = (𝛷𝑇 Φ + ̅̅̅
𝑅)−1 Φ𝑇 (𝑅𝑆 − 𝐹𝑥(𝑘𝑖 )) (1)
Step 2) Take the first term of 𝛥𝑈 matrix, 𝛥𝑈(1) and add this term to 𝑢(𝑘 − 1) to get 𝑢(𝑘),
𝑢(𝑘) = 𝑢(𝑘 − 1) + 𝛥𝑈(1).
Step 6) We go back to the Step 1) with the new values of 𝑥(𝑘𝑖 + 1) and we will start again.
for kk=1:N_sim;
DeltaU=inv(Phi_Phi+0.1*eye(Nc,Nc))*(Phi_R*r(kk)-Phi_F*Xf);%Notice that rw =0.1
%in the code
deltau=DeltaU(1,1);
u=u+deltau;
u1(kk)=u;
y1(kk)=y;
xm_old=xm;
xm=Ap*xm+Bp*u;
y=Cp*xm;
Xf=[xm-xm_old;y];
End
6. Finally, we plot the results of the input (u) and output signal (y).
k=0:(N_sim-1);
figure
subplot(211)
plot(k,y1)
xlabel('Sampling Instant')
legend('Output')
subplot(212)
plot(k,u1)
xlabel('Sampling Instant')
legend('Control')
3
Real Time Control Sergio Ricardo Lopez Chacon
Exercise Model Predictive Control
4
Real Time Control Sergio Ricardo Lopez Chacon
Exercise Model Predictive Control
Figure 3 Output and control signal, Np=20, Nc=5 and Figure 4 Output and control signal, Np=20, Nc=3 and
𝑟𝑤 = 2 𝑟𝑤 = 2
We kept 𝑟𝑤 = 2 for the new simulations. In Figure 3, we can appreciate small oscillations in the output and control
signal. However, the output signal reaches the set-point faster when we assume Nc=5 than when we assume Nc=3,
showing us a higher and aggressive control. Nevertheless, when we assume Nc = 3, we do not have oscillations (Figure
4). We also observe that the control signal u tends to one instead of zero due to the change of Ap and Bp in the plant
state-space model.
Reference
Wang, L., (2009) “Model Predictive Control System Design and Implementation Using MATLAB”, Advances in
Industrial Control, Springer – Verlag London Limited.