# Getting error General::munfl from NonlinearModelFit [duplicate]

I am trying to fit data to a sum of Gaussians.

This data is found in a file called lpeff.txt. So I write the following code:

model =
V1*Exp[-((r - μ1)^2/σ1^2)] + V2*Exp[-((r - μ2)^2/σ2^2)] + V3*Exp[-((r - μ3)^2/σ3^2)];

(* mydata =
Import[
"C:\\Users\\em\\Documents\\lambda_we_threshold2\\vot\\lambda_p\\effective_lp\\lpeff.txt",
{"Data", All, {1, 2}}]; *)
mydata = Import["https://pastebin.com/raw/7m8XF3EN", "Table"];

myfit =
NonlinearModelFit[
mydata, model,
{{V1, 53.55}, {μ1, 0.09466}, {σ1, -0.3954},
{V2, 36480}, {μ2, -0.4239}, {σ2, 0.2253},
{V3, -49.63}, {μ3, 0.3712}, {σ3, 0.1697}},
r,
MaxIterations -> Infinity];


When I run the code, I get the following error:

General::munfl: 4.3598*10^-308/2 is too small to represent as a normalized machine number; precision may be lost

How can I fix this?

• Increase the precision – ktm Jul 19 '19 at 14:15
• – Michael E2 Jul 19 '19 at 14:31
• Assuming that you are needing to perform a regression rather than fitting a mixture of probability distributions...having the V2 starting value at 36480 completely dominates the other two gaussian shapes. Also, while not a consequential error, having the starting value for $\sigma_1$ being negative is very odd. – JimB Jul 19 '19 at 14:36
• The issue might not be "fixing" underflow but that underflow is a consequence of poor starting values or a model that just doesn't fit the data very well. I can't tell if this is a duplicate without the data. Is it possible to share the data? – JimB Jul 19 '19 at 14:42
• I can get LinearAlgebraBLASTRSV::oflow and Divide::infy errors but not General::munfl. Problems with code generally require the code for the problems to be diagnosed (i.e., mydata) – Michael E2 Jul 19 '19 at 14:53

This might be an answer (but without your data or at least the range of values of your values for r I can't be certain).

Here is the plot of model predictions given the starting values used in NonlinearModelFit:

model = V1*Exp[-((r - μ1)^2/σ1^2)] + V2*Exp[-((r - μ2)^2/σ2^2)] + V3*Exp[-((r - μ3)^2/σ3^2)];
Plot[model /. {V1 -> 53.55, μ1 -> 0.09466, σ1 -> -0.3954, V2 -> 36480, μ2 -> -0.4239,
σ2 -> 0.2253, V3 -> -49.63, μ3 -> 0.3712, σ3 -> 0.1697}, {r, -2, 1}, PlotRange -> All]


Given this result, you might get the error messages you show whenever values of r are less than -5 and greater than +5.

model /. {V1 -> 53.55, μ1 -> 0.09466, σ1 -> -0.3954,
V2 -> 36480, μ2 -> -0.4239, σ2 -> 0.2253,
V3 -> -49.63, μ3 -> 0.3712, σ3 -> 0.1697} /. r -> -5


model /. {V1 -> 53.55, μ1 -> 0.09466, σ1 -> -0.3954,
V2 -> 36480, μ2 -> -0.4239, σ2 -> 0.2253,
V3 -> -49.63, μ3 -> 0.3712, σ3 -> 0.1697} /. r -> 5


So to tell whether you just have very small numbers whose underflow needs fixing or you need better starting values depends on your data.

I get a different error (version difference?) but a good fit:

\$Version
(*  "12.0.0 for Mac OS X x86 (64-bit) (April 7, 2019)"  *)

mydata = Import["https://pastebin.com/raw/7m8XF3EN", "Table"];

model = V1*Exp[-((r - μ1)^2/σ1^2)] +
V2*Exp[-((r - μ2)^2/σ2^2)] +
V3*Exp[-((r - μ3)^2/σ3^2)];

myfit = NonlinearModelFit[mydata,
model, {{V1, 53.55}, {μ1, 0.09466}, {σ1, -0.3954}, {V2,
36480}, {μ2, -0.4239}, {σ2,
0.2253}, {V3, -49.63}, {μ3, 0.3712}, {σ3, 0.1697}}, r,
MaxIterations -> Infinity];


LinearAlgebraBLASTRSV::oflow: Machine overflow encountered during computations.

Plot[myfit[r],
{r, Min[data[[All, 1]]], Max[data[[All, 1]]]},
Prolog -> {Red, Point@data}, PlotRange -> All]


The parameter values of V2 and μ2 seem extremely large and offsetting:

myfit["BestFitParameters"]
(*
{V1 -> 40.168, μ1 -> 0.136379, σ1 -> -0.389159,
V2 -> 3.13376*10^162, μ2 -> -37.1206, σ2 -> 1.93767,
V3 -> -45.3854, μ3 -> 0.376968, σ3 -> 0.164943}
*)


It seems clear that the left-hand side of the data looks nothing like a normal distribution, which probably is the reason for the parameter values.

• Thank you for the observation on the distribution of the data. Do you perhaps have any alternative suggestions on functions I can use to fit this data? – Otto Jul 20 '19 at 17:08
• @Otto I'm not data scientist, but I believe the idea is that there is usually a scientific basis for the model, in which the parameters correspond to actual quantities in the system you're studying. On a numerical/mathematical basis, it's always possible to add more functions and get a better fit, but using random functions leads to a scientifically meaningless solution. If it's just a numerical fit in order to have, say, a smooth symbolic approximation to the data, then myfit does not seem too bad. I just doubt the parameters have any meaning. – Michael E2 Jul 20 '19 at 17:33
• Gaussian or sum of Gaussians are ideal for representing interactions between the types of elementary particles I'm studying. – Otto Jul 20 '19 at 18:13
• It would appear that the errors you and I have observed can be avoided by increasing precision in the computation. How does one change precision in Mathematica? – Otto Jul 20 '19 at 18:15
• @Otto I tried this: NonlinearModelFit[SetPrecision[mydata, 20], model[[{1, 3}]] + a (1/(r - b)^2), SetPrecision[ List @@@ myfit["BestFitParameters"][[{1, 2, 3, 7, 8, 9}]], 20]~ Join~{{a, 1}, {b, 0}}, r, PrecisionGoal -> 8, WorkingPrecision -> 20, MaxIterations -> 200] and got no errors, but basically got the same fit, which should be expected, given how good myfit is. The error did not result in a bad computed fit, as it turns out. – Michael E2 Jul 20 '19 at 18:28