Message Boards Message Boards

[?] Perform a nonlinearmodelfit of data?

Hi to everybody i need help in this data fit please: i have to do a nonlinearmodelfit of data but there is a problem cause the function has problem in one point: zero. The function is this f(x)= a*(Sin[x]/x)^, if i want to plot it there is no problem(cause sin^2 and x^2 has same behaviou near 0) but in the fit problems come out. Thanks Here the code:

`nlmf = NonlinearModelFit[Data, (a*((Sin[x])/x)^2), {a}, x, 
  PrecisionGoal -> 500, AccuracyGoal -> 500, MaxIterations -> 100]
During evaluation of In[79]:= Power::infy: Infinite expression 1/0^2 encountered.

During evaluation of In[79]:= Infinity::indet: Indeterminate expression 0 ComplexInfinity encountered.

During evaluation of In[79]:= FindFit::fitm: Unable to solve for the fit parameters; the design matrix is nonrectangular, non-numerical, or could not be inverted.

During evaluation of In[79]:= Power::infy: Infinite expression 1/0^2 encountered.

During evaluation of In[79]:= Infinity::indet: Indeterminate expression 0 ComplexInfinity encountered.

During evaluation of In[79]:= FindFit::fitm: Unable to solve for the fit parameters; the design matrix is nonrectangular, non-numerical, or could not be inverted.

Out[79]= NonlinearModelFit[{{2200, 9486}, {2189, 10552}, {2178, 
   10660}, {2167, 10691}, {2156, 10700}, {2145, 10680}, {2134, 
   10639}, {2123, 10577}, {2112, 10504}, {2101, 10436}, {2090, 
   10333}, {2079, 10166}, {2068, 10100}, {2057, 9976}, {2046, 
   9819}, {2035, 9654}, {2024, 9478}, {2013, 9317}, {2002, 
   9162}, {1991, 9048}, {1980, 8984}, {1969, 8965}, {1958, 
   8984}, {1947, 9030}, {1936, 9121}, {1925, 9258}, {1914, 
   9414}, {1903, 9623}, {1892, 9873}, {1881, 10155}, {1870, 
   10494}, {1859, 10889}, {1848, 11176}, {1837, 11495}, {1826, 
   11864}, {1815, 12180}, {1804, 12485}, {1793, 12759}, {1782, 
   12998}, {1771, 13217}, {1760, 13334}, {1749, 13425}, {1738, 
   13478}, {1727, 13501}, {1716, 13528}, {1705, 13531}, {1694, 
   13501}, {1683, 13418}, {1672, 13279}, {1661, 13104}, {1650, 
   12889}, {1639, 12591}, {1628, 12249}, {1617, 11839}, {1606, 
   11408}, {1595, 10981}, {1584, 10585}, {1573, 10261}, {1562, 
   10049}, {1551, 9982}, {1540, 10002}, {1529, 10113}, {1518, 
   10289}, {1507, 10569}, {1496, 10946}, {1485, 11431}, {1474, 
   12026}, {1463, 12787}, {1452, 13597}, {1441, 14266}, {1430, 
   15098}, {1419, 16008}, {1408, 16824}, {1397, 17582}, {1386, 
   18376}, {1375, 19053}, {1364, 19589}, {1353, 20042}, {1342, 
   20441}, {1331, 20703}, {1320, 20914}, {1309, 21151}, {1298, 
   21357}, {1287, 21454}, {1276, 21538}, {1265, 21464}, {1254, 
   21267}, {1243, 20892}, {1232, 20374}, {1221, 19643}, {1210, 
   18766}, {1199, 17729}, {1188, 16679}, {1177, 15642}, {1166, 
   14492}, {1155, 13816}, {1144, 13367}, {1133, 13033}, {1122, 
   12877}, {1111, 12867}, {1100, 13090}, {1089, 13484}, {1078, 
   14092}, {1067, 15027}, {1056, 16444}, {1045, 17656}, {1034, 
   19058}, {1023, 20815}, {1012, 22564}, {1001, 24362}, {990, 
   26293}, {979, 28316}, {968, 29934}, {957, 31290}, {946, 
   32643}, {935, 33930}, {924, 35041}, {913, 36093}, {902, 
   37248}, {891, 38366}, {880, 39588}, {869, 40839}, {858, 
   41956}, {847, 42478}, {836, 42965}, {825, 43080}, {814, 
   42755}, {803, 41994}, {792, 41020}, {781, 39657}, {770, 
   37566}, {759, 35810}, {748, 34369}, {737, 33016}, {726, 
   31783}, {715, 30675}, {704, 29725}, {693, 29015}, {682, 
   28536}, {671, 28396}, {660, 28749}, {649, 29370}, {638, 
   30305}, {627, 31795}, {616, 33682}, {605, 35917}, {594, 
   38372}, {583, 41375}, {572, 44263}, {561, 46902}, {550, 
   49528}, {539, 52227}, {528, 54665}, {517, 57096}, {506, 
   59745}, {495, 62909}, {484, 66633}, {473, 70708}, {462, 
   75122}, {451, 79348}, {440, 83554}, {429, 88045}, {418, 
   92402}, {407, 96770}, {396, 100595}, {385, 104304}, {374, 
   107582}, {363, 110605}, {352, 112369}, {341, 113676}, {330, 
   114831}, {319, 115789}, {308, 116596}, {297, 117505}, {286, 
   118329}, {275, 119644}, {264, 120884}, {253, 122499}, {242, 
   124128}, {231, 125596}, {220, 127536}, {209, 129644}, {198, 
   131939}, {187, 134775}, {176, 137662}, {165, 140433}, {154, 
   142510}, {143, 145542}, {132, 147803}, {121, 150105}, {110, 
   152663}, {99, 154762}, {88, 158816}, {77, 162147}, {66, 
   165049}, {55, 170577}, {44, 174954}, {33, 179777}, {22, 
   184220}, {11, 186457}, {0, 191722}, {-11, 196086}, {-22, 
   196191}, {-33, 195012}, {-44, 194256}, {-55, 193430}, {-66, 
   192380}, {-77, 191275}, {-88, 190630}, {-99, 189674}, {-110, 
   186922}, {-121, 181245}, {-132, 175605}, {-143, 171991}, {-154, 
   167245}, {-165, 162507}, {-176, 157289}, {-187, 152795}, {-198, 
   147118}, {-209, 142994}, {-220, 137857}, {-231, 134399}, {-242, 
   130981}, {-253, 128261}, {-264, 125292}, {-275, 122908}, {-286, 
   120692}, {-297, 118446}, {-308, 115961}, {-319, 113284}, {-330, 
   110564}, {-341, 107978}, {-352, 105759}, {-363, 103556}, {-374, 
   101445}, {-385, 99799}, {-396, 97537}, {-407, 95693}, {-418, 
   93850}, {-429, 91862}, {-440, 89593}, {-451, 87843}, {-462, 
   86063}, {-473, 84206}, {-484, 82186}, {-495, 79957}, {-506, 
   77514}, {-517, 74852}, {-528, 71791}, {-539, 68280}, {-550, 
   64719}, {-561, 61914}, {-572, 58394}, {-583, 54866}, {-594, 
   51409}, {-605, 48113}, {-616, 44851}, {-627, 42147}, {-638, 
   39837}, {-649, 37785}, {-660, 35808}, {-671, 34129}, {-682, 
   32631}, {-693, 31129}, {-704, 29530}, {-715, 27801}, {-726, 
   26095}, {-737, 24562}, {-748, 23387}, {-759, 22445}, {-770, 
   21767}, {-781, 21418}, {-792, 21439}, {-803, 21791}, {-814, 
   22468}, {-825, 23446}, {-836, 24921}, {-847, 25957}, {-858, 
   26846}, {-869, 27660}, {-880, 28401}, {-891, 29084}, {-902, 
   29631}, {-913, 30087}, {-924, 30375}, {-935, 30495}, {-946, 
   30403}, {-957, 30162}, {-968, 29731}, {-979, 29116}, {-990, 
   28376}, {-1001, 27536}, {-1012, 26596}, {-1023, 25638}, {-1034, 
   24787}, {-1045, 23958}, {-1056, 23160}, {-1067, 22403}, {-1078, 
   21705}, {-1089, 20984}, {-1100, 20138}, {-1111, 19225}, {-1122, 
   18186}, {-1133, 17143}, {-1144, 16123}, {-1155, 15205}, {-1166, 
   14348}, {-1177, 13607}, {-1188, 12979}, {-1199, 12531}, {-1210, 
   12259}, {-1221, 12179}, {-1232, 12314}, {-1243, 12651}, {-1254, 
   12924}, {-1265, 13253}, {-1276, 13581}, {-1287, 13916}, {-1298, 
   14260}, {-1309, 14579}, {-1320, 14905}, {-1331, 15195}, {-1342, 
   15427}, {-1353, 15591}, {-1364, 15662}, {-1375, 15689}, {-1386, 
   15615}, {-1397, 15506}, {-1408, 15336}, {-1419, 15105}, {-1430, 
   14882}, {-1441, 14638}, {-1452, 14388}, {-1463, 14125}, {-1474, 
   13892}, {-1485, 13644}, {-1496, 13360}, {-1507, 13029}, {-1518, 
   12596}, {-1529, 12152}, {-1540, 11647}, {-1551, 11196}, {-1562, 
   10720}, {-1573, 10274}, {-1584, 9868}, {-1595, 9497}, {-1606, 
   9224}, {-1617, 9039}, {-1628, 8961}, {-1639, 9046}, {-1650, 
   9184}, {-1661, 9363}, {-1672, 9574}, {-1683, 9814}, {-1694, 
   10097}, {-1705, 10385}, {-1716, 10685}, {-1727, 11009}, {-1738, 
   11333}, {-1749, 11657}, {-1760, 11873}, {-1771, 12090}, {-1782, 
   12257}, {-1793, 12384}, {-1804, 12461}, {-1815, 12494}, {-1826, 
   12490}, {-1837, 12460}, {-1848, 12414}, {-1859, 12372}, {-1870, 
   12304}, {-1881, 12232}, {-1892, 12133}, {-1903, 12013}, {-1914, 
   11850}, {-1925, 11655}, {-1936, 11400}, {-1947, 11143}, {-1958, 
   10858}, {-1969, 10552}, {-1980, 10239}, {-1991, 9953}, {-2002, 
   9664}, {-2013, 9424}, {-2024, 9189}, {-2035, 8987}, {-2046, 
   8865}, {-2057, 8835}, {-2068, 8825}, {-2079, 8829}, {-2090, 
   8866}, {-2101, 8908}, {-2112, 8953}, {-2123, 9041}, {-2134, 
   9119}, {-2145, 9231}, {-2156, 9314}, {-2167, 9396}, {-2178, 
   9484}, {-2189, 9584}, {-2200, 9652}, {-2211, 9699}, {-2222, 
   9736}, {-2233, 9762}, {-2244, 9801}, {-2255, 9812}, {-2266, 
   9821}, {-2277, 9833}, {-2288, 9832}, {-2299, 9843}, {-2310, 
   9820}, {-2321, 9801}, {-2332, 9779}, {-2343, 9716}, {-2354, 
   9657}, {-2365, 9557}, {-2376, 9476}}, (a Sin[x]^2)/x^2, {a}, x, 
 PrecisionGoal -> 500, AccuracyGoal -> 500, MaxIterations -> 100]`
