2

I am having trouble using NonlinearModelFit to fit to data generated by the equation I am trying to fit to.

test = Table[{x, ((Sqrt[3] + Sqrt[x + 3])^2)^(2/3) - 
                  Sqrt[3 ((Sqrt[3] + Sqrt[x + 3])^2)^(1/3)] - 
                 ((Sqrt[3] + Sqrt[3])^2)^(2/3) - 
                  Sqrt[3 ((Sqrt[3] + Sqrt[3])^2)^(1/3)] + 5},
             {x, -3, 5, .1}
            ]

Here is the data I generated with the equation.

 Blah = ListPlot[test]

Plot of data

This is what it looks like

nlm = NonlinearModelFit[
  test, ((Sqrt[y] + Sqrt[F + y])^2)^(2/3) - 
   Sqrt[y ((Sqrt[y] + Sqrt[F + y])^2)^(1/
     3)] - ((Sqrt[y] + Sqrt[y])^2)^(2/3) - 
   Sqrt[y ((Sqrt[y] + Sqrt[y])^2)^(1/3)], {y}, F]

My attempt to fit. The error says there are imaginary numbers yet there are none.

MarcoB
  • 67,153
  • 18
  • 91
  • 189

2 Answers2

4

Here it is with the constraint. I changed the symbols to a,x just for readability (There was nothing wrong with the y,F except that single Caps are good to avoid )

nlm = NonlinearModelFit[
   test,
    {((Sqrt[a] + Sqrt[x + a])^2)^(2/3) - 
          Sqrt[a ((Sqrt[a] + Sqrt[x + a])^2)^(1/3)] - ((Sqrt[a] + 
          Sqrt[a])^2)^(2/3) - Sqrt[a ((Sqrt[a] + Sqrt[a])^2)^(1/3)]
            +5 , (* constraint *) a >= 3}, {a}, x];

 nlm["BestFitParameters"] 
{a -> 3.}

You were also missing the +5..btw.

Dr. belisarius
  • 115,881
  • 13
  • 203
  • 453
george2079
  • 38,913
  • 1
  • 43
  • 110
2

The following trick sometimes work:

f[a_?NumericQ, x_?NumericQ] := 
 Module[{s = ((Sqrt[a] + Sqrt[x + a])^2)^(2/3) - 
               Sqrt[a ((Sqrt[a] + Sqrt[x + a])^2)^(1/3)] - ((Sqrt[a] + 
               Sqrt[a])^2)^(2/3) - Sqrt[a ((Sqrt[a] + Sqrt[a])^2)^(1/3)] + 5},
        10^3 Im@s + Re@s]

nlm = NonlinearModelFit[test, {f[a, x]}, {a}, x, 
                       Method -> {"NMinimize", Method -> "SimulatedAnnealing"}]

nlm["BestFitParameters"] 
(* {a -> 3.} *)
Dr. belisarius
  • 115,881
  • 13
  • 203
  • 453