Why do i get "Error using vertcat Dimensions of arrays being concatenated are not consistent" while using fmincon to do nonlinear optimization
10 views (last 30 days)
Show older comments
TSAM KIT CHAN
on 31 Mar 2024 at 20:28
Commented: TSAM KIT CHAN
on 31 Mar 2024 at 21:07
i am currently working on a fmincon codes about power optimization with non linear constraints.
i have met this error. However, as i searched, it seems appear for error in matrix calculation. there really isnt matrix calcualtion in my codes
"Error using vertcat Dimensions of arrays being concatenated are not consistent"
Below are my codes
%% Define Objective Function
ai = 1;
bi = 1;
ci = 1;
di = 1;
ei = 1;
fun = @(x) ai*x(1) + bi*x(2) + ci*x(3) + di*x(4) + ei*x(5)
function Impedance= Z(R,L,C)
w = 3.141592653589793e+02;
Impedance = R+L*w*j-C*w*j;
end
function PAngle = Pangle(P,Q)
PAngle = atan(Q/P);
end
function ZAngle = Zangle(Z)
ZAngle = atan( (imag(Z)/j) /(real(Z)));
end
%% Set Parameters for fmincon
A = [];
b = [];
Aeq = [];
beq = [];
lb = [0,0,0,0,0];
ub = [inf,inf,inf,inf,inf];
%% Provide Initial Point for fmincon%
x0 = zeros(5,1);
%% Define Nonlinear Constraints
nonlcon = @(x)power(x);
%% Set Parameters for fmincon and Call fmincon
%options = optimoptions('fmincon','Algorithm','sqp');
%[x,fval] = fmincon(@(x)fun(x),x0,A,b,Aeq,beq,lb,ub,nonlcon,options);
[x,fval] = fmincon(@(x)fun(x),x0,A,b,Aeq,beq,lb,ub,nonlcon);
function [c,ceq] = power(x)
Z12 = Z(88e-3,130e-6,0);
Z15 = Z(13e-3,280e-6,0);
Z35 = Z(78e-3,50e-3,0);
Z45 = Z(29e-3,467e-6,0);
Z6 = Z(30e-3,147e-6,0);
w = 3.141592653589793e+02;
Y12 = 1/Z12;
Y15 = 1/Z15;
Y35 = 1/Z35;
Y45 = 1/Z45;
Y6 = 1/Z6;
Ym = [Y15+Y12 -Y12 0 0 -Y15 0;
-Y12 Y12 0 0 0 0;
0 0 Y35 0 -Y35 0;
0 0 0 Y45 -Y45 0;
-Y15 0 -Y35 -Y45 Y15+Y35+Y45+Y6 -Y6;
0 0 0 0 -Y6 Y6;
];
Gm = real(Ym);
Bm = imag(Ym)*j;
PL1 = 24e3;
QL1 = 18e3;
PL2 = 41e3;
QL2 = 28e3;
PL3 = 50e3;
QL3 = 34e3;
PL4 = 44e3;
G15 = -Gm(1,5);
G12 = -Gm(1,2);
G21 = G12;
G35 = -Gm(3,5);
G45 = -Gm(4,5);
G51 = G15;
G53 = G35;
G56 = -Gm(5,6);
G65 = G56;
B15 = -Bm(1,5);
B12 = -Bm(1,2);
B21 = B12;
B35 = -Bm(3,5);
B45 = -Bm(4,5);
B51 = B15;
B53 = B35;
B56 = -Bm(5,6);
B65 = B56;
cos15 = cos(0-Zangle(Z15));
sin15 = sin(0-Zangle(Z15));
cos12 = cos(0-Zangle(Z12));
sin12 = sin(0-Zangle(Z12));
cos35 = cos(Pangle(50e3,34e3)-Zangle(Z35));
sin35 = sin(Pangle(50e3,34e3)-Zangle(Z35));
cos45 = cos(Pangle(44e3,0)-Zangle(Z45));
sin45 = sin(Pangle(44e3,0)-Zangle(Z45));
cos51 = cos(Zangle(Z15));
sin51 = sin(Zangle(Z15));
cos53 = -cos35;
sin53 = -sin35;
cos54 = -cos45;
sin54 = -sin45;
cos65 = cos(0-Zangle(Z6));
sin65 = sin(0-Zangle(Z6));
cos56 = -cos65;
sin56 = -sin65;
V1 = 600;
V2 = 600;
V3 = 600;
V4 = 230;
V5 = 230;
V6 = 230;
display(x(1));
eq1 = V1*(V5* (G15*cos15+w*B15*sin15) +V2* (G15*cos12+w*B15*sin12) )+PL1;
eq2 = V2*(V1* (G21*cos12+w*B21*sin12)) +PL2;
eq3 = V3*(V5* (G35*cos12+w*B35*sin35)) +PL3;
eq4 = V5*(V1* (G15*cos51+w*B15*sin51) + V3* (G35*cos53+w*B35*sin53)+V4* (G45*sin54+w*B45*cos54) +V6* (G56*sin56+w*B56*cos56));
eq5 = V6*(V5* (G65*cos65+w*B65*sin65));
%ceq=[-x(1)+eq1;
% -x(2)+eq2;
% -x(3)+eq3;
% -x(5)+eq4;
% -x(4)+eq5;
%];
ceq=[-x(1)+V1*(V5* (G15*cos15+w*B15*sin15) +V2* (G15*cos12+w*B15*sin12) )+PL1;
-x(2)+V2*(V1* (G21*cos12+w*B21*sin12)) +PL2;
-x(3)+V3*(V5* (G35*cos12+w*B35*sin35)) +PL3;
-x(5)+V5*(V1* (G15*cos51+w*B15*sin51) + V3* (G35*cos53+w*B35*sin53)+V4* (G45*sin54+w*B45*cos54) +V6* (G56*sin56+w*B56*cos56));
-x(4)+V6*(V5* (G65*cos65+w*B65*sin65));
];
c=[];
end
0 Comments
Accepted Answer
Matt J
on 31 Mar 2024 at 20:43
Moved: Matt J
on 31 Mar 2024 at 20:43
However, as i searched, it seems appear for error in matrix calculation. there really isnt matrix calcualtion in my codes
Certainly you do. When you create the matrix Ym, that is a matrix operation. So is the creation of ceq. In any case, you don't seem to be using Matlab's debugging tools to trap the error, so you should do that.
2 Comments
Matt J
on 31 Mar 2024 at 20:52
It appears that you want to have
ceq=[-x(1)+V1*(V5* (G15*cos15+w*B15*sin15)+V2*(G15*cos12+w*B15*sin12) )+PL1;
-x(2)+V2*(V1*(G21*cos12+w*B21*sin12))+PL2;
-x(3)+V3*(V5*(G35*cos12+w*B35*sin35))+PL3;
-x(5)+V5*(V1*(G15*cos51+w*B15*sin51)+V3*(G35*cos53+w*B35*sin53)+V4*(G45*sin54+w*B45*cos54)+V6* (G56*sin56+w*B56*cos56));
-x(4)+V6*(V5*(G65*cos65+w*B65*sin65));
];
however, this leads to complex-valued output, so you still have a problem,
>> [c,ceq]=nonlcon(x0)
0
c =
[]
ceq =
1.0e+08 *
6.5993 + 4.8771i
2.6097 + 0.0000i
0.0006 + 0.0228i
-2.3747 - 4.8993i
0.0000 + 3.3332i
More Answers (0)
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!