MATLAB Lesson 10 - Surface plots

Surface plots are an alternative to contour plots when visualising functions of two variables.

Surface plots

Surface plots of z = f(x, y) are produced by the MATLAB function surf. By default the height z determines the colour. As with contour plots, you must first calculate a rectangular array of function values.

Create a surface plot of the function f(x,y) = x3 - 3 x + y2, as used for the contour plots in the previous module. Try both a coarse 7 x 9 grid and a finer 61 by 81 grid.

Define the vector of 7 grid points for x in the interval [-3, 3]

Define the vector of 9 grid points for y in the interval [-4, 4]

Use the meshgrid function to create the 7 by 9 arrays X and Y.

Evaluate the function at each point in X and Y.

Create a surface plot.

>>  x = linspace(-3, 3, 7);
>>  y = linspace(-4, 4, 9);
>>  [X, Y] = meshgrid(x, y);
>>  F = X.^3 - 3*X + Y.^2 ;
>>  surf(x, y, F);

Surface plot, coarse grid

A finer 61 by 81 produces a much clearer image of the function, as below.

Surface plot, finer grid

The MATLAB M-file used to create this plot is plt3d.m.

Interpolated shading

The plots above have grid lines which can obscure feature of the function when finer grids are used. This can be overcome using the MATLAB command shading interp which removes the grid lines and changes the way in which the colour values are calculated, giving the figure below.

Surface plot, finer grid, interpolated shading

 

Mesh plots

A mesh plot which only has a grid of mesh lines can be created with the mesh command.

Using the data for x, y, and F created above, create a mesh plot of the function f.

Create a mesh plot of the rectangular array of data F with axes determined by the vectors x and y.

>>  mesh(x, y, F);




mesh plot

The figure window

MATLAB's figure window give you access to a number of tools to help explore and customise a figure.

Figure window with mesh plot

In the row of icons under the menu items the following tools are particularly useful.

First (left) click on the icon. For the zoom tools, then left click in the figure window (what does right clicking do?). For the Rotate 3D tool, hold the (left) mouse button down and move the mouse to rotate the image.

The zoom tools are also useful for 2D plots.

 

Warnings

 

Self-test Exercise

What do the following commands produce?
  • surfc(x, y, F)
  • colorbar
  • axis off

Answer:

A surface plot over a contour plot.

A colorbar (colour scale), by default to the right of the plot.

Makes the axes invisible.


Use the mouse to select the text between the word "Answer" and here to see the answer.

Summary

A 3-D plot of the surface z = f(x,y) using a surface or mesh plot is a good way to see the properties (minima, maxima, smoothness) of a function of two variables.