# Mathematica FindFit doesn't work for me in Mathematica

Tags:
1. Jun 29, 2014

### nikolafmf

Hello,

I want to fit a two-parameter function to two points in this way:

FindFit[{{0, 1}, {1, 0}}, D + C *Log[-1 + Sqrt[-C^2 + (-1 + x)^2] + x], {C, D}, x]

But Mathematica won't give the values for parameters C and D. Instead it says:

"FindFit::nrlnum: "The function value {0. -3.14159\ I,1. -1.5708\ I}\\n is not a list of real numbers with dimensions {2} at {C,D} = {1.,1.}."

Does anyone know what is the problem?

2. Jun 29, 2014

### Bill Simpson

First, C and D have pre-defined meaning to Mathematica and using those as variable names can get you into all kinds of trouble. I use c and d instead.

Next, your probably expect this to be a Real valued function. Look inside that square root. Your point {1,0} has x==1 and you are taking Sqrt[-c^2+(-1+1)^2]==Sqrt[-c^2]. The only c that gives you a Real value would be c==0. Bu you then have d+c*Log[-1+0+1]==d+c*Log[0]== d+c*-Infinity. Your y value for that is 0 so you have d==0, well as long as you are willing to accept that 0*-Infinity==0. When you try to substitute that c,d into your equation for your first point you get 0+0*-Infinity==1. So there is no c,d that is going to give you a Real valued function and go through both of your points.

The warning/error message from Mathematica is telling you that it is probing around, trying to see how good a fit various values of c and d will give you and it keeps finding complex values.

What parts of this do you want to give up and maybe there will be a compromise that will work.

3. Jun 30, 2014

### nikolafmf

Hm. I got that logarithmic function as a minimization problem calculated by Euler-Lagrange equations. It is strange not to have a solution.

4. Jun 30, 2014

### Bill Simpson

You could try another method of getting a solution

Code (Text):
In[1]:= sol = NMinimize[
Abs[d + c*Log[-1 + Sqrt[-c^2 + (-1 + 1)^2] + 1] - 0] +
Abs[d + c*Log[-1 + Sqrt[-c^2 + (-1 + 0)^2] + 0] - 1], {c, d}][[2]]

Out[1]= {c -> -0.0764088, d -> 0.05372}

In[2]= Plot[{Re[d + c*Log[-1 + Sqrt[-c^2 + (-1 + x)^2] + x]],
Im[d + c*Log[-1 + Sqrt[-c^2 + (-1 + x)^2] + x]]} /. sol, {x, -1, 2}]

Out[2]= ...RealAndComplexPlotsOverlaidSnipped...
Or maybe go back through the calculations that got you here and see if there might be something you didn't expect.

5. Jul 2, 2014

### nikolafmf

Ok, I have changed the model, but still don't get a result with FindFit. I try this:

FindFit[{{0, 1}, {1, 0}}, d + c Log[1 + Sqrt[-c^2 + (1 + x)^2] + x], {c, d}, x]

But there is an answer to this problem, since I found a curve by explicitly solving the system of two equations with two unknowns which come from the boundary conditions. It is like this:

Plot[-0.949989* Log[1 + Sqrt[-0.949989^2 + (1 + x)^2] + x] + 1.258177, {x, 0, 1}].

Why FindFit doesn't work?

6. Jul 3, 2014

### Bill Simpson

Mathematica sometimes does not have "mathematical maturity" which is a bright student looking at a problem and seeing which way makes sense and solving it that way. It can be thrown a vast number of different problems and has to have code that tries to figure out what to do.

If I try this

FindFit[{{0, 1}, {1, 0}}, d + c Log[1 + Sqrt[-c^2 + (1 + x)^2] + x], {c, d}, x]

then in trying to pick points to test it looks like it uses one of your endpoints and ends up with 1/Sqrt[0] and another end point ends up with the Jacobian failing.

If I try this

FindFit[{{0, 1}, {1, 0}}, d+c Log[1+Sqrt[-c^2+(1+x)^2]+x], {c,d}, x, Method->"NMinimize"]

then it stumbles once with {c -> 1.3103615131441126, d -> 0.29014492166092026} where the function is not real for x< about .31 and probably ends soon after with {c -> -0.499922, d -> 0.934341} which goes through one of your end points but misses the other by a bit.

So I think the answer to your question "Why FindFit doesn't work?" is that Mathematica isn't perfect and sometimes trying to fit functions that can be complex valued for much of their domain it can fail.

Trying to NMinimize on the sum of squares fails, again because your function can be complex. But this

In[12]:= NMinimize[Abs[d + c Log[1 + Sqrt[-c^2 + (1 + 0)^2] + 0] - 1] +
Abs[d + c Log[1 + Sqrt[-c^2 + (1 + 1)^2] + 1] - 0], {c, d}]

Out[12]= {2.42687*10^-10, {c -> -0.949989, d -> 1.25818}}

succeeds because Abs specifically avoids the problem with Complex numbers in your problem.

If you read the documentation for FindFit and click on all the links in that and all the links in those then you can stumble on a note that says for a good fit you sometimes need to give it good starting points. So

In[13]:= FindFit[{{0, 1}, {1, 0}}, d+c Log[1+Sqrt[-c^2+(1+x)^2]+x], {{c,-.95}, {d,1.25}}, x]

Out[13]= {c -> -0.949989, d -> 1.25818}

I don't know how far from that solution you can start and still have it succeed.