fmincon unidentified variable in objective function

5 views (last 30 days)
Hi, I am running a fmincon code to optimize a function. However in my objective function an error occurs that says I have a undefined variable 'x'.
This is however what I want to optimize. Does anyone see what I am doing wrong?
%= [x1 x2 x3 w1 y1 w2 y2 w3 w4]=[v1 v2 v3 v4 v5 v6 v7 v8 v9]
%objective = min 150x1+230x2+260x3+238y1-170w1+210y2-150w2-36w3-10w4
% all constraints are inequality and should all be smaller then
% multiply greater than by -1
FUN= 150*x(1)+230*x(2)+260*x(3)+238*x(5)-170*x(4)+210*x(7)-150*x(6)-36*x(8)-10*x(9);
%FUN= @v 150*v(1)+230*v(2)+260*v(3)+238*v(5)-170*v(4)+210*v(7)-150*v(6)-36*v(8)-10*v(9);
x0=[50,50,50,50,50,50,50,50,50];
A=[ 1 1 1 0 0 0 0 0 0; -2.5 0 0 1 -1 0 0 0 0;0 -3 0 0 0 1 -1 0 0;0 0 -20 0 0 0 0 1 1;0 0 0 0 0 0 0 1 0];
b=[500;-200;-240;0;6000];
lb=[zeros(1,9)];
ub=[];
sol = fmincon(FUN,x0,A,b,[],[],lb,ub)
Thanks

Accepted Answer

Matt J
Matt J on 8 May 2019
Edited: Matt J on 8 May 2019
This should fix it,
FUN= @(x)150*x(1)+230*x(2)+260*x(3)+238*x(5)-170*x(4)+210*x(7)-150*x(6)-36*x(8)-10*x(9);
however, you should really consider using linprog instead. fmincon is overkill for such a simple linear problem.
  4 Comments
Matt J
Matt J on 8 May 2019
Edited: Matt J on 8 May 2019
I think there's a discrepancy between the two threads. I get a bounded solution (the same one) and exitflag=1 with both solvers.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!