This example shows how to solve a scalar minimization problem with nonlinear inequality constraints. The problem is to find $$x$$ that solves

$$\underset{x}{\mathrm{min}}f(x)={e}^{{x}_{1}}\left(4{x}_{1}^{2}+2{x}_{2}^{2}+4{x}_{1}{x}_{2}+2{x}_{2}+1\right),$$

subject to the constraints

$$\begin{array}{l}{x}_{1}{x}_{2}-{x}_{1}-{x}_{2}\le -1.5\\ {x}_{1}{x}_{2}\ge -10.\end{array}$$

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:

$\mathit{c}\left(\mathit{x}\right)=\left[\begin{array}{c}{\mathit{x}}_{1}{\mathit{x}}_{2}-{\mathit{x}}_{1}-{\mathit{x}}_{2}+1.5\\ -10-{\mathit{x}}_{1}{\mathit{x}}_{2}\end{array}\right]$.

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;

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.

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)

Local minimum found that satisfies the constraints. Optimization completed because the objective function is non-decreasing in feasible directions, to within the value of the optimality tolerance, and constraints are satisfied to within the value of the constraint tolerance.

`x = `*1×2*
-9.5473 1.0474

fval = 0.0236

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)

c =2×110^{-4}× -0.3179 -0.3063

ceq = []

Both nonlinear constraints are negative and close to zero, indicating that the solution is feasible and that both constraints are active at the solution.

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