function [a, b, k, n] = min_bisection_der(fun_expression, alpha, beta, epsilon, lambda)
% Bisection using derivatives method for finding the local minimum of a
% function.
%
% fun_expr: (symbolic expression over x) The symbolic expression of the
%           objective function
% alpha:    (number) The starting point of the interval in which we seek
%           for minimum
% beta:     (number) The ending point of the interval in which we seek
%           for minimum
% epsilon:  (number) The epsilon value
%           **note:**
%             epsilon in not used in this method, but it is part of the
%             method calling interface.
% lambda:   (number) The lambda value (accuracy)
%
% return:
%  a:       (vector) Starting points of the interval for each iteration
%  b:       (vector) Ending points of the interval for each iteration
%  k:       (number) The number of iterations
%  n:       (number) The calls of objective function fun_expr
%

% Error checking
if alpha > beta || lambda <= 0
    error ('Input criteria not met')
end

% Init output vectors
a = alpha;
b = beta;
dfun = matlabFunction(diff(fun_expression));

k=1;
while b(k) - a(k) > lambda
    % bisect [a,b]
    x_mid = (a(k) + b(k)) / 2;
    
    % set new search interval
    k = k + 1;
    df = dfun(x_mid);
    if df < 0
        a(k) = x_mid;
        b(k) = b(k-1);
    elseif df > 0
        a(k) = a(k-1);
        b(k) = x_mid;
    else % df == 0
        a(k) = x_mid;
        b(k) = x_mid;
        break;
    end
end

% Update the objctive function calls. In this case the derivative of the
% function ;)
n = k;
