Nonlinear Inequality Constraints
This example shows how to solve a scalar minimization problem with nonlinear inequality constraints. The problem is to find that solves
subject to the constraints
Because neither of the constraints is linear, create a function,confun.m
, that returns the value of both constraints in a vectorc
. Because thefmincon
solver expects the constraints to be written in the form
, write your constraint function to return the following value:
.
Create Objective Function
The helper functionobjfun
is the objective function; it appears at theend of this example. Set thefun
argument as a function handle to theobjfun
function.
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 functionconfun
at 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
这段代码创建了objfun
helper 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
这段代码创建了confun
helper 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