The number of rows in A must be the same as the number of elements of b error
20 views (last 30 days)
Show older comments
[x, fval, exitflag] = maximize_profit
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 Comments
Dyuman Joshi
on 16 Sep 2023
Your definition of the optimization problem is not consistent -
c is 1x39, A is 40x40, b is 4x1.
It seems c is missing an element and b is supposed to be 40x1.
If I were to guess, b might be -
b = [30*ones(10,1); 40*ones(10,1); 50*ones(10,1); 60*ones(10,1)];
However, only you know what you are attempting to solve. So, make the appropriate changes and your code should be working fine.
Answers (1)
Torsten
on 16 Sep 2023
Edited: Torsten
on 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);
x(1:10).'
x(11:20).'
x(21:30).'
x(31:40).'
0 Comments
See Also
Categories
Find more on Genetic Algorithm 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!