FindFit doesn't work for me in Mathematica

In summary: Whether you think of that as a good thing or a bad thing is in the mind of the beholder, but for some problems if you have a solution that is close to the answer then FindingFitting can be a good path to get you to that answer.In summary, Mathematica can struggle with fitting complex-valued functions, and may require good starting points in order to successfully find a fit. It may also be helpful to try alternative methods, such as NMinimize, to find a solution.
  • #1
nikolafmf
114
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
  • #2
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
  • #3
Hm. I got that logarithmic function as a minimization problem calculated by Euler-Lagrange equations. It is strange not to have a solution.
 
  • #4
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.
 
  • #5
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
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

1. Why is FindFit not working for me in Mathematica?

The most common reason for FindFit not working is that the data provided may not be in the correct format. Make sure that the data is in the form of a list of points, with x and y values. Additionally, check the syntax of your FindFit function to ensure that it is correct.

2. What should I do if FindFit is giving me incorrect results?

First, double check that your data is accurate and in the correct format. If the data is correct, try adjusting the initial values and constraints in your FindFit function. Sometimes, providing more accurate initial values can improve the results. You can also try using a different fitting model to see if it better captures your data.

3. Can FindFit handle large datasets?

Yes, FindFit is designed to handle large datasets. However, it may take longer to run for larger datasets. You can also try using the "Method->" option in your FindFit function to specify a specific method that may be more efficient for your dataset.

4. Is there a way to visualize the fitting results in Mathematica?

Yes, you can use the Plot or ListPlot function in Mathematica to plot your original data points and the fitted curve together. This allows you to visually assess the accuracy of the fitting results.

5. Can I use FindFit for nonlinear fitting?

Yes, FindFit is capable of handling nonlinear fitting. You can specify the fitting model using the "Model->" option in your FindFit function. Make sure to provide accurate initial values and constraints for the parameters in your fitting model.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
257
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
13
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
932
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
9
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
10K
Back
Top