  function xRoots = FindZeros(alpha)
% alpha is a positive real number.
% xRoots is a column that houses all the roots (in left-to-right order) 
% of the function
%
%                              f(x) sin(alpha*x) - x 
%
% that occur in the interval [-1,1]. 
  
% Bracketing interval set-up. The zeros of
%                f'(x) = alpha*cos(alpha*x) - 1
% can be used to create bracketing intervals for the zeros of f.

% We always have f(0) = 0 so
xRoots = 0;
% Are there other roots? Note that  f(x) = -f(-x) so that we just
% have to look for zeros in (0,1].
if alpha <= 1
    % No more roots because f'(x) = alpha(cos(alpha*x)-1/alpha) < 0
    % for all x.
    return
else
    % Find the smallest positive root of f'(x).
    L = fzero(@(x) fp(x,alpha),[0 pi/alpha]);
    % And here is the next root of f'...
    R = -L+2*pi/alpha;
    while f(L,alpha)*f(R,alpha)<=0
        % It is a bracketing root so apply fzero...
        x = fzero(@(x) f(x,alpha),[L,R]);
        % Update the list of roots...
        xRoots = [-x ; xRoots ; x];
        % Get the next potential bracketing interval using
        % periodicity of f', i.e.,
        %   f'(x+2*pi/alpha) = alpha*cos(alpha*x + 2*pi)-1
        %                    = alpha*cos(alpha*x)-1 = f'(x)
        newR = L + 2*pi/alpha;
        L = R;
        R = newR;
    end  
end

function y = f(x,alpha)
y = sin(alpha*x) - x;

function yp = fp(x,alpha)
yp = alpha*cos(alpha*x) - 1;

% Grading:

% 2 points: correct usage of fzero
% 2 points: finding bracketing intervals
% 1 point: working, fully efficient solution

% -1 for having to check each bracketing interval for sign difference
% -0 for not handling alpha <= 1
