clear all; close all; clc;
%% Optimizing on a disk
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: CVX
cvx_begin
    variable x(2);
    minimize x(1) + x(2)
    subject to
        norm(x, 2) <= 1;
cvx_end
x2 = x;
x2
    

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