%% Create Nonlinear Constraints from Function
% In problem-based optimization, constraints are two optimization expressions with a comparison operator (==, <=, or >=) between them. You can use fcn2optimexpr to create one or both optimization expressions. See Convert Nonlinear Function to Optimization Expression.
% Create the nonlinear constraint that gammafn2 is less than or equal to –1/2. This function of two variables is in the gammafn2.m file.

type gammafn2

% Create optimization variables, convert the function file to an optimization expression, and then express the constraint as confn.

x = optimvar('x','LowerBound',0);
y = optimvar('y','LowerBound',0);
expr1 = fcn2optimexpr(@gammafn2,x,y);
confn = expr1 <= -1/2;
show(confn)

% Create another constraint that gammafn2 is greater than or equal to x + y.

confn2 = expr1 >= x + y;

% Create an optimization problem and place the constraints in the problem.

prob = optimproblem;
prob.Constraints.confn = confn;
prob.Constraints.confn2 = confn2;
show(prob)

% Copyright 2018-2019 The MathWorks, Inc.