fmincon constraints error: Aeq must have 81 column(s).

7 views (last 30 days)
I am trying to minimize the norm of a matrix. But I want to put constraints on my argument x which is a 9x9 matrix such that x*ones(9,1) = ones(9,1) i.e all the rows of matrix x must sum to one
fun = @(x) norm(A*x - B);
rng(4)
x0 = rand(9,9);
lb = zeros(1,81);
ub = ones(1,81)*0.999999;
A = [];
b = [];
Aeq = ones(9,1);
beq = ones(9,1);
nonlcon = [];
options = optimoptions(@fmincon,'Algorithm','sqp','MaxFunctionEvaluations',5000)
x = fmincon(fun,x0,A,b,Aeq,beq,lb,ub,nonlcon, options);
Here A and B are two 15x9 matrices.
I get this error: Aeq must have 81 column(s).
  2 Comments
Ameer Hamza
Ameer Hamza on 10 Apr 2020
What are the values of A and B in
fun = @(x) norm(A*x - B);
AMIT PAWAR
AMIT PAWAR on 10 Apr 2020
A = [0.0020 0.0061 0.0095 0.0155 0.0141 0.0252 0.0321 0.0257 0.0102;
0.0067 0.0087 0.0023 0.0079 0.0058 0.0142 0.0334 0.0214 0.0202;
0.0029 0.0049 0.0000 0.0039 0.0148 0.0126 0.0345 0.0167 0.0174;
0.0000 0.0015 0.0027 0.0033 0.0234 0.0133 0.0286 0.0139 0.0137;
0.0000 0.0005 0.0094 0.0055 0.0243 0.0140 0.0216 0.0123 0.0110;
0.0012 0.0019 0.0121 0.0087 0.0212 0.0140 0.0165 0.0112 0.0093;
0.0029 0.0046 0.0123 0.0111 0.0176 0.0134 0.0133 0.0104 0.0079;
0.0042 0.0074 0.0118 0.0125 0.0146 0.0126 0.0112 0.0096 0.0055;
0.0054 0.0101 0.0109 0.0131 0.0121 0.0118 0.0096 0.0090 0.0000;
0.0055 0.0127 0.0103 0.0139 0.0096 0.0111 0.0074 0.0084 0.0000;
0.0035 0.0141 0.0120 0.0155 0.0062 0.0107 0.0026 0.0077 0.0000;
0.0010 0.0109 0.0186 0.0192 0.0014 0.0106 0.0000 0.0064 0.0000;
0.0001 0.0043 0.0231 0.0245 0.0000 0.0108 0.0000 0.0041 0.0296;
0.0000 0.0008 0.0137 0.0260 0.0000 0.0110 0.0000 0.0041 0.0885;
0.0000 0.0001 0.0038 0.0164 0.0047 0.0102 0.0000 0.0088 0.0600;
0.0000 0.0000 0.0009 0.0060 0.0059 0.0086 0.0000 0.0142 0.0375]
B = [0.0067 0.0087 0.0023 0.0079 0.0058 0.0142 0.0334 0.0214 0.0202;
0.0029 0.0049 0.0000 0.0039 0.0148 0.0126 0.0345 0.0167 0.0174;
0.0000 0.0015 0.0027 0.0033 0.0234 0.0133 0.0286 0.0139 0.0137;
0.0000 0.0005 0.0094 0.0055 0.0243 0.0140 0.0216 0.0123 0.0110;
0.0012 0.0019 0.0121 0.0087 0.0212 0.0140 0.0165 0.0112 0.0093;
0.0029 0.0046 0.0123 0.0111 0.0176 0.0134 0.0133 0.0104 0.0079;
0.0042 0.0074 0.0118 0.0125 0.0146 0.0126 0.0112 0.0096 0.0055;
0.0054 0.0101 0.0109 0.0131 0.0121 0.0118 0.0096 0.0090 0.0000;
0.0055 0.0127 0.0103 0.0139 0.0096 0.0111 0.0074 0.0084 0.0000;
0.0035 0.0141 0.0120 0.0155 0.0062 0.0107 0.0026 0.0077 0.0000;
0.0010 0.0109 0.0186 0.0192 0.0014 0.0106 0.0000 0.0064 0.0000;
0.0001 0.0043 0.0231 0.0245 0.0000 0.0108 0.0000 0.0041 0.0296;
0.0000 0.0008 0.0137 0.0260 0.0000 0.0110 0.0000 0.0041 0.0885;
0.0000 0.0001 0.0038 0.0164 0.0047 0.0102 0.0000 0.0088 0.0600;
0.0000 0.0000 0.0009 0.0060 0.0059 0.0086 0.0000 0.0142 0.0375;
0.0000 0.0000 0.0003 0.0021 0.0041 0.0073 0.0085 0.0167 0.0286]
copy and paste this into your script.

Sign in to comment.

Accepted Answer

Ameer Hamza
Ameer Hamza on 10 Apr 2020
See this example. This consider random values for matrices A and B in fun
rng(0);
A = rand(9);
B = rand(9);
fun = @(x) norm(A*reshape(x,9,9) - B);
rng(4)
x0 = rand(1,81);
lb = zeros(1,81);
ub = ones(1,81)*0.999999;
A = [];
b = [];
Aeq = repelem(eye(9),1,9);
beq = ones(9,1);
nonlcon = [];
options = optimoptions(@fmincon,'Algorithm','sqp','MaxFunctionEvaluations',5000);
x = fmincon(fun,x0,A,b,Aeq,beq,lb,ub,nonlcon, options);
x = reshape(x, 9, 9);

More Answers (0)

Categories

Find more on Linear Programming and Mixed-Integer Linear Programming in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!