FindFit doesn't work for me in Mathematica

  • Context: Mathematica 
  • Thread starter Thread starter nikolafmf
  • Start date Start date
  • Tags Tags
    Mathematica Work
Click For Summary

Discussion Overview

The discussion revolves around the challenges of using the FindFit function in Mathematica to fit a two-parameter logarithmic model to a set of data points. Participants explore issues related to complex values, variable naming conflicts, and alternative fitting methods.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Mathematical reasoning

Main Points Raised

  • One participant reports an error when using FindFit, indicating that the function returns complex values instead of real numbers.
  • Another participant suggests that the variable names C and D conflict with predefined meanings in Mathematica, recommending the use of lowercase c and d instead.
  • Concerns are raised about the nature of the logarithmic function being fitted, particularly regarding the square root term that can lead to complex results for certain values of c.
  • A different approach using NMinimize is proposed, which yields specific values for c and d that appear to work better than FindFit.
  • One participant mentions having derived a curve from boundary conditions, suggesting that a solution exists despite issues with FindFit.
  • There is a discussion about Mathematica's limitations in handling complex-valued functions and the importance of providing good starting points for fitting.
  • Another participant shares that using the absolute value in NMinimize can help avoid issues with complex numbers, leading to successful parameter estimation.

Areas of Agreement / Disagreement

Participants express differing views on the reasons for FindFit's failure, with some attributing it to variable naming and others to the nature of the function being fitted. There is no consensus on a single solution, as multiple approaches and perspectives are presented.

Contextual Notes

Participants note that the function being fitted can yield complex values, which complicates the fitting process. There are also references to the need for good initial parameter estimates to improve the chances of success with FindFit.

Who May Find This Useful

This discussion may be useful for Mathematica users encountering issues with fitting functions, particularly those involving logarithmic or complex-valued models.

nikolafmf
Messages
112
Reaction score
0
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?
 
Physics news on Phys.org
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.
 
  • Like
Likes   Reactions: 1 person
Hm. I got that logarithmic function as a minimization problem calculated by Euler-Lagrange equations. It is strange not to have a solution.
 
You could try another method of getting a solution

Code:
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.
 
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?
 
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.
 
  • Like
Likes   Reactions: 1 person

Similar threads

  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 13 ·
Replies
13
Views
3K
Replies
10
Views
3K
  • · Replies 4 ·
Replies
4
Views
10K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 2 ·
Replies
2
Views
3K