clear; clc; close all;

mu = 0;

fx = @(x) 0.25 * x^4 + 0.5 * mu * x^2;
gx = @(x) x^3 + mu * x;

L = 3 + mu;
x = 1;
alpha = 2 / (L + mu);

niter = 500;
alphas = zeros(niter, 1);
fvals = zeros(niter, 1);

for i = 1:niter

    g = gx(x);
    x = x - alpha * g;
    gnew = gx(x);
    alpha = alpha + 0.5/L^2 * g * gnew / ((g)^2 + 1e-20);
    alphas(i) = alpha;
    fvals(i) = fx(x);

end % End for

fstar = 0;

figure;
subplot(1, 2, 1);
k = 1:niter;
semilogy(k, fvals - fstar, 'LineWidth', 3);
hold on;
semilogy(k, fvals(1)./k, '--', 'LineWidth', 3);
hold off;
legend('\texttt{HDM}', '$1/k$', 'Interpreter', 'latex');
xlabel('Iteration');
ylabel('$f(x^k) - f(x^\star)$', 'Interpreter', 'latex');
set(gca, 'FontSize', 24);
% title('Function Value Gap');

subplot(1, 2, 2);
plot(1:niter, alphas, 'LineWidth', 2);
xlabel('Iteration');
ylabel('$\alpha_k$', 'Interpreter', 'latex');
set(gca, 'FontSize', 24);
% title('Step Size \alpha');
