5
$\begingroup$

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.

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

1 Answer 1

5
$\begingroup$

I think not all your data adheres to your model, or perhaps your model is too simple to fit here.

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}
]

non-linear fit result

non-linear fit plot

That's not bad.


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}
]

linear fit results

linear fit plot all data

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}
]

linear fit results without first two points

linear fit plot without the first two points

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.

$\endgroup$
8
  • $\begingroup$ Thanks for your complete answer. Is it possible to express the second fitting function 1.81333 + 0.451063x in form of a n^b? $\endgroup$
    – KratosMath
    Commented Aug 19, 2017 at 6:24
  • $\begingroup$ 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]. $\endgroup$ Commented Aug 19, 2017 at 15:26
  • $\begingroup$ 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? $\endgroup$
    – KratosMath
    Commented Aug 19, 2017 at 17:32
  • $\begingroup$ @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 $\endgroup$ Commented Sep 11, 2017 at 21:56
  • 2
    $\begingroup$ 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? $\endgroup$
    – JimB
    Commented Sep 12, 2017 at 5:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.