%% Solve Nonlinear Programming Problem Using Problem-Based Approach
% Find a minimum of the peaks function, which is included in MATLAB®, in the region x^2 + y^2 \le 4. To do so, create optimization variables x and y.

x = optimvar('x');
y = optimvar('y');

% Create an optimization problem having peaks as the objective function.

prob = optimproblem("Objective",peaks(x,y));

% Include the constraint as an inequality in the optimization variables.

prob.Constraints = x^2 + y^2 <= 4;

% Set the initial point for x to 1 and y to –1, and solve the problem.

x0.x = 1;
x0.y = -1;
sol = solve(prob,x0)

%
%% Unsupported Functions Require fcn2optimexpr
% If your objective or nonlinear constraint functions are not entirely composed of elementary functions, you must convert the functions to optimization expressions using fcn2optimexpr. See Convert Nonlinear Function to Optimization Expression and Supported Operations on Optimization Variables and Expressions.
% To convert the present example:

convpeaks = fcn2optimexpr(@peaks,x,y);
prob.Objective = convpeaks;
sol2 = solve(prob,x0)

% Copyright 2018–2020 The MathWorks, Inc.