%% Nonlinear Inequality Constraints
% This example shows how to solve a scalar minimization problem with nonlinear inequality constraints. The problem is to find x that solves
% \[\mathop {\min }\limits_x f(x) = {e^{{x_1}}}\left( {4x_1^2 + 2x_2^2 + 4{x_1}{x_2} + 2{x_2} + 1} \right),\]
% subject to the constraints
% x_1 x_2 - x_1 - x_2 \le -1.5\\
% x_1 x_2 \ge -10.
% Because neither of the constraints is linear, create a function, confun.m, that returns the value of both constraints in a vector c. Because the fmincon solver expects the constraints to be written in the form c(x) \le  0, write your constraint function to return the following value:
% c\left(x\right)=\left\lbrack \begin{array}{c}
% x_1 x_2 -x_1 -x_2 +1\ldotp 5\\
% -10-x_1 x_2
% \end{array}\right\rbrack.
%% Create Objective Function
% The helper function objfun is the objective function; it appears at the end of this example. Set the fun argument as a function handle to the objfun function.

fun = @objfun;

%
%% Create Nonlinear Constraint Function
% Nonlinear constraint functions must return two arguments: c, the inequality constraint, and ceq, the equality constraint. Because this problem has no equality constraint, the helper function confun at the end of this example returns [] as the equality constraint.
%
%% Solve Problem
% Set the initial point to [-1,1].

x0 = [-1,1];

% The problem has no bounds or linear constraints. Set those arguments to [].

A = [];
b = [];
Aeq = [];
beq = [];
lb = [];
ub = [];

% Solve the problem using fmincon.

[x,fval] = fmincon(fun,x0,A,b,Aeq,beq,lb,ub,@confun)

%
%% Examine Solution
% The exit message indicates that the solution is feasible with respect to the constraints. To double-check, evaluate the nonlinear constraint function at the solution. Negative values indicate satisfied constraints.

[c,ceq] = confun(x)

% Both nonlinear constraints are negative and close to zero, indicating that the solution is feasible and that both constraints are active at the solution.
%
%% Helper Functions
% This code creates the objfun helper function.

function f = objfun(x)
f = exp(x(1))*(4*x(1)^2 + 2*x(2)^2 + 4*x(1)*x(2) + 2*x(2) + 1);
end

% This code creates the confun helper function.

function [c,ceq] = confun(x)
% Nonlinear inequality constraints
c = [1.5 + x(1)*x(2) - x(1) - x(2);     
     -x(1)*x(2) - 10];
% Nonlinear equality constraints
ceq = [];
end

% Copyright 2019 The MathWorks, Inc.