Functions of several variables on Mathematica
D. McClendon

Defining functions of several variables

Recall that to define a function ℝ -> ℝ like f(x) = x + sin x, we execute

f[x_] = x + Sin[x]

To define a function whose domain is (ℝ^n), use similar syntax.  There are two valid formats.  The first is as follows:

f[{x_,y_}] = 3x^2 - y^2 Sin[x]

However, you don’t actually need the braces around the x_,y_.  This also works:

g[x_,y_] = 3x^2 - y^2 Sin[x]

Whichever way you use, your subsequent commands should be consistent with the way you defined the function.  For example, after executing the commands above defining f and g,

f[{2,Pi/3}]

works, but

f[2, Pi/3]

doesn’t (because when f was defined, there were braces).  Similarly,

g[2, Pi/3]

works, but

g[{2,Pi/3}]

doesn’t, because when g was defined, there were no braces.

Why might you want to use braces?

Suppose f(x,y) = (xe^(x-2y))  and h(t) = (cos 2t, sin t).  Since h : ℝ -> (ℝ^2)and f : (ℝ^2) -> ℝ, the composition f ∘ h exists as a function ℝ -> ℝ.  But in order for Mathematica to correctly compute this composition, you need the input of f to have braces, since the output of a vector-valued function like h “comes with” braces around the output.    
So when composing functions of several variables, make sure you define the functions using braces around the input variables.

Evaluating expressions and generating tables

This works pretty much the same way as with functions of single-variable functions, although some tweaks need to be made in the Table command to make the table look right:

f[{x_,y_,z_}] = 3x^2 - (2x+z^2)/(y z - x^2);
f[{1,2,-3}]
4f[{-3,1,2}]-2f[{1,0,2}]

Table[{{x,y,z},f[{x,y,z}]}, {x,1,2},{y,4,5},{z,5,8}] //TableForm

For example, the first part of this output is telling you f(1,4,5) = 30/19.

Graphs of functions (ℝ^2) -> ℝ

To graph a function of two variables, use the Plot3D command.  Here, you need to indicate both the input variables with their minimum and maximum values.  For instance, this command will sketch the graph of f(x,y) = 3(x^2) - (y^2) sin x for -5 < x < 5 and -4 < y < 4:

f[{x_,y_}] = 3x^2 - y^2 Sin[x];
Plot3D[f[{x,y}], {x,-5,5},{y,-4,4}]

To see which axis is which, add the same AxesLabel code that was used in the ParametricPlot3D command:

f[{x_,y_}] = 3x^2 - y^2 Sin[x];
Plot3D[f[{x,y}], {x,-5,5},{y,-4,4},
AxesLabel -> {"x","y","z"},
AxesStyle -> Larger]

Plotting multiple graphs on the same axis:

Replace the single function with a list of functions enclosed with braces.

f[{x_,y_}] = 3x^2 - y^2 Sin[x];
g[{x_,y_}] = 20 - x^2/4 - y^4/3;
Plot3D[{f[{x,y}], g[{x,y}]}, {x,-5,5},{y,-4,4},
AxesLabel -> {"x","y","z"},
AxesStyle -> Larger]

Specifying a range of z-values:

Use PlotRange -> {zmin, zmax}.  Notice that when you do this, parts of the graph that would stick above or below your box get shown in gray (which is not ideal).

f[{x_,y_}] = 3x^2 - y^2 Sin[x];
g[{x_,y_}] = 20 - x^2/4 - y^4/3;
Plot3D[{f[{x,y}], g[{x,y}]}, {x,-5,5},{y,-4,4},
AxesLabel -> {"x","y","z"},
AxesStyle -> Larger,
PlotRange -> {-10,20}]

To get rid of the gray parts, insert ClippingStyle -> None:

f[{x_,y_}] = 3x^2 - y^2 Sin[x];
g[{x_,y_}] = 20 - x^2/4 - y^4/3;
Plot3D[{f[{x,y}], g[{x,y}]}, {x,-5,5},{y,-4,4},
AxesLabel -> {"x","y","z"},
AxesStyle -> Larger,
PlotRange -> {-10,20},
ClippingStyle -> None]

If you don’t like the box, you can insert Boxed -> False to get rid of the box.
The BoxRatios command also works the same for Plot3D as it does for ParametricPlot3D.
You can also change the tick marks with the Ticks command, the same as it works for ParametricPlot3D.

Changing the aesthetic appearance of the graphs:

Use the PlotStyle command.  One particularly useful style to add to a graph is Opacity, which makes the graphs partially transparent (Opacity[0] is completely solid, and Opacity[1] is completely transparent; Opacity[.5] is halfway in between, and you can use any number from 0 to 1):

f[{x_,y_}] = 3x^2 - y^2 Sin[x];
g[{x_,y_}] = 20 - x^2/4 - y^4/3;
Plot3D[{f[{x,y}], g[{x,y}]}, {x,-5,5},{y,-4,4},
AxesLabel -> {"x","y","z"},
AxesStyle -> Larger,
PlotRange -> {-10,20},
ClippingStyle -> None,
PlotStyle -> {{Green,Opacity[.85]},{Red, Opacity[.5]}}]

You can also change the number of lines that go across each surface with the Mesh command:

f[{x_,y_}] = 3x^2 - y^2 Sin[x];
Plot3D[f[{x,y}], {x,-5,5},{y,-4,4},
AxesLabel -> {"x","y","z"},
AxesStyle -> Larger,
PlotRange -> {-10,20},
ClippingStyle -> None,
Mesh ->3]

Notice there are three curves drawn across the shape.  Changing the 3 after the Mesh to 10 curves drawn across the shape:

f[{x_,y_}] = 3x^2 - y^2 Sin[x];
Plot3D[f[{x,y}], {x,-5,5},{y,-4,4},
AxesLabel -> {"x","y","z"},
AxesStyle -> Larger,
PlotRange -> {-10,20},
ClippingStyle -> None,
Mesh ->10]