Note: This page has been translated by MathWorks. Please click here

To view all translated materals including this page, select Japan from the country navigator on the bottom of this page.

To view all translated materals including this page, select Japan from the country navigator on the bottom of this page.

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).$$ | (6-56) |

subject to the constraints

*x*_{1}*x*_{2} – *x*_{1} – *x*_{2} ≤
–1.5,*x*_{1}*x*_{2} ≥
–10.

Because neither of the constraints is linear, you cannot pass
the constraints to `fmincon`

at
the command line. Instead you can create a second file, `confun.m`

,
that returns the value at both constraints at the current `x`

in
a vector `c`

. The constrained optimizer, `fmincon`

,
is then invoked. Because `fmincon`

expects the constraints
to be written in the form *c*(*x*) ≤ 0, you
must rewrite your constraints in the form

x_{1}x_{2} – x_{1} – x_{2} +
1.5 ≤ 0,– x_{1}x_{2} –10
≤ 0. | (6-57) |

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);

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 = [];

x0 = [-1,1]; % Make a starting guess at the solution options = optimoptions(@fmincon,'Algorithm','sqp'); [x,fval] = ... fmincon(@objfun,x0,[],[],[],[],[],[],@confun,options);

`fmincon`

produces the solution `x`

with
function value `fval`

:

x,fval x = -9.5474 1.0474 fval = 0.0236

You can evaluate the constraints at the solution by entering

[c,ceq] = confun(x)

This returns numbers close to zero, such as

c = 1.0e-14 * 0.5107 -0.5329 ceq = []

Note that both constraint values are, to within a small tolerance,
less than or equal to 0; that is, `x`

satisfies *c*(*x*) ≤ 0.

Was this topic helpful?