clear all; close all; clc;
%% Optimizing on a circle
f = @(x) x(1) + x(2);

%% Approach 1: fmincon
options = optimoptions('fmincon', 'Display', 'iter');
% fmincon requires an initial guess.
% It allows us to initialize anywhere we want in R^2.
x0 = randn(2, 1);
x1 = fmincon(f, x0, [], [], [], [], [], [], @constraints, options);
x1


%% Approach 2: Manopt
problem.M = spherefactory(2);
problem.cost = f;
problem = manoptAD(problem);
% Since we do not specify x0, Manopt picks a random point on the circle.
x2 = trustregions(problem);
x2


%% Matlab requires functions to be defined at the end
function [g, h] = constraints(x)
   g = []; % no inequality constraints
   h = x(1)^2 + x(2)^2 - 1; % this must be equal to zero
end
