1
$\begingroup$

I am having some troubles with the NonlinearModelFit function. However hard I try, I cannot get a nice fit curve for the data points. I know the parameter "a" is about 15*10^-6 but even if I plug in the guess parameter I still do not get the fitting. Do you have any suggestions?

Thanks a lot!

f[z_] := a*Sqrt[1 + (z)^2/(a^2*Pi/632.8/10^-9)^2];
data = {{0, 8*10^-6}, {2*10^-2, 23*10^-6}, {3*10^-2, 
53*10^-6}, {-3.4*10^-2, 73*10^-6}, {-2*10^-2, 45*10^-6}};
nlm = NonlinearModelFit[data, f[z], {a}, z];
Show[ListPlot[data], Plot[{nlm[z]}, {z, -4*10^-2, 4*10^-2}],Frame -> True, Axes -> False, PlotRange -> Automatic]

enter image description here

$\endgroup$
  • $\begingroup$ "even if I plug in the guess parameter" - how did you try to use your guess within NonlinearModelFit[]? I do not see it in the code you provided. $\endgroup$ – J. M. will be back soon May 12 '16 at 7:09
  • $\begingroup$ Oh I didn't put it in the code I provided. nlm = NonlinearModelFit[data, f[z], {{a, 10*10^-6}}, z]. But the guess parameter doesn't really do anything. The curve still looks the same. $\endgroup$ – Phil Nguyen May 12 '16 at 7:19
  • $\begingroup$ Welcome! I suggest the following: 1) As you receive help, try to give it too, by answering questions in your area of expertise. 2) Take the tour and check the faqs! 3) When you see good questions and answers, vote them up by clicking the gray triangles, because the credibility of the system is based on the reputation gained by users sharing their knowledge. Remember to accept the answer, if any, that solves your problem, by clicking the checkmark sign! $\endgroup$ – user9660 May 12 '16 at 7:20
  • $\begingroup$ If I plot f[z] with your suggested initial condition of a=15*10^(-6) it does not resemble at all the few data points you provide. Do you maybe have a typo in your definition of f? Also: Slightly more data points might improve the fit (this is a general remark) $\endgroup$ – Lukas May 12 '16 at 8:14
  • $\begingroup$ Thank you @Lukas! I am pretty sure the definition of f I provided is correct. I unfortunately cannot get more data points at this point. $\endgroup$ – Phil Nguyen May 12 '16 at 9:02
1
$\begingroup$

I guess that the model is not good enough for the data. May be a good idea would be to try a different model, of course if the theory behind does not strictly prescribe this present form. Something along this line, for example:

    model1 = a*Sqrt[z^2 + c] + b;
model2 = a*Abs[z + c] + b;
ff1 = FindFit[data, model1, {a, {b, 0}, {c, 0.0001}}, z]
ff2 = FindFit[data, model2, {a, {b, 0}, {c, 0.001}}, z]
Show[{
  ListPlot[data],
  Plot[model1 /. ff1, {z, -0.04, 0.04}, PlotStyle -> Red],
  Plot[model2 /. ff2, {z, -0.04, 0.04}, PlotStyle -> Green]
  }]

yielding

enter image description here

where the red and green lines show the fitting with two close, but slightly different models.

Have fun!

$\endgroup$
  • $\begingroup$ Thanks a lot Alexei! The green curve you worked out actually looks like what my teacher is expecting me to get. However, the model I provide is concrete so I don't think it's possible to try out a different model. $\endgroup$ – Phil Nguyen May 12 '16 at 8:54
1
$\begingroup$

When in doubt, check the data and the model twice, three times, many times.

Check beam radius and remember: only two parameters need to be specified to give the whole beam profile: the wavelength Lambda and the beam waist w.

$w \sqrt{\frac{\lambda ^2 z^2}{\pi ^2 w^4}+1}$

nlm = NonlinearModelFit[data, w*Sqrt[1 + ((\[Lambda]*z)/(\[Pi]*w^2))^2], {w, \[Lambda]}, z]

FittedModel[6.88071*10^-6 Sqrt[1+75065.2 z^2]]

nlm["BestFitParameters"]
{w->6.88071*10^-6,\[Lambda]->-4.07508*10^-8}
lp1 = ListPlot[data, PlotStyle -> Red]

enter image description here

p1 = Plot[{nlm[z]}, {z, -0.04, 0.04}]

enter image description here

Show[lp1, p1]

enter image description here

$\endgroup$
  • 1
    $\begingroup$ Thanks for your help Louis. This is actually what I did earlier: providing the fit function with 2 parameters, although slightly different (I set the Raleigh length zr = second parameter because \lambda is already known to be 632.8nm for our lab). I got the same curve you did but my teacher's remark was that zr and w are of course not independent parameters and that I should have been able to get a better fit that would look, quite curiously, exactly like the green curve Alexei just worked out below. $\endgroup$ – Phil Nguyen May 12 '16 at 8:52
  • $\begingroup$ @PhilNguyen, well never ever mess up with your teacher ... $\endgroup$ – user9660 May 12 '16 at 8:57
  • $\begingroup$ Sorry I don't get what you meant. $\endgroup$ – Phil Nguyen May 12 '16 at 9:10

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.