POSTED BY: Riccardo Moro
9 Replies

Oh yes! Thank a lot !!

POSTED BY: Riccardo Moro

Well, forgive me for replying so late to your comment and for my bad english, yes you are rigth my analitycal model is correct but my data don't follow it (it is an experiment based on diffraction) cause of various problems even if i rescale the data, so in the end i have to analyse the data in other ways. Thank you all for the time you have invested and i have a final question: i can't find a function that find points of minimum ( of maximum is FindPeaks) from data does it exist or no? I can solve the problem with a Table but i was curious about the existance of this function. Thank again

POSTED BY: Riccardo Moro

Riccardo,

minima are the maxima of the negative function! Let data be your data from above, then you can do:

maxIndx = First /@ FindPeaks[Last /@ data];
minIndx = First /@ FindPeaks[Last /@ -data];
ListLinePlot[data, ImageSize -> Large, Epilog -> {PointSize[.01], Red, Point[data[[maxIndx]]], Green, Point[data[[minIndx]]]}]

which gives:

enter image description here

Regards -- Henrik

POSTED BY: Henrik Schachner

You need to understand the model for your data, you can not just put almost random arbitrary rough functions and expect it to work. It is like fitting circle with a square and complaining it does not work. Nice advice given to you here by other member assumes that you will put more work into the details of the fit. Did you read any literature on fitting, and do you understand the mechanism behind your data? Many functions can fit your data, but they can be really far from the real mechanism behind the data, so they loose meaning. Are you sure you need an analytic model at all, maybe Interpolation would do for your goals? I got this "example" fit, notebook is attached. It fits twice: first a simple model, then fits residues of a simple model. It works with rescaled data: over x and y, you can easily rescale back. Rescaled data are easier to understand.

