%% Obtain the Objective Function Value
% Call fmincon with the fval output to obtain the value of the objective function at the solution.
% The Bound Constraints example shows two solutions. Which is better? Run the example requesting the fval output as well as the solution.

fun = @(x)1+x(1)./(1+x(2)) - 3*x(1).*x(2) + x(2).*(1+x(1));
lb = [0,0];
ub = [1,2];
A = [];
b = [];
Aeq = [];
beq = [];
x0 = (lb + ub)/2;
[x,fval] = fmincon(fun,x0,A,b,Aeq,beq,lb,ub)

% Run the problem using a different starting point x0.

x0 = x0/5;
[x2,fval2] = fmincon(fun,x0,A,b,Aeq,beq,lb,ub)

% This solution has an objective function value fval2 = 1, which is higher than the first value fval = –0.6667. The first solution x has a lower local minimum objective function value.
% Copyright 2019 The MathWorks, Inc.