0
$\begingroup$

I performed an experiment of Fraunhofer diffraction with slits, the results of which should be proportional to $\operatorname{sinc}^2(x) = \left(\frac{\sin x}{x}\right)^2$. However, an error occurred when trying to fit the data to the formula.

This is what my data looks like:

Experimental data

This is my attempt:

f3 = NonlinearModelFit[s3, a(Sinc[b (x - ξ)])^2, {{a, 4000}, 
      {b, 1}, {c, 0}, {ξ, 10}}, x]

but I get these:

Power::infy: Infinite expression 1/0. encountered.
Power::infy: Infinite expression 1/0.^2 encountered.
Infinity::indet: Indeterminate expression 0. ComplexInfinity encountered.
NonlinearModelFit::nrjnum: The Jacobian is not a matrix of real numbers at {a,b,c,ξ} = {4000.,1.,0.,10.}.

I added these initial values {4000, 1, 0, 10} for the parameters after reading this post. I think they should be sufficient for starting points because the model function with these parameters is actually really close:

fit model

I can't figure out where something like 1/0. could occur. Can someone tell me what to do to make this work?

$\endgroup$
10
  • 1
    $\begingroup$ Please provide your data too, thanks $\endgroup$ Commented Oct 27, 2023 at 12:07
  • $\begingroup$ Your fit function does not depend on $c$ and so the optimizer cannot optimize it with respect to $c$. Remove $c$ from the parameter list and try again. $\endgroup$
    – Roman
    Commented Oct 27, 2023 at 12:22
  • 1
    $\begingroup$ If your response is a "count", then Poisson regression would likely be better than using NonlinearModelFit in that the constant variance assumption of NonlinearModelFit is not appropriate for such data. $\endgroup$
    – JimB
    Commented Oct 27, 2023 at 14:21
  • $\begingroup$ Your function has $x-\xi$ in the denominator. If one of your predictor values ($x$) happens to be the same as the starting value of $\xi$, then you'll get the error you see. The same thing will happen if one of the $x$ values equals the current estimated value of $\xi$. There are several ways to avoid this error. $\endgroup$
    – JimB
    Commented Oct 27, 2023 at 14:41
  • $\begingroup$ @UlrichNeumann The data contains 4000 points, so it's unrealistic to list it all here. Is it possible to upload a txt file? Or maybe I should pick 40 of them? $\endgroup$
    – L0wc3ll
    Commented Oct 27, 2023 at 14:58

2 Answers 2

6
$\begingroup$

Try NonlinearModelFit[...,Method->"NMinimize"]

Examplary data (please provide your data)

data = Table[{x, Exp[1] (Sinc[Pi (x - 10)])^2}, {x, 0, 20, 1/5}];
f3 = NonlinearModelFit[data, a (Sinc[b (x - \[Xi])])^2, {a, b, \[Xi]},x, Method -> "NMinimize"]  
Show[{Plot[f3[x], {x, 0, 20}, PlotRange -> All],ListPlot[data]}]

enter image description here

Hope it helps!

$\endgroup$
3
  • $\begingroup$ It worked! Thank you so much! $\endgroup$
    – L0wc3ll
    Commented Oct 27, 2023 at 15:15
  • $\begingroup$ Could you explain why adding "NMinimize" would make this work? Is it because something goes wrong with $\operatorname{sinc}(x)$ when processed symbolically? $\endgroup$
    – L0wc3ll
    Commented Oct 27, 2023 at 15:15
  • $\begingroup$ @L0wc3ll No I have no explaination. I only experienced NMInimize to provide robust minimization. $\endgroup$ Commented Oct 27, 2023 at 15:35
0
$\begingroup$

This is just an extended comment (probably more for my benefit than yours). Does your data (4,000 points) look more like the following:

Data with Poisson variation

or is it extremely smooth as in the figure you displayed?

I'm trying to understand what should the error structure look like so that it is appropriately incorporated into the analysis.

$\endgroup$
2
  • $\begingroup$ It was really smooth, with magnitude of the error at each point below 5. Is there any way I can upload a new picture for you? $\endgroup$
    – L0wc3ll
    Commented Oct 28, 2023 at 7:27
  • $\begingroup$ I finally discovered how to edit my question and I've uploaded a new pic. $\endgroup$
    – L0wc3ll
    Commented Oct 28, 2023 at 7:34

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.