function [x0, nIter] = solution_hw1_p1(f, range)
%BISECT Summary of this function goes here
%   Detailed explanation goes here

    vals = f(range);

    if vals(1)*vals(2)>=0
        error('Initial range does not bracket a root.');
    end
    
    nIter = 0;
    while range(2)-range(1)>1e-6
        nIter = nIter + 1;
        midPoint = 0.5*(range(2)+range(1));
        fMidPoint = f(midPoint);
        
        if vals(1)*fMidPoint<0
            range(2) = midPoint;
        elseif vals(2)*fMidPoint<0
            range(1) = midPoint;
        else
            error('Range no longer brackets a root!');
        end
        
        if nIter>1000
            error('Convergence did not occur within 1000 iterations.');
        end
        
    end
    
    x0 = 0.5*(range(1) + range(2));
    

end

