4
$\begingroup$

I'm trying to use NonlinearModelFit to fit the data:

Data = {{2.046204620462046`, 1.274347668`}, {2.012987012987013`, 1.131369542`}, {1.984`, 0.939944276`}, {1.952755905511811`, 0.721156437`}, {1.9224806201550388`, 0.614591638`}, {1.8931297709923665`, 0.572324748`}, {1.8674698795180722`, 0.536605273`}, {1.8397626112759644`, 0.49948005`}, {1.8128654970760234`, 0.478452409`}, {1.7867435158501441`, 0.455583518`}, {1.7613636363636365`, 0.439702881`}, {1.7342657342657342`, 0.411870668`}, {1.712707182320442`, 0.365204419`}, {1.6870748299319729`, 0.303633546`}, {1.6666666666666667`, 0.232056102`}, {1.6445623342175066`, 0.140710942`}, {1.6230366492146597`, 0.070744185`}, {1.6020671834625324`, 0.035633753`}, {1.597938144329897`, 0.044761696`}}; fit = NonlinearModelFit[Data, B/( 1 + g (x - (1.42 + Ea))^(-3/2)), {B, g, Ea}, x] 

which gives:

NonlinearModelFit::nrlnum: "The function value {-1.22471-0.217191\ I,-1.0682-0.243262\ I,-0.863406-0.265857\ I,-0.628591-0.289822\ I,<<12>>,0.26533 -0.472365\ I,0.318041 -0.47811\ I,0.312374 -0.479155\ I}\n is not a list of real numbers with dimensions {19} at {B,g,Ea} = {1.,1.,1.}."

The function I am using to fit my data gets imaginary values for some values of the parameters. But how can I force Mathematica to avoid these values?

$\endgroup$
5
  • 1
    $\begingroup$ The reason is because it hit a negative value under the root, which ended up generating imaginary numbers. You can try using either assumptions, or use Abs, like this: !Mathematica graphics now it worked. If this meets your model or not, I do not know. I do not do stats. But that is the reason why it failed. $\endgroup$ Commented Aug 25, 2013 at 5:05
  • 1
    $\begingroup$ another option is to add a constraint, like this !Mathematica graphics again, I do not know if this meets your model needs or not. $\endgroup$ Commented Aug 25, 2013 at 5:16
  • $\begingroup$ When I try to evaluate the code, Mathematica 9.0.1 neither evaluates the code nor returns an error !Mathematica graphics. However following what @Nasser has suggested fixes the problem. $\endgroup$ Commented Aug 25, 2013 at 5:31
  • $\begingroup$ @Nasser would you consider an answer? You solved the problem, so why not document it? $\endgroup$ Commented Aug 25, 2013 at 6:42
  • $\begingroup$ This is a common problem. I've addressed it in my preferred fashion in this answer. $\endgroup$ Commented Aug 25, 2013 at 15:37

1 Answer 1

6
$\begingroup$

I run a trace on the command, Trace[NonlinearModelFit[Data, B/(1 + g (x - (1.42 + Ea))^(-3/2)), {B, g, Ea}, x]] and looking at the result, I noticed a check is being done using FreeQ[...,Complex] and right after that, saw lots of $Failed messages (hard to read trace messages).

Mathematica graphics

So when I Saw that I realized it was the use of the root in the model. Internally, it was generating a numerical data to fit, and used values that caused a negative value to appear, for example

 B/(1 + g (x - (1.42 + Ea))^(-3/2)) /. {B -> 1, g -> 1, Ea -> 1, x -> 1} (* 0.7411531317364896 - 0.4380013322510369 I *) 

And NonlinearModelFit does not like to work with complex numbers to make the fit.

So, what to do? Add a constraints, or use Abs value to make sure the value under the root remain non-negative. I also tried Assumptions, but these did not help.

The constraint is added after form. For example

NonlinearModelFit[Data, {B/(1 + g (x - (1.42 + Ea))^(-3/2)), Ea <= 0}, {B, g, Ea}, x] 

Mathematica graphics

It seems your model did not consider that complex numbers can be generated for some values, and the correct constraint is needed to insure this does not happen.

$\endgroup$
1
  • 2
    $\begingroup$ Important to note is that specifying constraints also changes the method from Levenberg-Marquardt to NLIP. Although the latter will certainly obey any constraints given, its convergence properties do not seem to be as good. $\endgroup$ Commented Aug 25, 2013 at 15:43

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.