%
% Keeping lambda (accuracy) fixed, test the iteration needed for different
% epsilon values.
%


% Load the functions and interval
GivenEnv;

fig_dir = 'figures';
if ~exist(fig_dir, 'dir')
    mkdir(fig_dir);
end

% Setup
% ========================

% lambda = 0.01
% epsilon: e < lambda/2 = 0.005
% de: A small step away from zero and lambda/2
%     de = 0.0001
% N: 50 points (50 epsilon values)

N = 50;
lambda = 0.01;
de = 0.0001;
epsilon = linspace(de, (lambda/2)-de, N);
k = zeros(1,N); % preallocate k


%
% Call the min_bisection method for each epsilon value for each
% function and keep the number of iterations needed.
% Then plot and save the # of iterations k(epsilon) for each function.
%

figure('Name', 'iterations_over_epsilon_min_bisection', 'NumberTitle', 'off');
set(gcf, 'Position', [100, 100, 1280, 600]); % Set the figure size to HD

for i = 1:length(funs)
    for j = 1:N
        [a, b, k(j)] = min_bisection(funs(i), a_0, b_0, epsilon(j), lambda);
    end
    fprintf('%20s(%34s ):  [a, b]= [%f, %f], iterations(min, max)= (%d, %d)\n', ...
            "min_bisection", char(funs(i)), a(end), b(end), k(1), k(N) );
    subplot(1, length(funs), i)
    plot(epsilon, k, '-b', 'LineWidth', 1.0)
    title(titles(i), 'Interpreter', 'latex')
    xlabel('epsilon')
    ylabel('Iterations')
end

%
% Print and save the figures
%
%fig_epsc = fullfile(fig_dir, "iter_over_epsilon_min_bisection" + ".epsc");
fig_png  = fullfile(fig_dir, "iter_over_epsilon_min_bisection" + ".png");

%print(gcf, fig_epsc, '-depsc', '-r300');
print(gcf, fig_png, '-dpng', '-r300');

    