Main Content

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

min x f ( x ) = e x 1 ( 4 x 1 2 + 2 x 2 2 + 4 x 1 x 2 + 2 x 2 + 1 ) ,

subject to the constraints

x 1 x 2 - x 1 - x 2 - 1 . 5 x 1 x 2 - 1 0 .

Because neither of the constraints is linear, create a function,confun.m, that returns the value of both constraints in a vectorc. Because thefminconsolver expects the constraints to be written in the form c ( x ) 0 , write your constraint function to return the following value:

c ( x ) = [ x 1 x 2 - x 1 - x 2 + 1 . 5 - 10 - x 1 x 2 ] .

Create Objective Function

The helper functionobjfunis the objective function; it appears at theend of this example. Set thefunargument as a function handle to theobjfunfunction.

fun = @objfun;

Create Nonlinear Constraint Function

Nonlinear constraint functions must return two arguments:c, the inequality constraint, andceq, the equality constraint. Because this problem has no equality constraint, the helper functionconfunat theend of this examplereturns[]as the equality constraint.

Solve Problem

Set the initial point to[1].

x0 = [1];

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

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

Solve the problem usingfmincon.

[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

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

Helper Functions

这段代码创建了objfunhelper function.

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

这段代码创建了confunhelper function.

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

Related Topics