Documentation

This is machine translation

Translated by
Mouseover text to see original. Click the button below to return to the English version of the page.

Note: This page has been translated by MathWorks. Click here to see
To view all translated materials including this page, select Country from the country navigator on the bottom of this page.

Nonlinear Equality and Inequality Constraints

You can include nonlinear constraints by writing a function that computes both equality and inequality constraint values. A nonlinear constraint function has the syntax

`[c,ceq] = nonlinconstr(x)`

The function `c(x)` represents the constraint ```c(x) <= 0```. The function `ceq(x)` represents the constraint `ceq(x) = 0`.

Note

You must have the nonlinear constraint function return both `c(x)` and `ceq(x)`, even if you have only one type of nonlinear constraint. If a constraint does not exist, have the function return `[]` for that constraint.

For example, if you have the nonlinear equality constraint ${x}_{1}^{2}+{x}_{2}=1$ and the nonlinear inequality constraint x1x2 ≥ –10, rewrite them as

`$\begin{array}{c}{x}_{1}^{2}+{x}_{2}-1=0,\\ -{x}_{1}{x}_{2}-10\le 0,\end{array}$`

and then solve the problem using the following steps.

For this example, solve the problem

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

subject to these nonlinear constraints.

Step 1: Write a file objfun.m.

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

Step 2: Write a file confuneq.m for the nonlinear constraints.

```function [c,ceq] = confuneq(x) % Nonlinear inequality constraints c = -x(1)*x(2) - 10; % Nonlinear equality constraints ceq = x(1)^2 + x(2) - 1;```

Step 3: Invoke constrained optimization routine.

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

After 21 function evaluations, the solution produced is

```x,fval x = -0.7529 0.4332 fval = 1.5093 [c,ceq] = confuneq(x) % Check the constraint values at x c = -9.6739 ceq = -2.2204e-16```

Note that `ceq` is equal to 0 within the default tolerance on the constraints of `1.0e-006` and that `c` is less than or equal to 0, as desired.

Watch now