# How can I use nonlinear model fit with multiple variables? [closed]

I have a data set with four entries. the first three entries correspond to variables in the fit x,y, & z. The fourth entry corresponds to the function value. I am trying to fit this data to a rate law with several constants. The code is provided below:

data1 = {{1, 1, 0, .0362}, {1, 1, 1, .0239}, {3, 1, 1, .0390}, {1, 3,
1, .0351}, {1, 1, 3, .0114}, {10, 1, 0, .0534}, {1, 10,
0, .0280}, {1, 1, 10, .0033}, {2, 2, 2, .0380}, {1, 1,
4, .0090}, {.6, .6, .6, .0127}, {5, 5, 5, .0566}}

NonlinearModelFit[data1, (
kt*(x*y - z/Kp))/(1 + (x*ka2)^.5 + ka1*y + z/Kd)^3, {kt, Kp, ka1, ka2,
Kd}, {x, y, z}]


How could I modify my code to get Mathematica to fit the indicated parameter values?

Edit: Solved. thanks. I will be more careful next time.

## closed as off-topic by Kuba♦, ubpdqn, xzczd, Karsten 7., Sjoerd C. de VriesApr 27 '15 at 8:26

This question appears to be off-topic. The users who voted to close gave this specific reason:

• "This question arises due to a simple mistake such as a trivial syntax error, incorrect capitalization, spelling mistake, or other typographical error and is unlikely to help any future visitors, or else it is easily found in the documentation." – Kuba, ubpdqn, xzczd, Karsten 7., Sjoerd C. de Vries
If this question can be reworded to fit the rules in the help center, please edit the question.

• You forgot to add Kp to parameters list. And there is a typo, data or data1 at the end. – Kuba Apr 27 '15 at 7:25

As @Kuba mentioned in his comment, you have one syntax error that prevents evaluation of your code: you missed a parameter (Kp) in the list of parameters passed to NonlinearModelFit. Using the value of data defined in your question, here is the updated version with all the necessary parameters:

NonlinearModelFit[
data,
(kt (x y - z/Kp))/(1 + Sqrt[x ka2] + ka1 y + z/Kd)^3,
{kt, ka1, ka2, Kd, Kp},
{x, y, z}
]


The fitting will run after this correction, but the reported best fit function is still wildly off the mark:

(4.93595 (x y - 0.403726 z))/(1 + 1.58293 Sqrt[-x] - 3.22328 y +
0.278425 z)^3


In fact, the values predicted by the reported fit are not even real numbers, as you can see from the fact that the fitting function contains Sqrt[-x], for positive x input!

In order to improve on this unconstrained fit, you will want to provide reasonable starting values for at least some of your fitting parameters. This will significantly improve your chances of finding a reasonable fit with physical significance.

You can pass initial guesses of the parameter values to NonlinearModelFit using the same syntax of the FindFit function. In the documentation for FindFit one finds that:

FindFit[data, expr, {{par1, p1}, {par2, p2}, ...}, vars] starts the
search for a fit with {par1 -> p1, par2 -> p2,...}


In your case, I have no idea what the physical significance of your parameters might be, so I arbitrarily chose to set the starting value for the kt parameter to 10. In the following, note the {kt,10} part in the parameter list.

NonlinearModelFit[
data,
(kt (x y - z/Kp))/(1 + Sqrt[x ka2] + ka1 y + z/Kd)^3,
{{kt, 10}, ka1, ka2, Kd, Kp},
{x, y, z}
]


When you do that, the fitted model is:

(0.308793 (x y - 0.0867491 z))/(1 + 0.805969 Sqrt[x] + 0.300665 y +
0.173308 z)^3


and the predicted values match your experimental data quite well.

As further refinements, you could also add constraints on the parameter values as equations, domain definitions, or inequalities in the fitting model definition. These additional parameters are well described in the documentation of the NonlinearModelFit and FindFit functions.

• Thank you. This post was very helpful. I must admit that I was very perplexed by the output the program was providing me. This clears things up very nicely. – Nick L. Apr 27 '15 at 9:17