Mathematica FindFit doesn't work for me in Mathematica

  • Thread starter Thread starter nikolafmf
  • Start date Start date
  • Tags Tags
    Mathematica Work
AI Thread Summary
The user is attempting to fit a two-parameter function using Mathematica's FindFit but encounters issues due to complex values arising from the function's structure. The discussion highlights that using C and D as variable names can cause conflicts, suggesting the use of lowercase c and d instead. It explains that the function's domain leads to complex outputs, particularly when evaluating at certain points, which prevents a valid fit through the specified data points. Alternative methods like NMinimize are proposed, which can yield real-valued solutions by avoiding complex numbers. Ultimately, the conversation emphasizes that Mathematica may struggle with certain mathematical complexities, requiring careful consideration of starting points for successful fitting.
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 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 1 person

Similar threads

Replies
1
Views
2K
Replies
4
Views
2K
Replies
1
Views
1K
Replies
13
Views
2K
Replies
4
Views
10K
Replies
1
Views
2K
Replies
2
Views
2K
Back
Top