I would like to write the following statement:

data2fit = NonlinearModelFit[data2, {a Sqrt[b x + c] + d, b x + c > 0}, {a, b, c, d}, {x}]

However, it returns unevaluated. Without the constraint, I get something like:

NonlinearModelFit::nrnum: The function value -1.86156*10^15+1.42765*10^13 I is not a real number at {a,b,c,d} = {-145129.,1.,-739.575,-14092.9}. >>

It doesn't seem to like the fact that there is a constraint that includes the independent variable x. The fit can be made to work if I use Abs[b x + c] in the model, but that's not really the same thing.

Is there a good way to specify this type of constraint?

  • $\begingroup$ Does dropping the constraint but using Sqrt[Max[b x + c, 0]] work? If not, can you share your data? I also wonder if making good guesses at the final values would help avoid the errors. $\endgroup$ – JimB Jan 2 '16 at 3:15
  • $\begingroup$ How about something like this: NonlinearModelFit[data2, {a Sqrt[b x + c] + d, b Min[data2[[All, 1]]] + c > 0}, {{a, 1}, {b, 0.5}, {c, 1}, {d,1}}, {x}] while making sure that the initial values satisfy the constraint? In other words replace x in the constraint with Min[data2[[All,1]]]. $\endgroup$ – JimB Jan 2 '16 at 3:41
  • $\begingroup$ One way to approach it, not necessarily the best, is to transform the model so that real and imaginary parts of the parameters are fitted simultaneously, where you can give an additional (implicit) constraint that the imaginary part is to be zero. I showed how to use a package I wrote for this here. $\endgroup$ – Oleksandr R. Jan 2 '16 at 3:44
  • 4
    $\begingroup$ You may like to note as well that your parameters a, b, and c are very strongly correlated with one another. Recovering meaningful values for them may be difficult unless there are other constraints to guide the process. $\endgroup$ – Oleksandr R. Jan 2 '16 at 4:09
  • 2
    $\begingroup$ @OleksandrR. makes a very good point. Those 3 parameters are so strongly related that using a Sqrt[x + c] +d and a Sqrt[b x + 1] + d are equivalent models. There are really only two parameters to fit. $\endgroup$ – JimB Jan 2 '16 at 5:23

Your Answer

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

Browse other questions tagged or ask your own question.