Generating MATLAB Graphcis

MATLAB has excellent capability to produce planar plots and 3-D mesh surface plots. To see some of these capabilities, enter the following command from a MATLAB prompt:

 
      expomap

Graphics are supported on workstations such as the Suns, and on PCs with the appropriate graphics hardware and software, but please note that not all terminals support graphics. If you experience difficulty in using the MATLAB plotting capabilities, contact the ITS Consultants in VCC 209 or CII 3111, phone x6393, or send e-mail to consult@rpi.edu.

The plot command creates linear x-y plots; if x and y are vectors of the same length, the command

 
      plot(x,y)

opens a graph window and draws an x-y plot of the elements of x versus the elements of y. You can, for example, draw the graph of the sine function over the interval -4 to 4 with the following commands:

      x = -4: . 01 :4;
      y = sin(x);
      plot (x, y)

The vector x is a partition of the domain with mesh size 0.01, while y is a vector giving the values of sine at the nodes of this partition (recall that sin operates entrywise - see section on "Scalar Functions").

When in the graph screen, pressing any key will return you to the command screen while the command

 
      shg (show graph)

will then return you to the current graph screen. When creating graphics on, for example, a Sun, you may wish to keep the graph window exposed, but moved to the side, and the command window active.

As a second example, you can draw the graph of y = e^(-x^2) over the interval -1.5 to 1.5 as follows:

      
      x = -1.5:.01:1.5;
      y = exp ( -x.^ 2);
      plot (x, y)

Note that one must precede the carat ( ^ ) by a period to ensure that it operates entrywise (see section on "Matrix Operations".)

Refer to the MATLAB User's Guide or the help facility for such features as multiple plots, titles, labels, gridlines, and manual scaling.

A hardcopy of the graph window can be obtained with the MATLAB command print:

      print

Three dimensional mesh surface plots are drawn with the function mesh. The command

      mesh(z)

creates a three-dimensional perspective plot of the elements of the matrix z. The mesh surface is defined by the z-coordinates of points above a rectangular grid in the x-y plane. Try

 
      mesh(eye(10))

To draw the graph of a function z = f(x, y) over a rectangle, first define vectors xx and yy, which give partitions of the sides of the rectangle. Next, with the function meshdom (mesh domain) one create a matrix x, each row of which equals xx and whose column length is the length of yy, and similarly a matrix y, each column of which equals yy, as follows:

       [x,y] = meshdom(xx,yy);

Then compute a matrix z, obtained by evaluating f entrywise over the matrices x and y, to which mesh can be applied.

You can, for example, draw the graph of z = e^(-(x^2)-(y^2)) over the square [-2, 2] X [-2, 2] as follows:

      xx = -2:0.1:2;
      yy = xx;
      [x,y] = meshdom(xx,yy);
      z = exp(-x.^2 - y.^2);
      mesh(z)

One could, of course, replace the first three lines of the preceding with

      [x,y] = meshdom(-2:0.1:2, -2:0.1:2);

Refer to the MATLAB User's Guide for further information on working with meshes in MATLAB.


Other Matlab Hints

Click on an item listed below to access additional MATLAB-related hints.


Return to ACS home page.


Please send comments and suggestions to

consult@rpi.edu