fmincon--How to use multivariable optimization
Show older comments
Hi, I am using 3 or 4 variables to get the min or max of one function
But I cannot get the right answer, I don't know why,could anyone help me solve this problem?
fun=@(x)(x(1)+x(2)+x(3)).*x(2)./(x(1).*(x(2)-x(3)));
lb = [10,10,10];
ub = [100,100,100] ;
x0 = [10,50,100]; % Starting guess at the solution
[x1,fval] = fmincon(fun,x0,[],[],[],[],lb,ub);
xmax2 = x1(1)
ymax2 = x1(2)
zmax2 = x1(3)
max = fval
First of all ,how to add the constraint of X(2) =/(unequal) X(3)?
Second, the right answer is
x = 10 99 100
f =-2.0691e+03
How to get it ? Thank you!!!
Answers (2)
Walter Roberson
on 2 Mar 2022
You would need to use a non-linear constraint to force that x2 ~= x3
However... by examination we can see that if x(2)-x(3) is negative then you are dividing by a negative and the result would be negative. The other terms are all positive or sums of positive, so getting a negative expression due to x(2)-x(3) being negative is going to give you a result that is less positive than any possible answer when x(2)>x(3) .
That tells us that you would be better off constraining x(2)<x(3) . Which is something you can implement as a linear constraint:
A = [0 1 -1]
b = -eps(realmin)
Here, b is the first representable number that is less than 0. You do not use 0 itself because the test is A*x.' <= b and in the case of the two being equal, 1*x(2)+-1*x(3) would be exactly 0 and you do not want 0 as an allowed outcome.
For such a small number of variables, this is very easy to do with a discrete exhaustive search. Here, I use ndgridVecs (download here) instead of ndgrid to save memory.
lb = [10,10,10];
ub = [100,100,100] ;
ranges=arrayfun(@(a,b)a:b, lb,ub,'uni',0);
[x1,x2,x3]=ndgridVecs(ranges{:});
F=(x1+x2+x3).*x2./(x1.*(x2-x3));
[fval,imin]=min(F(:));
[i,j,k]=ind2sub(size(F),imin);
x1=x1(i),
x2=x2(j),
x3=x3(k)
fval,
3 Comments
Young
on 3 Mar 2022
Walter Roberson
on 3 Mar 2022
No, fmincon can use a vector of unknown values, but it cannot do multiple objectives.
Matt J
on 3 Mar 2022
@Young, what is the status of your original question? Has it been addressed by either my answer or Walter's. If so, please Accept-click the appropriate answer and post your new question in a new thread.
However, for multi-objective problems you could look at either fgoalattain , paretosearch, or gamultiobj.
Categories
Find more on Surrogate Optimization 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!