Linprog error - The number of rows in A must be the same as the number of elements of f & Error in [x fval] = linprog(f,A,b,Aeq,beq,lb,ub);
Show older comments
Hi , I am trying to do a linprog optimisation by referring to the example in Matlab. I need help to solve the errors. Ty.
my code is:
variables = {'e1','e2','p1','p2','a','c','k','m','y','z'};
N = length(variables);
% create variables for indexing
for v = 1:N
eval([variables{v},' = ', num2str(v),';']);
end
% Lower Bound Constraints
lb = zeros(size(variables));
lb([e1,e2,p1,p2]) = [14.4,14.4,26.4,28.8];
% Upper Bound Constraints
ub = Inf(size(variables));
ub([e1,e2,p1,p2,k,m]) = [48,48,84,84,2.5,2.5];
% Other Constraints
A = zeros(2,4);
A(1,k) = 1; A(1,e2) = -1/36; b(1) = 1.7;
A(2,m) = -1; A(2,p2) = -1/36; b(2) = 1.7;
Aeq = zeros(2,4); beq = zeros(2,1);
Aeq(1,[a,e1]) = [1,-7/30];
Aeq(2,[c,p1]) = [1,-7/60];
% Objective function
if a <= c
y = a;
else
y = c;
end
if k <= m
z = k;
else
z = m;
end
f = z*y*24768;
% Solve the Problem with linprog
[x, fval] = linprog(f,A,b,Aeq,beq,lb,ub);
for d = 1:N
fprintf('%12.2f \t%s\n',x(d),variables{d})
end
fval;
2 Comments
Geoff Hayes
on 22 Feb 2015
Lee - can you describe the problem that you are trying to optimize? For example, which function are you trying to minimize (this would be your f) and subject to what constraints (this would be your A)? The error message is telling you that because you have two rows in your matrix, then your f has to be a 2x1 matrix as well as per an example from linprog.
Accepted Answer
More Answers (0)
Categories
Find more on Linear Programming and Mixed-Integer Linear Programming 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!