# Fitting with constraints including the independent variable

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?

• 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. – JimB Jan 2 '16 at 3:15
• 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]]]. – JimB Jan 2 '16 at 3:41
• 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. – Oleksandr R. Jan 2 '16 at 3:44
• 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. – Oleksandr R. Jan 2 '16 at 4:09
• @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. – JimB Jan 2 '16 at 5:23