Mathematical Principles for Scientific Computing and Visualization  
http://www.farinhansford.com/books/scv
by Gerald Farin and Dianne Hansford

Bivariate functions

A bivariate function is a function of two (bi-) variables (-variate).

Let's look at the first function in this chapter -- f(x,y) = sin(x^2 + y^2).
The two variables are x and y.

In the math cell below, you can use the left-mouse button to rotate the 3D function.
(Sometimes these are called 2-1/2 D functions.)

Do you know how the function is plotted (displayed)?
Evaluation of a function means that we plug in an (x,y) pair and determine the function value there.
Mathematica has a default number of evaluations that it decides is needed to create a nice looking image. This number will vary depending on the complexity of the function.

Notice that when we change the domain over which the function is plotted, the plot looks very polygonal.
When you dynamically change what needs to be plotted, the only way for Mathematica to (re-)evaluate the surface fast enough is to evaluate fewer positions.

sinx2y2[x_, y_] := Sin[x^2 + y^2]

Manipulate[Plot3D[sinx2y2[x,y], {x, -1.5, xmax}, {y,-1.5,ymax}, AxesLabel->{"x","y","f(x,y)"},ImageSize->400, Mesh->None, PlotPoints->70], {{xmax, 3.14}, 0,6}, {{ymax, 3.14}, 0,6}]

Here is a simpler function -- that can help us understand bivariate function better.

Detect in the plot that the function is quadratic in x and linear in y.

Here we can easily see that Mathematica is plotting univariate functions: f(x,constant) and f(constant, y).
The program picks some number of values at which to plot these univariate functions that live on the bivariate function.
Can you identify f[x,0] and f[0,y]?

fct[x_, y_] := x^2 + y

Manipulate[Plot3D[fct[x,y], {x,-1, xmax}, {y,-1,ymax},PlotRange->{{-1,1},{-1,1},{-1,3}},ImageSize->400, AxesLabel->{"x","y","f(x,y)"}], {{xmax,1}, 0,1}, {{ymax,1}, 0,1}]

A plane can be defined as plane(x,y) = ax + by + c

This is a plane thru the origin with largest slope in the direction (a,b).

Example: plane(x,y) = x+y has largest slope in (1,1)
Example: plane(x,y) = x has largest slope in (1,0)

a = 1;
b = 0;
c = 0;

plane[x_,y_] := a*x + b*y + c

Manipulate[Plot3D[plane[x,y], {x,0, xmax}, {y,0,ymax},PlotRange->{{0,10},{0,10},{0,plane[10,10]}}, ImageSize->400], {{xmax,10}, 1,10}, {{ymax,10}, 1,10}]

Tangent Plane

The tangent plane to a bivariate function f(x,y) at a particular (x,y) is analagous to a tangent to a univariate function at a particular x-value.

Let's say we want the tangent plane at (xhat, yhat). To find the tangent plane there, we need the concept of a partial derivative of a bivariate function.  df(x,yhat)/dx will give us the derivative of the function in x that passes thru yhat. df(xhat, y)/dy will give us the derivative of the function in y that passes thru xhat. (See the chapter for the correct font/notation.)

As we know from univariate functions, the derivative (slope) at a given x-value defines a tangent line there. Thus we use these slopes in the x- and y-directions to define the tangent plane.

tangent plane at xhat, yhat : 
tangent(x,y) = f(xhat,yhat) + fx(xhat,yhat)(x - xhat) + fy(xhat,yhat)(y - yhat)
where fx and fy are the partial derivatives in x and y, respectively.
See how similar this is to the tangent to a univariate function!

Clear[xp,yp,myfct,myfctdx,myfctdy, xmin,xmax,ymin,ymax];

(* Extents for function domain *)
xmin = -2;
xmax = 2;
ymin = -2;
ymax = 2;

(* Define function and its partials *)
myfct[x_, y_]= -x^3 + y^2 + x; 
(* myfct[x_, y_]= 2x^3 - y^2 + 2x - y; *)
myfctdx[x_, y_]= D[myfct[x,y], x];
myfctdy[x_, y_]= D[myfct[x,y], y];

