Main Content

packfcn

R2026b

Combine objective and nonlinear constraint functions

Description

objconstr = packfcn(obj,nlconst) combines the objective function obj and nonlinear constraint function nlconst into a function objconstr. The function objconstr(x) returns a structure suitable for a combined surrogateopt objective and constraint function. For information on converting between the surrogateopt structure syntax and other solvers, see Convert Nonlinear Constraints Between surrogateopt Form and Other Solver Forms.

example

Examples

collapse all

Combine the objective and constraint from the example Constrained Nonlinear Problem Using Optimize Live Editor Task or Solver into a form suitable for surrogateopt.

Create the objective function as an anonymous function ros(x).

ros = @(x)100*(x(2) - x(1)^2)^2 + (1 - x(1))^2;

Create the nonlinear constraint helper function unitdisk, which appears at the end of this example. Save the helper function with the name unitdisk.m in the current folder.

Combine the objective and nonlinear constraint functions into one function suitable for surrogateopt.

objconstr = packfcn(ros,@unitdisk);

Specify bounds and solve the problem using surrogateopt.

lb = [-2 -2];
ub = -lb;
[x,fval] = surrogateopt(objconstr,lb,ub)

Best function value near 0.05, with one infeasible point at 3600 at the start

surrogateopt stopped because it exceeded the function evaluation limit set by 
'options.MaxFunctionEvaluations'.
x = 1×2

    0.7865    0.6183

fval = 
0.0456

This code creates the unitdisk helper function.

function [ineqnonlin,eqnonlin] = unitdisk(x)
ineqnonlin = x(1)^2 + x(2)^2 - 1;
eqnonlin = [ ];
end

Input Arguments

collapse all

Objective function, specified as a function handle or function name.

The resulting function objconstr contains the field Fval.

objconstr.Fval = obj

Data Types: char | string | function_handle

Nonlinear constraint function, specified as a function handle or function name. Generally, the nonlinear constraint function returns two outputs.

[ineqnonlin,eqnonlin] = nlconst(x)

The output ineqnonlin is a vector or array whose entries represent the inequality constraints ineqnonlin(x) ≤ 0. The output eqnonlin is a vector or array whose entries represent the inequality constraints ineqnonlin(x) = 0. packfcn discards the eqnonlin output.

The resulting function objconstr contains the field Ineq.

objconstr.Ineq = ineqnonlin

Data Types: char | string | function_handle

Output Arguments

collapse all

Combined objective and constraint function, returned as a function handle. The function objconstr(x) returns a structure with the fields Fval and Ineq.

  • objconstr.Fval(x) is the objective function obj(x).

  • objconstr.Ineq(x) is the nonlinear inequality constraint function ineqnonlin(x), the first output of nlconst(x).

Version History

Introduced in R2020a