% PowerCurveTestLS.m (L. Braile, Sept. 2002, revised Feb. 2006)
% non-linear least squares by transformation to a
% linear equation - Power curve test y = ax^b
x = [1 1.5 2 2.5 3 3.5 4 4.5 5 5.5]; % x data
y = [.2 .45 .8 1.25 1.8 2.45 3.2 4.05 5 6.05]; % y data 
% Take logs of x and y
logx = log10(x);
logy = log10(y);
npts = max(size(x));
sigmay = ones(npts,1);
mode = 0;
[a,sigmaa,b,sigmab,r]=linfit(logx,logy,sigmay,npts,mode)
a = 10^a % correct a to original form
% Calculate values of y for the fitted line
xt = 0.2:0.2:5.8;
yt = a*(xt.^b);
% Plot data and fitted line on linear axes graph
plot(x,y,'or','markersize',10,'linewidth',2)
hold on
plot(xt,yt,'-b','linewidth',2)
xlabel('x data -- Power Curve -- linear plot','fontsize',16)
ylabel('y data','fontsize',16)
title('Least Squares Fit -- Power Curve (y = ax^b)','fontsize',16)
hold off
figure
% Plot data and fitted line on logarithmic axes graph
loglog(x,y,'or','markersize',10,'linewidth',2)
hold on
loglog(xt,yt,'-b','linewidth',2)
xlabel('x data -- Power Curve -- log-log plot','fontsize',16)
ylabel('y data','fontsize',16)
title('Least Squares Fit -- Power Curve (y = ax^b)','fontsize',16)
axis([1 10 0.1 10])
hold off