# Problem with fitting square root-like function

I have following data:

data={{374.46707, 0.665317421546049}, {375.52396999999996,
0.660664852164608}, {376.58087, 0.665317421546049}, {377.63777,
0.651359713401726}, {378.69467,
0.651359713401726}, {379.75156999999996,
0.637402005257403}, {380.80859999999996,
0.637402005257403}, {381.8655, 0.609486588968758}, {382.9224,
0.600181450205876}, {383.97929999999997,
0.56761346453579}, {385.0362, 0.507130062577058}, {386.0931,
0.407565077814223}, {387.15, 0}};


and I am fitting following function:

    nlm = NonlinearModelFit[data, {3/4 A (1 + Sqrt[1 - (8 ((x - B)+(x - B)^2))/(9(387.15 - B))]), 0.0 < A < 0.4, 387 < B < 387.15}, {{B,387}, {A, 0.306}}, x]


and I get an error, from which other errors follow:

NonlinearModelFit::nrnum: The function value -95.7402-17.0695 I is not a real number at {B,A} = {387.,0.306}.

What I am doing wrong?

• What does the error say?
– JimB
Oct 22, 2018 at 17:38
• NonlinearModelFit::nrnum: The function value -95.7402-17.0695 I is not a real number at {B,A} = {387.,0.306}. IPOPTMinimize::badobj: Invalid objective function. The objective function doesn't evaluate to a real-valued numeric result at the initial point. NonlinearModelFit::nrgnum: The gradient is not a vector of real numbers at {B,A} = {387.,0.306}. Oct 22, 2018 at 17:48
• The error message is telling you that you're imaginary numbers for at least some of the observations given the starting value of B = 387. Also the starting value you give isn't really in the range of values for which you've restricted B. I would remove the restriction on B and use 395 as the starting value.
– JimB
Oct 22, 2018 at 17:53

## 1 Answer

FindFit gives a quite good approximation! Try a fit function which vanishs for x=387.15

fit = FindFit[data,Sqrt[ 387.15 - x] (b  + c ( 387.15 - x) )/(1 + d ( 387.15 -x)), {b, c, d}, x]
(*{b -> 0.456939, c -> 0.0116198, d -> 0.175918}*)

Show[{ListPlot[data],
Plot[Sqrt[ 387.15 - x] (b  + c ( 387.15 - x) )/(1 + d ( 387.15 - x)) /. fit, {x, 375, 387.15},
PlotRange -> All]}, PlotRange -> {0, .7}]


Of course NonlinearModelFit also works:

nmf = NonlinearModelFit[data,Sqrt[ 387.15 - x] (b  + c ( 387.15 - x) )/(1 + d ( 387.15 - x)), { b, c, d}, x];
Normal[nmf]
(*((0.456939 + 0.0116198 (387.15 - x)) Sqrt[387.15 - x])/(1 +0.175918 (387.15 - x))*)