# Fitting Function with 3 parameters

I am trying to fit a function with 3 parameters however the result strongly depend on the initial values of parameters:

 data = ToExpression@Import["http://pastebin.com/raw.php?i=eSXQuV62"];

FindFit[data,
G/(2/3*x)*(1 - (4.13/G*(Log[k/(x*r)] + G/4.13))^(2/3)),
{{k, 0.0001}, {x, 0.2}, {G, 80}}, r]


the result are

{k -> -0.000803039, x -> 0.613938, G -> 47.9489}


for different set of initial values of parameters

  FindFit[data,
G/(2/3*x)*(1 - (4.13/G*(Log[k/(x*r)] + G/4.13))^(2/3)),
{{k, 0.0001}, {x, 0.2}, {G, 100}}, r]

{k -> -0.00247546, x -> 1.04233, G -> -341.32}


I also get following message

FindFit::nrlnum: "The function value {31.8606 -11.8829\ I,32.8832 -11.8583\ I,26.5785 -11.8548\ I,36.1803 -11.852\ I,27.6782 -11.8511\ I,<<42>>,20.6663 -11.7998\ I,17.8617 -11.7998\ I,7.70671 -11.7992\ I,<<56>>} is not a list of real numbers with dimensions {106} at {k,x,G} = {-0.00247546,1.04233,-341.32}.

Do you know how to solve this issue?

• Yes. 1. use Method -> NMinimize. 2. choose the parameter values or transform the model so that complex numbers do not appear. If you need more than these hints, you'll have to give Data. But try do to make use of them yourself first. May 20, 2014 at 15:19
• ...where's that? May 21, 2014 at 1:03
• Fitting is related to minimization of a function. This function can have several minimums. For this reason the initial guess matters. The best fit corresponds to the deepest minimum. So one way to understand, how close you are to the best solution would be to check the depth of the minimum. Another way might be to visualize your data with the fitting function on the same plot and to check it "by eye". May 21, 2014 at 8:26
• Thanks for reply. You mean minimization with respect to fitting parameters right? May 21, 2014 at 8:48
• @user14494 Right May 21, 2014 at 14:56

model = G/(2/3*x)*(1 - (4.13/G*(Log[k/(x*r)] + G/4.13))^(2/3));


Your model constrains the parameters by either k>0, x>0, or k<0, x<0. Otherwise a negative number gets under the logarithm. On the other hand comparing your model with the data, one finds that only the case k>0, x>0 can possibly fit it. For this reason one can look for the fitting parameters with a limitation of the fitting domain (check Menu/Help/FindFit/Scope/Constraints and Starting Values):

    ff = FindFit[
data, {model, k > 0, x > 0}, {{k, 0.5}, {x, 0.01}, {G, 80}}, r]
Show[{
ListPlot[data],
Plot[G/(2/3*x)*(1 - (4.13/G*(Log[k/(x*r)] + G/4.13))^(2/3)) /.
ff, {r, 500, 14100}, PlotStyle -> Red]
}]


yielding

(*  {k -> 110.323, x -> 0.229302, G -> 17.6893}   *)


I just tried several possible starting values until have found something looking reasonable. The plot looks as follows: The function to check is something like the following:

 Map[#^2 &,
Transpose[data][] -
Map[(model /. ff /. r -> #) &, Transpose[data][]]] // Mean


yielding 145.304 in the case of the present fitting parameters. You could use this function to quantitatively check the fitting quality. The data are so scattered, that I doubt that it is useful though.

• Dear Alexei, thanks for you time and answer, I tried to reproduce it however I did not succeed, you can find uploaded nb file. dropbox.com/s/6kukoxyrjzkm5rm/test%201.nb I will need to fit more than 10 sets of date with this model, and I would like to be able to do this fits myself. Do you have any idea why fitting worked in your case but not in my case? Jun 4, 2014 at 8:50