The number of rows in A must be the same as the number of elements of b error

6 vues (au cours des 30 derniers jours)
Kanav
Kanav le 16 Sep 2023
Modifié(e) : Torsten le 16 Sep 2023
[x, fval, exitflag] = maximize_profit
Error using linprog
The number of rows in A must be the same as the number of elements of b.

Error in solution>maximize_profit (line 25)
[x, fval, exitflag] = linprog(c, A, b, Aeq, beq, lb, ub);
function [x, fval, exitflag] = maximize_profit()
% Define the initial guess for decision variables as a vector
x0 = zeros(40, 1);
% Define the coefficients of the objective function as a double vector
c = [-6, 3, 17, 10, 63, 34, 15, 22, -2, 15, ...
-11, -7, -16, 9, 49, 16, 4, 10, -8, 8, ...
7, 3, 16, 13, 60, 25, 12, 19, 4, 13, ...
-4, 0, -3, -48, -15, -7, -17, -9, -3];
% Define the constraint matrix A
A = [eye(10), zeros(10, 30); % X1a to X1j constraints
zeros(10, 10), eye(10), zeros(10, 20); % X2a to X2j constraints
zeros(10, 20), eye(10), zeros(10, 10); % X3a to X3j constraints
zeros(10, 30), eye(10)]; % X4a to X4j constraints
% Define the right-hand side of inequality constraints (b)
b = [30; 40; 50; 60];
% Define the linear equality constraints matrix Aeq
Aeq = [];
beq = [];
% Define the lower and upper bounds for the decision variables
lb = zeros(40, 1);
ub = [];
% Solve the optimization problem
[x, fval, exitflag] = linprog(c, A, b, Aeq, beq, lb, ub);
end
  2 commentaires
Kanav
Kanav le 16 Sep 2023
This is the actual word problem I am trying to code. Maybe this will help you understand why and where I am facing the error?

Connectez-vous pour commenter.

Réponses (1)

Torsten
Torsten le 16 Sep 2023
Modifié(e) : Torsten le 16 Sep 2023
% Define the coefficients of the objective function as a double vector
c = -[-6, 3, 17, 10, 63, 34, 15, 22, -2, 15, ...
-11, -7, -16, 9, 49, 16, 4, 10, -8, 8, ...
7, 3, 16, 13, 60, 25, 12, 19, 4, 13, ...
-1, 0, 13, 3, 48, 15, 7, 17, 9 3];
% Define the constraint matrix A
A = [eye(10),eye(10),eye(10),eye(10)];
b = [30;30;20;20;10;20;20;10;30;10];
% Define matrix Aeq
Aeq = [ones(1,10),zeros(1,10),zeros(1,10),zeros(1,10);
zeros(1,10),ones(1,10),zeros(1,10),zeros(1,10);
zeros(1,10),zeros(1,10),ones(1,10),zeros(1,10);
zeros(1,10),zeros(1,10),zeros(1,10),ones(1,10)];
beq = [30;40;50;60];
% Define the lower and upper bounds for the decision variables
lb = zeros(40, 1);
ub = Inf*ones(40,1);
% Solve the optimization problem
[x, fval, exitflag] = linprog(c, A, b, Aeq, beq, lb, ub);
Optimal solution found.
x(1:10).'
ans = 1×10
0 0 0 0 0 20 10 0 0 0
x(11:20).'
ans = 1×10
0 0 0 20 0 0 10 0 0 10
x(21:30).'
ans = 1×10
30 10 0 0 10 0 0 0 0 0
x(31:40).'
ans = 1×10
0 0 20 0 0 0 0 10 30 0

Produits


Version

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by