Least-Squares Fitting—Logarithmic

Author

Eric W. Weisstein
August 6, 2002

This notebook downloaded from http://mathworld.wolfram.com/notebooks/Statistics/LeastSquaresFittingLogarithmic.nb.

For more information, see Eric's MathWorld entry http://mathworld.wolfram.com/LeastSquaresFittingLogarithmic.html.

©2005 Wolfram Research, Inc. except for portions noted otherwise

Notes

This is some of Eric's very old code.  It should be rewritten and cleaned up.

Code

LogarithmicFit[x_List,y_List]:=Module[
		{n=Length[x],i,sylnx,slnx,sy,b},
		sylnx=Sum[y[[i]]Log[x[[i]]],{i,n}];
		slnx=Sum[Log[x[[i]]],{i,n}];
		slnx2=Sum[Log[x[[i]]]^2,{i,n}];
		sy=Apply[Plus,y];
		b=(n sylnx-sy slnx)/(n slnx2-slnx^2);
		{(sy-b slnx)/n,b}
]

x=N[Table[i,{i,50}]];
y=Table[4.5+6.4 Log[x[[i]]]+.1 Random[Real,{-7,7}],{i,50}];
{a,b}=LogarithmicFit[x, y]

yfit=Table[a+b Log[ i],{i,50}];
Show[{ListPlot[y,DisplayFunction->Identity],ListPlot[yfit,PlotJoined->True,DisplayFunction->Identity]},
  DisplayFunction->$DisplayFunction]