fmincon unidentified variable in objective function

1 次查看(过去 30 天)
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

采纳的回答

Matt J
Matt J 2019-5-8
编辑:Matt J 2019-5-8
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 个评论
John D'Errico
John D'Errico 2019-5-8
Yep. bus14 asked the same question again. I showed that the vector
120
80
300
100
k
0
k
6000
0
yields an arbitrarily large negative result for any positive k, is always feasible, and clearly satisfies the bound constraints.
Matt J
Matt J 2019-5-8
编辑:Matt J 2019-5-8
I think there's a discrepancy between the two threads. I get a bounded solution (the same one) and exitflag=1 with both solvers.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Solver Outputs and Iterative Display 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by