clear all; close all; clc;

% See 'help fminunc'. Requires Matlab's Optimization Toolbox.
% Otherwise, you can also just use your own codes for unconstrained
% optimization which you write earlier in the course.
options = optimoptions('fminunc', 'SpecifyObjectiveGradient', true);

% Store all solutions x k as columns of X, for display
X = zeros(2, 10);
X(:, 1) = randn(2, 1); % random initialization at first
beta = 1; % pick some initial value for beta

for k = 2 : size(X, 2)
    X(:, k) = fminunc(@(x) F(x, beta), X(:, k - 1), options);
    beta = 2*beta;
end

disp(X)
function [val, grad] = F(x, beta)
    val = (x(1) + x(2)) + beta*(1/2)*(x(1)^2 + x(2)^2 - 2)^2;
    grad = [1, 1]' + beta*(x(1)^2 + x(2)^2 - 2)*2*x;
end