You are on page 1of 5

Real Time Control Sergio Ricardo Lopez Chacon

Exercise Model Predictive Control

Exercise Model Predictive Control


The objective of this exercise is to learn how to implement a predictive control system using receding horizon control,
please describe the process carefully included in the matlab files (mpcgain.m and recept.m). The plant state-space
model is given at the next figure being the weight matrix Qj =0 and Qλ= 1: (Remenber: Save the program in the same
directory as the one that contains the function).
1 1 0.5
𝑥𝑚 (𝑘 + 1) = [ ] 𝑥𝑚 (𝑘) + [ ] 𝑢(𝑘)
0 1 1
𝑦(𝑘) = [1 0]𝑥𝑚 (𝑘)
Once you have solved the MPC exercise, please change control horizon and predictive horizon describing all process
involved in the MPC and implement and comment the next actions as well:

• 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

Explanation 1) A, B and C are defined with the augmented model:

𝐴𝑚 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

Explanation 2) This part of the code defines F and Φ matrices as following:

𝐶𝐴 𝐶𝐵 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)

From the previous equation, we can get the following quantities:


𝛷𝑇 Φ ; 𝛷𝑇 𝐹; 𝛷𝑇 𝑅𝑆

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 3) We store 𝑢(𝑘) and 𝑦(𝑘) for plotting later.

Step 4) We compute a new 𝑥𝑚 𝑎𝑛𝑑 𝑦, following the plant model.


1 1 0.5
𝑥𝑚 (𝑘 + 1) = [ ] 𝑥𝑚 (𝑘) + [ ] 𝑢(𝑘)
0 1 1
𝑦(𝑘) = [1 0]𝑥𝑚 (𝑘)
Step 5) We establish 𝑥(𝑘𝑖 + 1), it is important to notice that the code calls Xf to 𝑥(𝑘𝑖 + 1), in order to generate
the output of the next time step.
𝛥𝑥 (𝑘 + 1)
𝑥(𝑘𝑖 + 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

Figure 1. Plot as a result of the procedure developed in both codes


Now, we will change the control and predictive horizon, as well as, we will establish the value of 𝒓𝒘 = 𝟐.
Considering Np = 15, Nc = 3 and rw = 2, we got the plot showed in Figure 2. We can appreciate that our signal output
(y) reached the set-point, almost at the same time step that in the previous simulation with the initial Np, Nc and 𝑟𝑤
(Figure 1). However, with this new set of parameters, there is no overshoot in the signal output or oscillations in the
control signal. Increasing the value of 𝑟𝑤 generates that the 𝛥𝑈 goes down slowly and cautiously, this creates a
smoother control and output signal.

Figure 2. Output and Control signal, assuming Np=15, Nc=3 and 𝑟𝑤 = 2

4
Real Time Control Sergio Ricardo Lopez Chacon
Exercise Model Predictive Control

We establish a new plant state-space model:


2 1 0.5
𝑥𝑚 (𝑘 + 1) = [ ] 𝑥 (𝑘) + [ ] 𝑢(𝑘)
0 2 𝑚 1.5
𝑦(𝑘) = [1 0]𝑥𝑚 (𝑘)
With this new plant, we will run the model once again for different Np and Nc.

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.

You might also like