# FindFit returns Infinity::indet error when data contains {0,0}

Let's consider the following data:

data = Table[{q, 2*q^2}, {q, 0, 2}];


and then, do a very simple fitting to this data:

FindFit[data, a*q^b, {a, b}, q]


Infinity::indet: Indeterminate expression 0. (-\[Infinity]) encountered. >>
FindFit::nrjnum: The Jacobian is not a matrix of real numbers at {a,b} = {1.,1.}. >>
Out= {a -> 1., b -> 1.}


if we remove the {0,0} point:

FindFit[Rest@data, a*q^b, {a, b}, q]


we correctly get:

{a -> 2., b -> 2.}


I've read this, which is very similar, but even if I hint it to start with the correct values, I get the same error message:

FindFit[data, a*q^b, {{a, 2}, {b, 2}}, q]


This would seem a too basic limitation of Mathematica 8 fitting algorithms, and so, before accusing it, what am I doing wrong?

Well, FindFit works by calculating a Jacobian of your expression, which is a matrix of derivatives with respect to all variables. Analytically, because $b$ is a real variable (and not an integer), you have $q^b = \exp(b \log q)$, so you see how that might become difficult when $q$ becomes zero.

However, the documentation for FindFit describes exactly your problem, and a possible solution. (I found it by looking for "FindFit Jacobian" in the documentation, so it's possible to find from your original error message.)

Specify the model gradient to avoid problems with a removable singularity:
[…] Gradient -> "FiniteDifference"

This works for you too:

In:=  FindFit[data, a*q^b, {a, b}, q, Gradient -> "FiniteDifference"]
Out=  {a -> 2., b -> 2.}

• Nothing to do with your answer but, I feel like the automatic choice of the best algorithm for the user case (typical characteristic of MMA capabilities) is completely failing in this one... But thank you for the quick answer! – P. Fonseca Apr 7 '12 at 10:02
• As a general rule, I would say that if one is able of doing a decent fit with Manipulate, there's no reason for Mathematica to fail on a completely automated answer... wouldn't you agree? – P. Fonseca Apr 7 '12 at 10:08
• The same manual workaround, with the Gradient option setting, seems to drastically improve the results from some uses of NonlinearModelFit. – murray Apr 7 '12 at 21:59

I think the problem has already been diagnosed correctly by F'x, but I wanted to mention two other work-arounds.

(a)

FindFit[data, a*q^b, {a, b}, q, Method -> "PrincipalAxis"]


(b)

FindFit[data, a*q^b, {{a, 1,10}, {b,1,10}}, q]


Maybe the last one is the best because it doesn't require you to remember a Method specification. All it does is to provide two initial guesses instead of one for the variables. This doesn't seem to be documented (I just went by what I knew from the closely related FindMinimum and it turned out to work for FindFit, too).

As you can see in (b), the initial bracketing values for a and b don't have to be very good guesses at all, which should make this approach useful in practice.