# Why the errors are varies?

3 views (last 30 days)
ZAHIR ZULKIFLY on 14 Nov 2020
Answered: Jaynik on 24 Jun 2024
This is the full code regarding single-step prediction horizon current control. When I run this code, the problems are varies.
1) Sometimes it said Qdt is undefined
2) M and N are not in same array, thus cannot calculate
3) Plot for figure 1 seems that the vector is not in same length
4) Lastly, plot for figure 2, it seems that it cannot receive variable x.
If there are problems regarding the Battery Pack model and bidirectional parameters, I hope that someone can point it out to me.
%MPC current control with one step in the horizon, together with Battery and Bidirectional models%
%Battery Pack model Parameters
%Input SOC0, ib(t) Output SOC(t), Vb(t)
ib = 1.3; %Constant battery current [A]
Qo = 6.5; %Rated battery capacity [Ah]
Rb = 0.0046; %Constant internal resistance [Ohm]
Efull = 1.39; %Fully charged voltage [V]
Eexp = 1.25; %Voltage at the end of the exp. zone [V]
Enom = 1.20; %Voltage at the end of the nominal zone [V]
Qexp = ib*1; %Battery charge at the end of the exp. zone[Ah]
Qnom = 5.2; %Battery charge at the end of the nom. zone[Ah]
A1 = Efull - Eexp; %Exponential zone amplitude [V]
B = 3/Qexp; %Exponential zone time constant inverse [1/Ah]
K = (Efull - Enom + A1*(exp(-B*Qnom)-1))*(Qo - Qnom)/Qnom;%Polarization voltage
SOC0 = 0.6; %Initial battery SOC
tend = 0.5; %Simulation time [s]
dt = 1e-6; %Simulation time step [s]
t = 0:dt:tend;
%Bidirectional Parameters and initial conditions
Lb = 33e-3; %inductance
C2 = 800e-6; %output capacitance
Cb = 700e-6;
vb = 6; %measured disturbance signal
S = 0.5; %steady state duty cycle
vdc = vb/(1-S); %steady state dc bus voltage
iL = vdc/((1-S)*R); %steady state inductor current
%%%%MPC model
%s=1 Q1 on, Q2 off
%s=0 Q1 off, Q2 on
Ts = 5e-5;
vdcref = 12; %DC-link referance voltage
told = 0;
z = 0;
for i = 3:size(t,3)
%Battery
Qdt(i) = -ib(i-1)*dt + Qdt(i-1);%Charge added during dt [Ah]
Qin = Qo*SOC0; %Initial charge [Ah]
Q(i) = Qin + Qdt(i); %Actual battery charge [Ah]
SOC(i) = SOC0 + Qdt(i)/Qo; %Actual state of charge
Eo = Efull + K + Rb*ib(i-1) - A1;%Battery constant voltage [V]
u(i) = Eo - K*Qo/Q(i) + A1*exp(-B*(Qo-Q(i)));%No-load voltage [V]
vb(i) = u(i) - Rb*ib(i-1); %Battery terminal voltage [V]
%MPC
if (t(i)-told > 5*Ts)
z = z+1;
iLk1_1(z) = iL(i-1) + Ts*vb(i-1)/Lb;
iLk1_0(z) = iL(i-1) -Ts*vdc(i-1)/Lb + Ts*vb(i-1)/Lb;
J_1(z) = abs(iLref - iLk1_1(z));
J_0(z) = abs(iLref - iLk1_0(z));
if (J_1(z) < J_0(z))
s = 1;
elseif (J_1(z) > J_0(z))
s = 0;
end
S(z) = s;
told = t(i);
a = [0, 1/Lb, -(1-S(z))/Lb; -1/Cb, -1/(Rb*Cb), 0; (1-S(z))/C2, 0, -1/(R*C2)];%Update matrix
end
%Bidirectional Model - trapezoidal method
M = inv(eye(3) - (((1/2)*dt).*a)) * (eye(3) + ((1/2)*dt).*a);
N = inv(eye(3) - 1/2*dt*a) * 1/2*dt*b;
x(:,i) = M*x(:,i-1) + N*(u(i-1) + u(i));
iL(i) = x(1,i);
vb(i) = x(2,i);
vdc(i) = x(3,i);
ib(i) = iL(i);
end
figure(1)
plot(t,SOC*100)
xlabel('time');
ylabel('SOC %');
figure(2)
plot(t,x)
xlabel('time');
ylabel('Voltage (V), Current (A)');
title('MPC Control Simulation')
legend('iL','vb','vdc')

Jaynik on 24 Jun 2024
Hi Zaheer,
AS per my understanding, the errors are getting generated due to the following:
1. Qdt is undefined: You are using Qdt(i) before it’s defined. You should initialize Qdt before the loop, similar to how you initialized SOC0. For example, you can add Qdt = zeros(size(t)); before the loop.
2. M and N are not in the same array: In your code, M and N are calculated inside the loop, but it seems like you forgot to define b which is used in the calculation of N. You need to define b before you can calculate N.
3. Vector not in same length for Figure 1: This could be because the vectors bring plot have different lengths. Ensure that t and SOC have the same length. You can check this with length(t) and length(SOC).
4. Cannot receive variable x for Figure 2: This might be because x is not defined for all time points. Make sure that x is defined for each point in time t.
Regarding the Battery Pack model and bidirectional parameters, they seem to be defined correctly as per your comments. Without a deeper understanding of the specific application and the mathematical model used, it is hard to say if the parameters are correct. You might want to double-check the values and units of the parameters to make sure they are correct for your specific application.
Hope this helps!