# How to find a fit for numerical data?

I would like to know how to fit a numerical data in two different ways. The first one is

datan = {{-1.99941, 3.36481}, {-1.99763, 4.08443}, {-1.99466,
3.50899}, {-1.99052, 2.98849}, {-1.98519, 2.34683}, {-1.97869,
3.3113}, {-1.97101, 3.35531}, {-1.96216, 4.3102}, {-1.95215,
2.97094}, {-1.94098, 2.6125}, {-1.92866, 2.44526}, {-1.9152,
3.39602}, {-1.9006, 3.35046}, {-1.88488, 3.16035}, {-1.86803,
2.71749}, {-1.85008, 2.034}, {-1.83103, 3.29505}, {-1.8109,
3.01073}, {-1.78969, 2.66289}, {-1.76742, 2.43722}, {-1.7441,
2.32192}, {-1.71975, 2.69802}, {-1.69437, 2.39546}, {-1.668,
3.09912}, {-1.64063, 1.90949}, {-1.61229, 2.17662}, {-1.58299,
2.47638}, {-1.55276, 2.25317}, {-1.5216, 2.40841}, {-1.48954,
1.9034}, {-1.4566, 2.51593}, {-1.4228, 2.0277}, {-1.38815,
2.28873}, {-1.35268, 1.94795}, {-1.3164, 2.20221}, {-1.27935,
2.33044}, {-1.24153, 1.97724}, {-1.20298, 1.88431}, {-1.16372,
1.61914}, {-1.12376, 1.9772}, {-1.08314, 2.13882}, {-1.04188,
2.25348}, {-1., 1.86668}, {-0.957526, 1.40721}, {-0.914485,
1.66627}, {-0.870901, 1.78616}, {-0.8268, 2.08214}, {-0.782209,
1.54093}, {-0.737155, 1.31384}, {-0.691663, 1.30438}, {-0.645761,
1.64397}, {-0.599476, 1.80947}, {-0.552835, 1.46119}, {-0.505867,
1.49977}, {-0.458598, 1.12732}, {-0.411058, 1.62071}, {-0.363274,
1.67635}, {-0.315274, 1.46581}, {-0.267087, 1.33225}, {-0.218742,
1.27569}, {-0.170268, 1.69785}, {-0.121692, 1.75691}, {-0.073044,
1.61574}, {-0.0243528, 1.12297}, {0.0243528, 0.967858}, {0.073044,
1.20717}, {0.121692, 1.37573}, {0.170268, 1.42958}, {0.218742,
0.869249}, {0.267087, 1.09798}, {0.315274, 1.12727}, {0.363274,
1.36133}, {0.411058, 1.2271}, {0.458598, 1.06877}, {0.505867,
0.867321}, {0.552835, 0.958877}, {0.599476, 1.497}, {0.645761,
1.29388}, {0.691663, 1.02559}, {0.737155, 0.83082}, {0.782209,
1.00069}, {0.8268, 1.46913}, {0.870901, 1.1681}, {0.914485,
0.964766}, {0.957526, 0.916658}, {1., 1.0225}, {1.04188,
1.03863}, {1.08314, 1.09699}, {1.12376, 1.02019}, {1.16372,
0.855622}, {1.20298, 0.949215}, {1.24153, 1.0585}, {1.27935,
1.17119}, {1.3164, 1.05726}, {1.35268, 0.780647}, {1.38815,
0.868919}, {1.4228, 0.824941}, {1.4566, 0.976392}, {1.48954,
0.964107}, {1.5216, 1.02742}, {1.55276, 0.904493}, {1.58299,
0.999191}, {1.61229, 1.04657}, {1.64063, 0.960569}, {1.668,
1.08807}, {1.69437, 0.729724}, {1.71975, 0.831124}, {1.7441,
0.679191}, {1.76742, 0.764854}, {1.78969, 0.92459}, {1.8109,
0.880412}, {1.83103, 0.89367}, {1.85008, 0.767452}, {1.86803,
0.879975}, {1.88488, 0.862821}, {1.9006, 0.99848}, {1.9152,
0.820868}, {1.92866, 0.675725}, {1.94098, 0.760808}, {1.95215,
0.91016}, {1.96216, 1.19938}, {1.97101, 0.755532}, {1.97869,
0.680112}, {1.98519, 0.632745}, {1.99052, 0.906384}, {1.99466,
1.22416}, {1.99763, 0.935568}, {1.99941, 0.598793}};

modeln = 1/(x/T - a);

fit = FindFit[datan, modeln, {a, T}, x]


Where I would like to fit a Rayleigh-Jeans distribution in the form

$$\vert c_{i} \vert^{2} = \frac{1}{\frac{x_{i}}{T} - a}$$

where T and a are parameters.

The other fit is to find a curve and plot it with the next points,

