# How do I use FindFit for an equation which has complex roots?

I have to fit an equation of the following form: $$\displaystyle 4ax\sqrt{x^2 + 9}\hspace{3mm} \bigg(1 - \frac{\sqrt{x^2 + 9}}{(b+1)c}\bigg)^b$$ where, a, b and c are parameters to be found w.r.t the fit, all of which are positive.

Now, the value of x is bound within the experiment, but I guess the formula should not limit the range of the equation. So, when I try to use FindFit or NonlinearModelFit, I encounter errors like this:

FindFit::nrjnum: The Jacobian is not a matrix of real numbers at {c,b} = {1.,1.}.

NonlinearModelFit::nrgnum: The gradient is not a vector of real numbers at {a,c,b} = {1.,1.,1.}.

NonlinearModelFit::grad: Evaluation of the gradient of function ExperimentalNumericalFunction[<<1>>] failed at {1.,1.,1.}.


Now, I understand that I encounter this error due to the term in the brackets taking negative values, and parameter b going fractional, thus leading to complex terms in the computation. What I don't know is how to solve the error, and get the required values for my parameters. So, any and all help would be more than appreciated.

I did not provide my experimental data set as I just want help with getting the parameters a, b and c, and I believe this could be done with any random data set.

Edit:

This is my Dataset:

DataSet =
List[{2.49816, 0.00669404}, {2.29963, 0.00880956}, {2.1011,
0.0128852}, {1.90257, 0.0218499}, {1.74816, 0.0312905}, {1.6489,
0.0378427}, {1.54963, 0.0477418}, {1.45037, 0.0669404}, {1.3511,
0.0826858}, {1.25184, 0.11114}, {1.15257, 0.143207}, {1.04779,
0.205082}, {0.976103, 0.258728}, {0.926471, 0.299961}, {0.876838,
0.347764}, {0.827206, 0.403185}, {0.777574, 0.498021}, {0.722426,
0.615164}, {0.672794, 0.698291}, {0.623162, 0.826858}, {0.573529,
1}, {0.523897, 1.23522}, {0.474265, 1.40213}, {0.424632,
1.66029}, {0.375, 2.00795}, {0.325368, 2.32794}, {0.275735,
2.69894}, {0.226103, 2.99961}, {0.165441, 3.06365}, {0.126838,
2.87551}, {0.110294, 2.64252}]


I have to fit it to the equation:

$$\frac{a x \sqrt{19479.83514523 + x^2}}{4 \pi^2} \bigg(1 + (d - 1) \frac{\sqrt{19479.83514523 + x^2}}{c}\bigg)^{\frac{-d}{d-1}}$$

I use $b = \frac{d}{d-1}$ and rewrite the equation to obtain the form:

$$\frac{a x \sqrt{19479.83514523 + x^2}}{4 \pi^2} \bigg(1 + \frac{\sqrt{19479.83514523 + x^2}}{(b-1)c}\bigg)^{-b}$$

And thus, I try to fit my data with this revised equation to get the values of a, b and c [following the procedure by Matthias Bernien in the answer]. But, when I plot the original data set, and the obtained equation from the fit, I get two entirely different graphs. Where am I going wrong, and how do I get the values of parameters a, b and c, so as to fit my data set?

• Regarding the data: While taking a random dataset is always a possibility, it would still be nice if you would provide one. This ensures a few things: Everyone has the same example to work with and people trying to help do not need to first generate some data to check whether they even show the same issues as your real data. And regarding the errors: The errors mention variables T,p and V, but the equation does not. Can you make the names consistent to make it easier to see what's happening? – Lukas Lang Jun 10 '18 at 15:31
• If you can't provide the data, you could at least provide your Mathematica code. – JimB Jun 10 '18 at 15:42
• You'll need to add in some restrictions on the parameters such as c(b+1) > 3. And for numerical stability you'll probably want to just use c instead of c(b+1) with the restriction that c > 3 which also means that you'll need to provide starting values other than the default values of 1. – JimB Jun 10 '18 at 16:09
• @JimB But then, how do I set b? And I think I can give a bound to (b + 1)c, but what about the case when x exceeds the value of (b + 1)c, or does FindFit take care of it? – Raj Jun 10 '18 at 16:13
• @Mathe172 Edited the question, and replaced the variables in the "error" section by a, b and c. Would see if I could provide a relevant dataset. – Raj Jun 10 '18 at 16:22

If your experimental data is not complex, you can use constraints to make sure that your model stays real. But you have to provide starting values that comply with your constraints. For example:

data = Table[{x, 5 Sin[x/4]^2 + RandomReal[]}, {x, 0, 10, 0.1}];
model = 4 a x Sqrt[x^2 + 9] (1 - Sqrt[x^2 + 9]/((b + 1) c))^b
cons = (1 - Sqrt[Max[data[[All, 1]]]^2 + 9]/((b + 1) c)) >= 0
fit = FindFit[data, {model, cons}, {{a, 1}, {b, 2}, {c, 5}}, x]
Plot[model /. fit, {x, 0, 10}, Prolog -> Point /@ data]


In the example the constraint cons ensures that what is on the left-hand side is not getting negative even for the highest value of x.

• What is cons here, and what is it doing? And can we not find a fit if we don't have some initial values for a, b and c? – Raj Jun 10 '18 at 16:19
• cons` are the constraints. In the example they ensure that what is on the left hand side is not getting negative even for the highest value of x. As starting values you can chose anything that complies with the constraints. – Matthias Bernien Jun 10 '18 at 16:23
• Thank You. I'll try it and get to you if I don't get it. – Raj Jun 10 '18 at 16:26
• I'm still getting difficulties in fitting my data set. I have revised my question, and have added a reference data set and an equation. Please have a look, and advise me how to fit the data, and get my parameters. – Raj Jun 11 '18 at 9:09
• Please can you put your equation in Mathematic form? I don't want to copy it all into Mathematica possibly wrongly. – Hugh Jun 11 '18 at 9:23