enter image description here

Attachments:
POSTED BY: Vitaliy Kaurov

Thank you all i try now

POSTED BY: Riccardo Moro

The Problem remains:

In[18]:= nlmf = 
 NonlinearModelFit[
  Dati, (a*((Sinc[b*x])^2) + c), {{a, 200000}, {b, 0.005}, {c, 8000}},
   x, PrecisionGoal -> 500, AccuracyGoal -> 500, MaxIterations -> 200]

During evaluation of In[18]:= Power::infy: Infinite expression 1/0 encountered.

During evaluation of In[18]:= Power::infy: Infinite expression 1/0^2 encountered.

During evaluation of In[18]:= Infinity::indet: Indeterminate expression 0. ComplexInfinity encountered.

During evaluation of In[18]:= NonlinearModelFit::nrjnum: The Jacobian is not a matrix of real numbers at {a,b,c} = {200000.,0.005,8000.}.
POSTED BY: Riccardo Moro

I'm not sure why this gives those errors and fails. The following succeeds though the result is not great.

nlmf = NonlinearModelFit[
  data, (a*Sinc[.005*x]^2 + c), {{a, 200000}, {c, 8000}}, x]
POSTED BY: Daniel Lichtblau

The Sinc function seems to be made for your problem. It is equivalent to Sin[x]/x when x is not 0, and it is 1 for x=0.

POSTED BY: Gianluca Gorni

Riccardo, try Sinc[x] instead of Sin[x]/x.

Regards -- Henrik

POSTED BY: Henrik Schachner
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract