2

I'm trying to fit:

corrN4096h3halvesTemp4dot16Trans[[1 ;; 5]] = {{103, 0.0410511}, {205,
    0.0308854}, {307, 0.0263356}, {409, 0.0236176}, {511, 
   0.0219038}}

to exponential Exp[-r/l]*r^(1 - n), but Mathematica returns FittedModel[1.e^(-1.r)] when I use

testFit = 
 NonlinearModelFit[corrN4096h3halvesTemp4dot16Trans[[1 ;; 5]], Exp[-r/l]*r^(1 - n), {n, l}, r]

.

I want something that gives values for n and l and the fitted model isn't correct at all.

--

I tried to fix this problem by replicating what I read from here: Having trouble fitting data using NonlinearModelFit

But doing so I get these results:

{xmin, xmax, ymin, ymax} = 
 Flatten[Through[{Min, Max}[#]] & /@ 
   Transpose@corrN4096h3halvesTemp4dot16Trans[[1 ;; 5]]]
eqn = y == x^-(n - 1)*Exp[-x/l]
eqn2 = y /. 
   Solve[eqn /. {y -> Rescale[y, {ymin, ymax}], 
      x -> Rescale[x, {xmin, xmax}]}, y] // First

Which produces

results1

And

nlm = NonlinearModelFit[corrN4096h3halvesTemp4dot16Trans[[1 ;; 5]], 
   eqn2, {n, l}, x, Method -> NMinimize];
Column[{nlm["BestFitParameters"], Normal[nlm], 
  nlm["CorrelationMatrix"] // MatrixForm}, Left, 2]

Gives the error

results2

And continuing on anyway with

Plot[nlm[x], {x, xmin, xmax}, 
 Epilog -> {Red, PointSize[0.02], 
   Point /@ corrN4096h3halvesTemp4dot16Trans[[1 ;; 5]]}]

Gives this:

results3

Which is also wrong.

Any help would be gladly welcomed.

user132318
  • 21
  • 1
  • You may want to change your model. Try adjusting l and n here: Manipulate[ Show[Plot[Exp[-r/l]*r^(1 - n), {r, 0, 500}], ListPlot[corrN4096h3halvesTemp4dot16Trans]], {{l, 500}, 0.1, 50000}, {{n, 1.6}, 0.1, 5}]. You will notice that even if l->Infinity, the slope of your fitting function is still too steep, so the fitting function will never approximate your data well. – mszynisz Feb 01 '17 at 09:21
  • Also, putting some initial values may force Mathematica to try and fit something: NonlinearModelFit[corrN4096h3halvesTemp4dot16Trans, Exp[-r/l]*r^(1 - n), {{n, 1.5}, {l, 500}}, r] – mszynisz Feb 01 '17 at 09:25
  • The l term in Exp[-r/l] is so large that the value is effectively 1. So drop that term and add a pre-multipler to the model, a * r^(1-n). I also recommend following ubpdqn answer and using the logarithm which effectively produces a linear fit. – Jack LaVigne Feb 19 '17 at 03:01

2 Answers2

3

Changing the model may actually give you a better fit:

corrN4096h3halvesTemp4dot16Trans = {{103, 0.0410511}, {205, 0.0308854}, 
  {307, 0.0263356}, {409, 0.0236176}, {511, 0.0219038}}

nmf = NonlinearModelFit[corrN4096h3halvesTemp4dot16Trans, 
  a*Exp[-r/l]*r^(1 - n), {{n, 1.6}, {l, 50}, a}, r, 
  MaxIterations -> 1000];
Show[Plot[nmf[r], {r, 0, 550}, PlotRange -> {Automatic, {0, 0.07}}], 
  ListPlot[corrN4096h3halvesTemp4dot16Trans]]

Model fit

Here I just multiplied your model by an optimisable constant. Notice that giving initial values may make the fitting more stable. Sometimes I also change the Method, if the fitting is not working well.

mszynisz
  • 824
  • 5
  • 9
1

If you log transform you can make linear and get reasonable fit.

data = {{103, 0.0410511}, {205, 0.0308854}, {307, 0.0263356}, {409, 
    0.0236176}, {511, 0.0219038}};
d = {#1, Log@#1, Log@#2} & @@@ data;
lm = LinearModelFit[d, {1, x, y}, {x, y}]
bf = lm["BestFitParameters"]
{l, n} = {1/#2, 1 - #3} & @@ bf
f[x_] := Exp[bf.{1, x, Log[x]}]
Plot[f[x], {x, 100, 600}, 
 Epilog -> {Red, PointSize[0.02], Point[data]}, 
 PlotLabel -> 
  Rasterize@
   TraditionalForm[
    Style[Exp[bf[[1]]] Exp[Defer[-x/4847.002851124474`]] x^
       Defer[(1 - 1.4455705608305924`)], 20]], Frame -> True]

enter image description here

ubpdqn
  • 60,617
  • 3
  • 59
  • 148