data = {0.0146098, 0.0134606, 0.0117886, 0.0118272, 0.0116045, 0.0136895, \
0.0142309, 0.0133225, 0.0120502, 0.0115602, 0.0122138, 0.0144221, \
0.0143005, 0.0115857, 0.0121894, 0.0110288, 0.0115023, 0.0107387, \
0.0112213, 0.0166045, 0.0111623, 0.0132333, 0.0109659, 0.0116389, \
0.0109815, 0.0115931, 0.0123628, 0.00990026, 0.0117823, 0.0107738, \
0.0115824, 0.00969016, 0.0090692, 0.0105925, 0.0134049, 0.00898036, \
0.00874895, 0.00848092, 0.00941636, 0.0104871, 0.0102619, 0.00806756, \
0.00939666, 0.00930185, 0.00827989, 0.00767965, 0.0068821, \
0.00745014, 0.00720067, 0.00776761, 0.00773065, 0.00680642, \
0.0072014, 0.00731968, 0.00667065, 0.00803056, 0.00635712, \
0.00660839, 0.00579348, 0.00664496, 0.00680048, 0.00614221, \
0.00627943, 0.00774538, 0.00731663, 0.00656819, 0.00731539, \
0.00640468, 0.00545746, 0.00704614, 0.00743988, 0.00767413, \
0.00774313, 0.00547266, 0.00650785, 0.0050816, 0.0065443, 0.0061788, \
0.00586096, 0.00631223, 0.00643369, 0.00552062, 0.00483852, \
0.00589909, 0.0060468, 0.00642089, 0.00538425, 0.00517127, \
0.00620243, 0.0055353, 0.0051133, 0.00543376, 0.00536765, 0.00633826, \
0.00503093, 0.00553102, 0.00487576, 0.00499182, 0.00499501, 0.005098, \
0.00578895, 0.00546778, 0.00540558, 0.00548438, 0.0040702, \
0.00431465, 0.00533605, 0.00428753, 0.00517276, 0.00469169, \
0.00447317, 0.00513875, 0.00496469, 0.00577266, 0.00497707, \
0.00404305, 0.00555311, 0.00443977, 0.00453418, 0.00494885, \
0.00386463, 0.00523461, 0.00472534, 0.00500563, 0.00521087, \
0.00486819, 0.00523756, 0.00456057}

• What have you tried? Commented Jul 8, 2021 at 2:36
• For the first dataset and model you just need better starting values. Try fit = FindFit[datan, modeln, {a, {T, 3}}, x] or fit = FindFit[datan, modeln, {a, T}, x, Method -> "NMinimize"]
– JimB
Commented Jul 8, 2021 at 3:56

You need better starting values. Fortunately with your model one can make reasonable guesses from the scatter plot.

ListPlot[datan, Frame -> True]


(* When x=0, then a0 = -1/y *)
a0 = -1/1.3
(* When x=2, then plug in a0 and solve for T *)
T0 = T /. Solve[0.8 == 1/(2/T - a0), T][[1]]

(* Define model and fit *)
modeln = 1/(x/T - a);
fit = FindFit[datan, modeln, {{a, a0}, {T, T0}}, x]
(* {a -> -0.77186, T -> 4.34542} *)
Show[ListPlot[datan], Plot[modeln /. fit, {x, -2, 2}]]


Same approach works for the other dataset.

ListPlot[Transpose[{datan[[All, 1]], data}], Frame -> True]


(* When x=0, then a0 = -1/y *)
a0 = -1/0.007
(* When x=2, then plug in a0 and solve for T *)
T0 = T /. Solve[0.005 == 1/(2/T - a0), T][[1]]

fit = FindFit[Transpose[{datan[[All, 1]], data}], modeln, {{a, a0}, {T, T0}}, x]
(* {a -> -145.694, T -> 0.0287942} *)
Show[ListPlot[Transpose[{datan[[All, 1]], data}]], Plot[modeln /. fit, {x, -2, 2}]]


It's not necessary to give "good" starting values, you only need constraint a<0 together with NonlinearModelFitand option Method -> "NMinimize":

modeln = 1/(x/T - a);

fit = NonlinearModelFit[datan, {modeln, a < 0}, {a, T}, x, Method -> "NMinimize"]
Show[{Plot[fit[x], {x, -2, 2}], ListPlot[datan]}]


The second case is solved similar...

• Can you describe how the initial values are chosen when Method -> "NMinimize" is used? It also seems essential to have the constraint a < 0 included otherwise NonlinearModelFit provides a wrong solution.
– JimB
Commented Jul 8, 2021 at 15:43
• @JimB I mentioned the need for the constraint a<0 in my answer too. How NMinimize handles initial values is out of my knowledge. Commented Jul 9, 2021 at 6:12
• Thanks. I know you mentioned that a needs to be less than zero. I was only emphasizing (but not clearly) that constraint was essential. You've suggested Method -> "NMinimize" many times in the past (as I have harped on good starting values zillions of times in the past) but including constraints with Method -> "NMinimize" wasn't always necessary. Hence my curiosity as to what that Method actually does.
– JimB
Commented Jul 9, 2021 at 15:41