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 vector c
. Because the fmincon
solver expects the constraints to be written in the form , write your constraint function to return the following value:
.
Create Objective Function
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;
Create Nonlinear Constraint Function
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.
Solve Problem
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
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×1
10-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
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