% if 2 parameters are specified, xmin = 0, and xmax is the maximum value
% that occurs in the data
% if 3 arguments are specified, the 3rd is assumed to be xmin
function [alpha] = fitlineonloglog(x,y,xmin,xmax)

% if no xmin is specified, set it to 0
if (nargin < 3)
    xmin = 0;
    xmax = max(x);
end

if (nargin < 4)
    xmax = max(x);
end

%eliminate zeros because taking the logarithm
y = y(x>0);
x = x(x>0);

x = x(y>0);
y = y(y>0);

%also ignore values outside min and max specified by call
xfit = x(x>=xmin);
yfit = y(x>=xmin);
xfit = xfit(xfit<=xmax);
yfit = yfit(xfit<=xmax);

[a,b] = polyfit(log(xfit),log(yfit),1);
fitline = exp(log(xfit)*a(1)+a(2));

b.normr
alpha = a(1);
p = loglog(x,y,'ko',xfit,fitline,'r-');
alphastr = sprintf('alpha = %.4f fit, R = ');
set(p,'LineWidth',2);