# fitting – (Non)LinearModelFit hangs on “trivial” linear fit (Fit succeeds)

A simplified version of my function and data:

``````    myfunc(x_, a_) = 8*a*Exp(-8/Sqrt(x))/(Sqrt(x)*Abs(WhittakerW(E*I/Sqrt(x), 1/2, 8/10*I*Sqrt(x)*a))^2)
mydata = {{4.5, 195}, {2.9, 175}, {2.1, 95}}
``````

`myfunc` also needs a global rescaling. If I want to fit both parameter `a` and the scale, NonLinearFitModel works great out of the box and quickly:

``````    NonlinearModelFit(mydata, scale*myfunc(x, a), {scale, a}, {x})
``````

If, however, I only want to find the best scaling for an `a` value of my choice, this:

``````   NonlinearModelFit(mydata, scale*myfunc(x, 4), {scale}, {x})
``````

takes forever in my computer (I aborted after a while, I don’t know whether it would succeed at some point or not). I am surprised: why the “complicated” fit works so easily, and this “trivial” linear one not?

`LinearModelFit` is supposedly the right tool here, but:

``````   LinearModelFit(mydata, {myfunc(x, 4)}, {x}, IncludeConstantBasis -> False)
``````

hangs in the same way. There must be something wrong I am doing.
I tried using 4.0 instead of 4 for `a`, enclosing with an `N()` or putting all constants , adding `WorkingPrecision -> MachinePrecision`, but I get the same result.

NonlinearModelFit works adding `Method -> "NMinimize"`, although it is slow, and raises a warning “You asked for a non-linear method, note that the model is linear”. I do not really know what `NMinimize` is doing here, I guess it applies the “right” simplification to myfunc, so the computation is easier?

Finally, after wasting one day, `Fit` just works:

``````   Fit(mydata, {myfunc(x, 4)}, {x})
``````

This reinforces my impression that I just need to apply some magical simplification, that `Fit` always employs, that `NonlinearModelFit` uses only for the two-parameter fit, and that `Method -> "NMinimize"` will cause. Is `LinearModelFit` evaluating `myfunc` much more times than nedeed? I am clueless.

`LinearModelFit` has several niceties that I would rather not give up for what appears to be such a stupid issue. Furthermore, I am probably going to learn a new important Mathematica quirk from this. Thus:

Why the above code, using (Non)LinearModelFit for a simple `scale*f(x)` model, hangs, and how should I modify it to make it work?

Posted on Categories Articles