Plotting Surfaces in MATLAB


MATLAB can be found on any of the cluster machines.  You can use it to generate plots of the surfaces we were looking at in class.  (More information can be obtained by typing

>> helpdesk
in the MATLAB command window and looking at the information on graphics.)

Example:  Plot the graph of f(x,y) = x^2 - 2y^2.

We will plot over the rectangle where x runs from -5 to 5 and y runs from -3 to 3.  First we need to set up a grid of points.  It takes a few commands to do this:

>> x = -5:0.5:5;
>> y = -3:0.25:3;
>> [ x_grid, y_grid ] = meshgrid(x,y);
The first MATLAB command above makes a list of x values running from -5 to 5 in steps of length 0.5.  The second command makes a list of y-values from -3 to 3 in steps of length 0.25.  The third step makes a grid of points on the rectangle where x varies from -5 to 5 and y varies from -3 to 3.  We need now to compute the z-values for this rectangular grid of points.
(Note:  if you type the above commands without semicolons at the end, MATLAB will display the results of the command.)
>> z = x_grid.^2 - 2*y_grid.^2;


A technical note:
The symbol .^ is needed intead of just ^ to square the list of numbers we stored as x_grid and y_grid.   If you have a matrix  X  in MATLAB and you want to SQUARE EACH ENTRY OF THE MATRIX, then the command you need is X.^2.  Without the dot before ^, the command would means something else (matrix power instead of powers of each individual matrix entry.)   Similarly if you want to take the product xy for all the x-values and y-values in your grid you must use
>> x_grid .* y_grid
If you type
>> x_grid * y_grid
without the dot before the asterisk then it means to multiply the matrix x_grid by the matrix y_grid instead of multiplying corresponding entries.


Back to plotting:

Now we are ready to plot:

>> mesh(x_grid, y_grid, z);
This produces an open mesh plot of this saddle-shaped surface.  You can also try
>> surf(x_grid, y_grid, z);
To understand the surface, it helps to be able to view it from different angles.  You can do this with the mouse.  First you have to click on the icon that looks like a dotted circle with an arrow pointing counterclockwise in the panel at the top of your plot window.  Then if you click on the plot and drag with the mouse you can turn the surface and view it from different angles.  You can also add various options to your plotting commands --- for more information about how to do this consult the help desk feature of MATLAB or type
>> help surf
>> help mesh
Now suppose you want to plot more than one surface at a time.  For example if we want to see how the plane z = x + 2y intersects this surface we can do the following sequence:
>> mesh(x_grid, y_grid, z);
>> hold on
>> surf(x_grid, y_grid, x_grid + 2*y_grid);
>> hold off
Here the "hold on" command tells MATLAB to keep add the next plot to the previous one.   When you have all the pieces you want just type "hold off".

In class we also talked about plotting level curves for a function.  We can do this in MATLAB using commands like

>> contour(x_grid, y_grid, z);
>> contour3(x_grid,y_grid, z);
Example:  In class we computed partial derivatives for the function z = sin(xy^2).   Make a plot of this surface using MATLAB.

Because this surface bends a lot more than our first example we need to plot with higher resolution.  I will restrict to a smaller region than before to get a better plot.

>> x = -2:0.1:2;
>> y = -2:0.1:2;
>>  [x_grid,y_grid] = meshgrid(x,y);
>>  z = sin(x_grid.*y_grid.^2);
>>  surf(x_grid,y_grid,z)
As an exercise replot on the rectangle -3 <= x <= 3 and -3 <= y <= 3.