1
$\begingroup$

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?

$\endgroup$
  • 4
    $\begingroup$ 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. $\endgroup$ – Oleksandr R. May 20 '14 at 15:19
  • $\begingroup$ ...where's that? $\endgroup$ – Oleksandr R. May 21 '14 at 1:03
  • 1
    $\begingroup$ 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". $\endgroup$ – Alexei Boulbitch May 21 '14 at 8:26
  • $\begingroup$ Thanks for reply. You mean minimization with respect to fitting parameters right? $\endgroup$ – user14494 May 21 '14 at 8:48
  • $\begingroup$ @user14494 Right $\endgroup$ – Alexei Boulbitch May 21 '14 at 14:56
3
$\begingroup$

This is your model:

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: enter image description here The function to check is something like the following:

 Map[#^2 &, 
  Transpose[data][[2]] - 
   Map[(model /. ff /. r -> #) &, Transpose[data][[1]]]] // 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.

$\endgroup$
  • $\begingroup$ 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? $\endgroup$ – user14494 Jun 4 '14 at 8:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.