Skip to main content

Section 3.5 Plotting Functions of Two Variables

Subsection 3.5.1 Functions on Rectangular Grids

Suppose you wish to plot a function \(f(x,y)\) on the rectangle \(a \le x \le b\) and \(c \le y \le d\text{.}\) The graph of a function of two variables is of course a three dimensional object. Visualizing the graph is often very useful.
For example, suppose you have a formula
\begin{equation*} f(x,y) = x \sin(xy) \end{equation*}
and you are interested in the function on the region \(0 \le x \le 5\text{,}\) \(\pi \le y \le 2 \pi\text{.}\) A way to plot this function in MATLAB would be the following sequence of commands:
>> f = @(x,y) x.*sin(x.*y)
>> [X,Y] = meshgrid(0:.1:5,pi:.01*pi:2*pi);
>> Z = f(X,Y)
>> mesh(X,Y,Z)
This will produce a 3-D plot that you can rotate by clicking on the rotate icon and then dragging with the mouse.Instead of the command mesh, you could use the command
>> surf(X,Y,Z)
The key command in this sequence is [X Y] = meshgrid(a:h:b,c:k:d), which produces matrices of x and y values in X and Y. Enter:
>> size(X)
>> size(Y)
>> size(Z)
to see that each of these variables is a \(101 \times 51\) matrix. To see the first few entries of X enter
>> X(1:6,1:6)
and to see the first few values of Y type
>> Y(1:6,1:6)
You should observe that the \(x\) values in X begin at \(0\) on the left column and increase from left to right. The \(y\) values on the other have start at \(\pi\) at the top and increase from top to bottom. Note that this arrangement is flipped from the usual arrangment in the \(x\)-\(y\) plane.
In the command [X Y] = meshgrid(a:h:b,c:k:d), \(h\) is the increment in the \(x\) direction and \(k\) is the increment in the \(y\) direction. Often we will calculate
\begin{equation*} h = \frac{b-a}{m}\quad \textrm{ and }\quad k = \frac{d-c}{n}\text{,} \end{equation*}
where \(m\) is the number of intervals in the \(x\) direction and \(n\) is the number of intervals in the \(y\) direction. To obtain a good plot it is best if \(m\) and \(n\) can be set between \(10\) and \(100\text{.}\)
A common way of visualizing a function of two variables is by a Contour Plot. In a contour plot we draw several level curves of the function, which are the curves at which the function is equal to a few values. A topographical map is an example of a contour plot. To produce a contour plot for the function \(f(x,y)\) as above, since we have input the function itself and created a meshgrid on which we want to plot it, we simply input:
>> contour(X,Y,Z,10)
The optional number ``10’’ specify how many contour curves to display.
For another example of meshgrid, contour and mesh, try the following and look at X and Y.
>> [X,Y] = meshgrid(0:.05:4,1:.02:2);
>> Z = (X+Y)./(1+X.^2+Y.^2);
>> contour(X,Y,Z,11)
>> mesh(X,Y,Z)

Subsection 3.5.2 Scattered Data and Triangulation

Often we are interested in objects whose bases are not rectangular. For instance, data does not usually come arranged in a nice rectangular grid; rather, measurements are taken where convenient.
In MATLAB we can produce triangles for a region by recording the coordinates of the vertices and recording which vertices belong to each triangle. The following script program produces such a set of triangles:
% mytriangles
% Program to produce a triangulation.
% V contains vertices, which are (x,y) pairs
V = [ 1/2 1/2  ;  1   1 ; 3/2 1/2 ;  .5  1.5 ;  0    0
      1    0  ;  2   0 ; 2    1  ; 1.5 1.5  ;  1    2
      0    2  ;  0    1]
% x, y are row vectors containing coordinates of vertices
x = V(:,1)';
y = V(:,2)';
%  Assign the triangles using Delaunay's algorithm
T = delaunay(x,y)
You can plot the triangles using
>> trimesh(T,x,y)
You can also prescribe values (heights) at each vertex directly (say from a survey):
>> z1 = [ 2 3 2.5 2 1 1 .5 1.5 1.6 1.7 .9 .5 ];
or using a function:
>> f = @(x,y) abs(sin(x.*y)).^(3/2);
>> z2 = f(x,y);
The resulting profiles can be plotted:
>> trimesh(T,x,y,z1)
>> trisurf(T,x,y,z2)
Each row of the matrix T corresponds to a triangle, so T(i,:) gives triangle number i. The three corners of triangle number i are at indices T(i,1), T(i,2), and T(i,3). So for example to get the \(y\)-coordinate of the second point of triangle number 5, enter
>> y(T(5,2))
To see other examples of regions defined by triangles, download mywedge.m (Program A.2.9) and mywasher.m (Program A.2.8) and run them. Each of these programs defines vectors x and y of \(x\) and \(y\) values of vertices and a matrix T. As before T is a list of sets of three integers. Each triple of integers indicates which vertices to connect in a triangle.
To plot a function, say \(f(x,y) = x^{2} - y^{2}\) on the washer figure try
>> mywasher
>> z = x.^2 - y.^2
>> trisurf(T,x,y,z)
Note again that this plot can be rotated using the icon and mouse.

Exercises 3.5.3 Exercises

1.

Plot the function \(f(x,y) = \sin(x) \ e^{-x^2 - y^2}\) on the rectangle \(-3 \le x \le 3\text{,}\) \(-2 \le y \le 2\) using meshgrid and mesh. Make an appropriate choice of \(m\) and \(n\) and if necessary a rotation to produce a good plot. Calculate the \(h\) and \(k\) corresponding to your \(m\) and \(n\text{.}\) Turn in your plot and the calculation of \(h\) and \(k\text{.}\)

2.

Modeling after mywasher.m (Program A.2.8), produce

A simple figure composed of 4 triangles.🔗
using integer coordinates for the vertices. Use the axis command to zoom out so the outside edges are clearly visible. Compute \(z = 3x +y^{2}\) and plot the graph. Turn in your program and the plots.