# Fitting data with ${\rm sinc}^2(x)$

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:

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:

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

• Please provide your data too, thanks Commented Oct 27, 2023 at 12:07
• 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. Commented Oct 27, 2023 at 12:22
• 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.
– JimB
Commented Oct 27, 2023 at 14:21
• 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.
– JimB
Commented Oct 27, 2023 at 14:41
• @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? Commented Oct 27, 2023 at 14:58

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


Hope it helps!

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

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

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.

• 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? Commented Oct 28, 2023 at 7:27
• I finally discovered how to edit my question and I've uploaded a new pic. Commented Oct 28, 2023 at 7:34