Variable 'x_opt' is not fully defined on some execution paths.

8 views (last 30 days)
i am write this code in userdefined Simulink function For model predictive control but this code has some error so what i have too do.
function [Sa,Sb,Sc] = control(I_ref,I_meas)
2 % Variables defined in the parameters file
3 % Sampling time of the predictive algorithm [s]
4 Ts = 25e-6;
5 % Load parameters
6 R = 10; % Resistance [Ohm]
7 L = 10e-3; % Inductance [H]
8 e = 100; % Back-EMF peak amplitude [V]
9 f_e = 50*(2*pi); % Back-EMF frequency [rad/s]
10 Vdc = 520; % DC-link voltage [V]
11 % Current reference
12 I_ref_peak = 10; % Peak amplitude [A]
13 f_ref = 50*(2*pi); % Frequency [rad/s]
14 % Voltage vectors
15 v0 = 0;
16 v1 = 2/3*Vdc;
17 v2 = 1/3*Vdc + 1j*sqrt(3)/3*Vdc;
18 v3 = -1/3*Vdc + 1j*sqrt(3)/3*Vdc;
19 v4 = -2/3*Vdc;
20 v5 = -1/3*Vdc - 1j*sqrt(3)/3*Vdc;
21 v6 = 1/3*Vdc - 1j*sqrt(3)/3*Vdc;
22 v7 = 0;
23 v = [v0 v1 v2 v3 v4 v5 v6 v7];
24 % Switching states
25 states = [0 0 0;1 0 0;1 1 0;0 1 0;0 1 1;0 0 1;1 0 1;1 1 1];
26 % Optimum vector and measured current at instant k-1
27 persistent x_old i_old
28 % Initialize values
29 if isempty(x_old)
30 x_old = 1;
31 end
32 if isempty(i_old)
33 i_old = 0+1j*0;
34 end
35 g_opt = 1e10;
36 % Read current reference inputs at sampling instant k
37 ik_ref = I_ref(1) + 1j*I_ref(2);
38 % Read current measurements at sampling instant k
39 ik = I_meas(1) + 1j*I_meas(2);
40 % Back-EMF estimate
41 e = v(x_old) - L/Ts*ik - (R - L/Ts)*i_old;
42 % Store the measured current for the next iteration
43 i_old = ik;
44 for i = 1:8
45 % i-th voltage vector for current prediction
46 v_o1 = v(i);
47 % Current prediction at instant k+1
48 ik1 = (1 - R*Ts/L)*ik + Ts/L*(v_o1 - e);
49 % Cost function
50 g = abs(real(ik_ref - ik1)) + abs(imag(ik_ref - ik1));
51 % Selection of the optimal value
52 if (g<g_opt)
53 g_opt = g;
54 x_opt = i;
55 end
56 end
57 % Store the present value of x_opt
58 x_old = x_opt;
59 % Output switching states
60 Sa = states(x_opt,1);
61 Sb = states(x_opt,2);
62 Sc = states(x_opt,3);
63end
  2 Comments
Walter Roberson
Walter Roberson on 27 Oct 2022
if (g<g_opt)
g_opt = g;
x_opt = i;
end
Suppose that condition is never true, then what is x_opt set to?

Sign in to comment.

Accepted Answer

Walter Roberson
Walter Roberson on 13 Mar 2018
The message is correct. You do not define the value of x_opt under the circumstance that none of the computed g values are less than the 1e10 that you initialize g_opt to.
  5 Comments
Noemi Hernandez-Oliva
Noemi Hernandez-Oliva on 9 Jan 2020
Hello Hussain can you tell me too what is the solution, I sent you an email
Hussain SARWAR KHAN
Hussain SARWAR KHAN on 20 Jan 2020
PFA, which contains the answer/solution of above question.
Remember in your Prayer,
Regards:
Hussain

Sign in to comment.

More Answers (2)

chao wang
chao wang on 6 Nov 2019
I have another way to achieve your goals.

Hussain SARWAR KHAN
Hussain SARWAR KHAN on 20 Jan 2020
PFA, which contains the answer/solution of above question.
Remember in your Prayer,
Regards:
Hussain

Categories

Find more on Model Predictive Control Toolbox in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!