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);
6 views (last 30 days)
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
John D'Errico
on 23 Feb 2015
Um, your objective is a product of z and y, which are in turn derived from the parameters.
This is NOT LINEAR programming. linprog will not be able to solve your problem as you have posed it.
4 Comments
John D'Errico
on 23 Feb 2015
I can't really help you a lot there. But if you are willing to try using linprog, why not try using a tool like this one:
A simple search on the FEX shows literally dozens of those tools.
More Answers (0)
See Also
Categories
Find more on Get Started with Optimization Toolbox 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!