function [roots, counter] = Newton_brackets(f, d, imax, tol, x_guess1, x_guess2)

% Initialize counter and roots list
counter = 1;
roots = [];  % To store the roots found

% Set next guess to infinity to fool while condition
abs_diff_between_guess1 = inf;
abs_diff_between_guess2 = inf;

% Set the interval boundaries
lower_bound = 4;
upper_bound = 10;

% Iterate until both roots are found or the maximum iterations are reached
while (length(roots) < 2) && (counter <= imax)

    % --- Root 1 search ---
    if abs_diff_between_guess1 > tol && length(roots) < 2
        % Calculate values of the function at the guess
        f_at_guess1 = feval(f, x_guess1);
        df_dx_at_guess1 = feval(d, x_guess1);

        % Calculate next point using Newton's formula
        x_next1 = x_guess1 - f_at_guess1 / df_dx_at_guess1;

        % Output to screen for root 1
        fprintf('ITERATION %i (Root 1) \r', counter);
        fprintf('First Guess = %10.6f \r', x_guess1);
        fprintf('Next guess = %10.6f \r \r', x_next1);

        % To test tolerance for root 1
        abs_diff_between_guess1 = abs(x_guess1 - x_next1);

        % Check if the current guess is outside the bounds
        if x_next1 < lower_bound || x_next1 > upper_bound
            fprintf('Guess for root 1 is outside the interval [4, 10], stopping the process. \n');
            break;
        end
        
        % Update the guesses for root 1
        x_guess1 = x_next1;

        % Check if the guess is within tolerance to consider it a root
        if abs_diff_between_guess1 <= tol
            roots = [roots, x_guess1];  % Store the root
        end
    end

    % --- Root 2 search ---
    if abs_diff_between_guess2 > tol && length(roots) < 2
        % Calculate values of the function at the guess
        f_at_guess2 = feval(f, x_guess2);
        df_dx_at_guess2 = feval(d, x_guess2);

        % Calculate next point using Newton's formula
        x_next2 = x_guess2 - f_at_guess2 / df_dx_at_guess2;

        % Output to screen for root 2
        fprintf('ITERATION %i (Root 2) \r', counter);
        fprintf('First Guess = %10.6f \r', x_guess2);
        fprintf('Next guess = %10.6f \r \r', x_next2);

        % To test tolerance for root 2
        abs_diff_between_guess2 = abs(x_guess2 - x_next2);

        % Check if the current guess is outside the bounds
        if x_next2 < lower_bound || x_next2 > upper_bound
            fprintf('Guess for root 2 is outside the interval [4, 10], stopping the process. \n');
            break;
        end
        
        % Update the guesses for root 2
        x_guess2 = x_next2;

        % Check if the guess is within tolerance to consider it a root
        if abs_diff_between_guess2 <= tol
            roots = [roots, x_guess2];  % Store the root
        end
    end

    % Increment counter for iterations
    counter = counter + 1;
end

% Adjust the counter to reflect the number of successful iterations
counter = counter - 1;

end
