1
$\begingroup$

I have plotted some data with a log scale on both axes as shown in the image below. I want to be able to fit this data such that the fit appears as linear on this log-log plot but I haven't been able to get the fit to work.

enter image description here

I have tried both fitting the original data without the log scaling and then converting the fit into a log scale but this generated an incorrect fit. What is the best way of doing this?

$\endgroup$
  • 1
    $\begingroup$ Make a linear fit on Log[data]? $\endgroup$ – Henrik Schumacher Aug 31 '18 at 16:59
  • 2
    $\begingroup$ Would you be so kind as to share the actual Mathematica code you are having trouble with? Have you tried regressing Log[Intensity] on Log[Laser Power] and a constant? $\endgroup$ – yosimitsu kodanuri Aug 31 '18 at 17:05
4
$\begingroup$

Contrary to my former comment, if one has a reason to expect a power law directly between the observed quantities, fitting a nonlinear model is a better idea since otherwise the random distribution of the error gets distorted.

The following produces x-y pairs with a power law with a normally distributed error on the y-coordinates.

n = 1000;
x = Subdivide[2., 4., n - 1];
b = RandomReal[{1, 4}];
a = RandomReal[{1, 20}];
c = RandomReal[{0, 20}];
y = a x^b + c + RandomVariate[NormalDistribution[0, 15], n]

Desing a model and with the parameters to the data with FindFit.

model = α t^β + γ;
fit = t \[Function] Evaluate[ model /. FindFit[Transpose[{x, y}], model, {α, β, γ}, t]];

Plotting in log-log scales:

Show[
 ListLinePlot[Transpose[{x, y}], ScalingFunctions -> {"Log", "Log"}],
 Plot[fit[t], {t, Min[x], Max[x]}, PlotStyle -> Red, 
  ScalingFunctions -> {"Log", "Log"}]
 ]

enter image description here

$\endgroup$
  • 1
    $\begingroup$ +1 Thanks for pointing out that the error structure is an essential part of the model, too. $\endgroup$ – JimB Aug 31 '18 at 17:43
  • $\begingroup$ @JimB Always at you service. =) $\endgroup$ – Henrik Schumacher Aug 31 '18 at 18:00
0
$\begingroup$

If Log10[10^x] gives a line in a LogLog plot, then using it as Linear fit should work. Example:

datay = {500, 1000, 3000, 5000, 10000};
datax = {50, 100, 300, 500, 1000};
ListLogLogPlot[Transpose@{datax, datay}, Joined -> True]
lm = LinearModelFit[Log10[datay], Log10[10^x], x]
ListLogLogPlot[Transpose@{Table[lm[x], {x, datax}], datax}, 
 Joined -> True]
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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