tangentplane[x_, y_, xp_,yp_] := myfct[xp, yp] + myfctdx[xp,yp]*(x - xp) +myfctdy[yp, yp]*(y- yp)


Manipulate[Plot3D[{myfct[x,y], tangentplane[x,y,xpoint,ypoint]}, {x,xmin,xmax},{y,ymin,ymax}, PlotStyle->{Directive[Yellow,Specularity[White,200],Opacity[1.0]],  Directive[Red,Specularity[White,20],Opacity[0.3]]},Mesh->None,PlotRange->Full, AxesLabel->{"x","y","f(x,y)"}, ImageSize->400], {{xpoint,0},xmin,xmax}, {{ypoint,0},ymin,ymax}]

Gradient

The tangent plane at (xp,yp) has the greatest slope in the direction v = (fx(xp,yp), fy(xp,yp)).
v is a vector that indicates a direction and speed. v is called the gradient.

If we plot the gradient for the function at some refinement of points on a regular grid, it is called a gradient field. The vectors are plotted as arrows. The lengths of the arrows indicate the speed -- greater slope will require a longer arrow.

Gradient fields are helpful for finding the shortest path to a maximum or minimum of a function -- e.g., given today's conditions, what is the fastest way to maximum profits!

Needs["VectorFieldPlots`"]
Clear[f];

(* Here is a simple function *)
(* Zero slope in the direction of the gradient results in a very short vector. *)
Plot3D[x^2, {x, 0, 2}, {y, 0, 2}]
GradientFieldPlot[x^2, {x, 0, 2}, {y, 0, 2}]

(* This is a little more complicated. *)
Plot3D[Sin[x y], {x, 0, 2}, {y, 0, 2}]
GradientFieldPlot[Sin[x y], {x, 0, 2}, {y, 0, 2}]

Plot3D[Cos[x ] + Cos[y], {x, 0, 4Pi}, {y, 0, 4Pi}]
GradientFieldPlot[Cos[x ] + Cos[y], {x, 0,4Pi}, {y, 0, 4Pi}]

Contours

A contour of a bivariate function is the set of points where f(x,y) = constant.
For example, we might want to know where the function is zero: f(x,y) = 0.
As the default setting, Mathematica creates contours for several different "constant" values. Color shades are used to accent the regions in between the "constant" value contour curves. These colors tell us that all values of the function in this region are bounded by the contour around it.

Plot3D[Cos[x ] + Cos[y], {x, 0, 4Pi}, {y, 0, 4Pi}]
ContourPlot[Cos[x]+Cos[y],{x,0,4Pi},{y,0,4Pi}]

Plot3D[y^2-x^2,{x,-4,4},{y,-4,4},MeshFunctions->{#3&},Mesh->{{0}},BoxRatios->{1,1,1}]
ContourPlot[y^2-x^2,{x,-4,4},{y,-4,4}]

Here is another application of the contour function -- finding the intersection of two functions.

Plot3D[{x^2+y^2,x+y -0.2}, {x, -2,2}, {y,-2,2}]
ContourPlot[x^2+y^2 == x+y -0.2, {x, -2,2}, {y,-2,2}]

Let's sneak in a trivariate function ... that is a function of three variables. So for each position in (x,y,z) space we have an associated function value -- for instance pressure or temperature.x^2, y^2

f(x,y,z) = x^2 + y^2 + z^2

Can you make sense of what you see here?!

ContourPlot3D[x^2 + y^2 + z^2, {x, -2,2}, {y, -2,2}, {z, -2,2}]

(SetDirectory[ToFileName[Extract["FileName"/.NotebookInformation[EvaluationNotebook[]],{1},FrontEnd`FileName]]];) 

Plot3D[x^ + y^2, {x, 0, 2}, {y, 0, 2}]
gradpic = GradientFieldPlot[x^2 + y^2, {x, 0, 2}, {y, 0, 2}]
Export["gradientfield1.eps", gradpic]

(SetDirectory[ToFileName[Extract["FileName"/.NotebookInformation[EvaluationNotebook[]],{1},FrontEnd`FileName]]];) 

Plot3D[x^2, {x, 0, 2}, {y, 0,2}]
c1 = ContourPlot[x^2,{x,0,2},{y,0,2}]
Export["contour.jpeg", c1]