function out = find_xintercept(x,y)

%out = find_xintercept(x,y)
%
%Function that finds the x_intercept of x and y data 
%out is a vector of x values where y==0
%Written by Mitch Harley
%14/2/2009
%Last edited 7/2/2012

out = [];
%First find if y==0

I = find(y==0);
out = x(I);

%MDH 7/2/2012
if size(out,1)>size(out,2)
    out = out';
end


%Now find where y goes from positive to negative
I = find(y>0);
J = find(y(2:end)<0);
II = intersect(I,J);

for i = 1:length(II)
    if (II(i)+1)<=length(y)
        x_inter = interp1([y(II(i)) y(II(i)+1)],[x(II(i)) x(II(i)+1)],0);
        out = [out x_inter];
    end
end

%Now find where y goes from negative to positive
I = find(y<0);
J = find(y(2:end)>0);
II = intersect(I,J);

for i = 1:length(II)
    if (II(i)+1)<=length(y)
        x_inter = interp1([y(II(i)) y(II(i)+1)],[x(II(i)) x(II(i)+1)],0);
        out = [out x_inter];
    end
end

%Sort rows
out = [sortrows(out')]';

%Deal with case of no intercepts
if isempty(out)
    out = NaN;
end
