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,be​q,lb,ub);

6 views (last 30 days)
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
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.
lee yz
lee yz on 23 Feb 2015
Edited: lee yz on 23 Feb 2015
actually I'm trying to do optimization to get the value of e1,e2,p1,p2 that can give the highest value of f.
My f formula is f = z*y*24768
My constraints are
14.4<= e1 <48 , 14.4<= e2 <48 , 26.4<= p1 <84 , 28.8<= p2 <84
a = e1/36 , c = p1/36 , k = 7(e2)/30 -1.7 , m = 7(p2)/60 -1.7
y is the lowest value between a and c
z is the lowest value between k and m
if z more than 2.5 then use z=2.5
Thank you. This is my first time using the optimization so I'm not familiar with the coding.

Sign in to comment.

Accepted Answer

John D'Errico
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
lee yz
lee yz on 23 Feb 2015
Can you recommend me any good examples of related particle swarm optimization? I need some references to do this because I'm not familiar with coding. Really appreciate with your help. Thank you.
John D'Errico
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.

Sign in to comment.

More Answers (0)

Categories

Find more on Get Started with Optimization Toolbox 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!