# How can I use NonlinearModelFit with multiple variables? [closed]

I am trying to find the coefficients for following equation:

$\quad \quad f(x,\,y,\,z) = a (x^2)\, exp(b x + y^2) + c z^2 y$

using NonlinearModelFit. But I can't find an example of how to formulate the data matrix and equation for NonlinearModelFit when there is more than one independent variable. Can someone provide an example?

## closed as off-topic by Sjoerd C. de Vries, Oleksandr R., bbgodfrey, Dr. belisarius, Daniel LichtblauMay 1 '15 at 14:20

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." – Sjoerd C. de Vries, Oleksandr R., bbgodfrey, Dr. belisarius, Daniel Lichtblau
If this question can be reworded to fit the rules in the help center, please edit the question.

• If you mean to generate a matrix with this formula, you can try Table. – happy fish May 1 '15 at 4:29
• Welcome to Mathematica.SE! I suggest that: 1) You take the introductory Tour now! 2) When you see good questions and answers, vote them up by clicking the gray triangles, because the credibility of the system is based on the reputation gained by users sharing their knowledge. Also, please remember to accept the answer, if any, that solves your problem, by clicking the checkmark sign! 3) As you receive help, try to give it too, by answering questions in your area of expertise. – bbgodfrey May 1 '15 at 4:33
• The first example in the "Scope" block of the NonlinearModelFit help page precisely shows the necessary steps. Took me less than 15 secs to find. – Sjoerd C. de Vries May 1 '15 at 6:42

This is what I understand you're asking:

For your problem the data matrix has to be of the form {{x,y,z,f},{...},...}. I will create some data.

data = MapThread[{#1[[1]], #1[[2]], #1[[3]],
0.34*(#1[[1]]^2)*Exp[-1.82*#1[[1]] + #1[[2]]^2] +
0.94*(#1[[3]]^2)*#1[[2]] + #2} &,
{RandomReal[1, {100, 3}], RandomReal[{-0.01, .01}, 100]}];


Then,

model = a*(x^2)*Exp[b*x + y^2] + c*(z^2)*y
NonlinearModelFit[data, model, {a, b, c}, {x, y, z}]

(*FittedModel[0.385936 E^(-1.89747 x + y^2) x^2 + 0.943513 y z^2]*)


To generate data using the given equation, you can use different values for {a,b,c} to generate data. Here's a method using With where {a,b,c} are local variables:

f[x_, y_, z_] := With[{a = 0.1, b = -3., c = 1.2}, a *x^2 *Exp[b *x + y^2] + c* z^2* y];


Then you need to build you dataset; You can use Table and Flatten to have your data in suitable format ({x,y,z,f[x,y,z]}):

data = Flatten[
Table[{x, y, z, f[x, y, z]}, {x, -3, 3}, {y, -3, 3}, {z, -3, 3}],
2];


then using NonLinearModelFit is simple:

NonlinearModelFit[data,
a *x^2 *Exp[b *x + y^2] + c* z^2* y, {a, b, c}, {x, y, z}]
(* 0.1 *x^2 *Exp[-0.3 *x + y^2] + 1.2 * z^2 * y *)


therefore fitted values for {a,b,c} are the same as given in $f(x,y,z)$ definition.