# Two ways to fit a function on data points

I have the following data:

eR = {1.1*10^-4 , 3.3*10^-4 , 1.1*10^-3 , 2*10^-3 ,
3.3*10^-3 , 5.8*10^-3 , 1.1*10^-2 , 1.9*10^-2 , 3.3*10^-2 ,
5.8*10^-2 , 1.1*10^-1} ;
NoDF = {2 , 2 , 2 , 3 , 4 , 4 , 7 , 11 , 18 , 21 , 35};
DSF = Transpose@{eR , NoDF};


Now, what I would like to do is to linearly fit a curve on the above set of points. Thus, I use the following

fitF = FindFit[DSF, a*n^b, {a, b}, n];


So, when I plot the data and the curve together they look ok. First I plot the data point

plot1F = {DSF } //
ListLogLogPlot[#, Joined -> False, FrameTicks -> All ,
Frame -> True] &


Secondly, I plot the fitting curve

a = Last[First[fitF]];
b = Last[Last[fitF]];
func[x_] := a x^b;
Data = Table[func[x] , {x , 1.1*10^-4 , 1.1*10^-1 , 0.00001}];
XF = Table[x , {x , 1.1*10^-4 , 1.1*10^-1 , 0.00001}];
DATAfit  = Transpose@{XF , Data};
plot2F = {DATAfit} //
ListLogLogPlot[# , PlotRange -> All , FrameTicks -> All,
Frame -> True] &


Then, I put them together

Show[plot1F , plot2F , PlotRange -> All]


The result looks ok for me. But then I was asked to change the function and first, take the logarithm of the points and then fit them with a + b*x function. So first I took the logarithm of the data point

DSFL = Transpose@{Log[eR] , Log[NoDF]};


and then fit the data

fitF2 = FindFit[DSFL, c + d n, {c, d}, n];
c = Last[First[fitF2]];
d = Last[Last[fitF2]];
funcL[x_] := c + d x;


But when I plot the data, it seems that there is something wrong with my plots. Because the curve does not fit the data

DataL = Table[funcL[x]  , {x , 1.1*10^-4 , 1.1*10^-1 , 0.00001}];
XF = Table[x , {x , 1.1*10^-4 , 1.1*10^-1 , 0.00001}];
DATAL = Transpose@{XF , DataL};
plot3F = {DATAL} //
ListPlot[# , PlotRange -> All , FrameTicks -> All,  Frame -> True] &
Show[plot1F , plot3F , PlotRange -> All]


Now, I want to know if there is something wrong with my function or code. Because I know that the method is correct and I should be able to fit the data with a c + d*x function.

• You don't want to take logs of the x axis values, just the y axis values. – Daniel Lichtblau Aug 18 '17 at 18:00
• @DanielLichtblau Thanks for your response. I will give it a shot. – KratosMath Aug 18 '17 at 18:33
• @DanielLichtblau I don't think that would solve the problem. Thanks by the way – KratosMath Aug 18 '17 at 18:41

Here is a somewhat simplified approach to your problem. Let's start with fitting the data directly to the non-linear model (in general a preferable approach, since no distortion is introduced in error statistics):

NonlinearModelFit[DSF, a n^b, {a, b}, n]
Plot[
%[n], Evaluate@Flatten@{n, MinMax@eR},
Epilog -> {Red, PointSize[0.015], Point@DSF}
]


Let's transform the data and try the linear fit then:

logData = Log10@DSF;

LinearModelFit[logData, x, x]
Plot[%[x],
Evaluate@Flatten@{x, MinMax@Log10@eR},
Epilog -> {Red, PointSize[0.015], Point@logData}
]


This is also not bad, but definitely the first two points don't seem to fit in the linear model.

We can try to redo the fit by excluding those points:

LinearModelFit[logData[[3 ;;]], x, x]
Plot[%[x],
Evaluate@Flatten@{x, MinMax@Log10@eR},
Epilog -> {Red, PointSize[0.015], Point@logData}
]


This is a lot better; notice also that the slope parameter here is numerically very close to the fitted exponent from the non-linear model fit, as it should be.

• Thanks for your complete answer. Is it possible to express the second fitting function 1.81333 + 0.451063x in form of a n^b? – KratosMath Aug 19 '17 at 6:24
• Exp[1.81333]*n^0.45106 yields 6.1308 n^0.4510 to convert the fit in logarithmic space back to the original form. In general the log of a * n^b results in Log[a] + b*Log[n]. – Jack LaVigne Aug 19 '17 at 15:26
• If I understand correctly, 1.81333+ 0.451063 x can be expressed as C*x^0.451063, where C is a constant. Am I right? – KratosMath Aug 19 '17 at 17:32
• @MarcoB I mis-clicked a downvote on your answer and just noticed. Now it is stuck. Do you know any way to get it changed to an up vote? Apologies for the mis-click – Jack LaVigne Sep 11 '17 at 21:56
• I don't know if it should be considered a mis-click, @JackLaVigne. Removing inconvenient data points for a better fit generally warrants a downvote in my book. However, I think it has been phrased that the model is inadequate to describe the data and removing those offending data points attempts to make that point. Is that correct? – JimB Sep 12 '17 at 5:06