User:Eml4500.f08.FEABBQ.koby/HW1

Graphics
Matlab is capable of producing a variety of mathematical graphs and figures in 2 and 3 dimensions. The basic commands to use these functions are:

Planar 2d Plots

The plot(x,y) function will create a linear graph if x and y are both 1d vectors of the same length. The first variable in the plot function is the horizontal axis, and the second is graphed on the vertical. The easiest way of producing a 1d vector is using colon notation:



The following is a graph of a sin wave from -4 to 4. Adding shown in the code above is the syntax for adding titles and axis labels using the title and xlabel/ylabel commands respectively.

Streamlined code exists for graphing functions as well. First, an M-file must be created such as the following:



Using this code would now create a 2d graph from -1.5 to 1.5:



2d parametric graphs can also be created in Matlab. The following code and Figure 3 demonstrate:

Annotating Graphs

Graphs are easily annotated in Matlab as well. Typing in the following commands will label the active figure. The format is like:

Other visual features are available as well:

All of these visual modifications should be performed after the plot command.

Multiple Graphs on the Same Figure

There are different ways to place multiple graphs onto the same Matlab figure. The first is to overload the  function. The following code and Figure 4 show how this can be done:

The  command can also be used. When hold is on, every plot graphed will go onto the active figure. Turning hold off will create a new figure for each new graph, which is the Matlab default.

Using Different Colors and Line Types on Graphs

Sometimes the default blue solid line in Matlab graphs can get a little boring. To add a little variety to your graphs, there are several commands that can be incorporated into the  function:



These commands are used inside single quotation marks inside the  function. would plot a dashed red line and  would plot a green dotted line.

Other Types of Graphs

There are several other types of graphs which Matlab can produce. Although this guide does not have the details for them, they can all be easily found using the  command.


 * Polar
 * Bar
 * Hist
 * Quiver
 * Compass
 * Feather
 * Rose
 * Stairs
 * Fill
 * Subplot

Graphics Hardcopy

Oftentimes it is necessary to print a graph from Matlab. This is most easily done with the convenient 'Print' menu option on the Figure. It is normally easier though to use 'Save as' and save the graph as a .jpg or other type of graphics file. For the more adventurous, Matlab has a  command which will print the active graphics item. The  option can also be used to append a different saved file to the current graphics item for printing. Just use the format.

3-D Line Plots

There are few more impressive ways to show off Matlab skills than producing colorful 3-D plots. The  command can easily plot in 3 dimensions, just like   plots in 2 dimensions. The following example shows how a 3-D plot (Figure 6) can be parametrically created:



All of the settings applicable to 2-D plots (including axis labels and commands, titles, colors, etc) can be used on a 3-D plot as well.

3-D Mesh and Surface Plots

The mesh and surf functions can create 3 dimensional representations of matrices, either with a wire mesh or solid surface. If a single matrix can be inputed into either function, the z value is the matrix value at point (x,y) (row, col). Figures 7 and 8 show the mesh and surf functions being used to graph the 'eye' (identity) matrix:



3-D functions can also be graphed using mesh and surf in conjunction with the  function. Combined with colon notation,  creates an x-y rectangle in which a function is graphed. The following code and Figure 9 show how a 3-D function can be defined and graphed:



There are several ways to change the appearance of a 3-D graph in Matlab. First of all, the  command will affect the superimposed mesh lines on a surface graph. Various color profiles can also be experimented with. The following commands can be used like this:

Any of the functions to change the appearance should be used after the graph has been made.

Graphs can also be produced parametrically with M-files. Check the guide for further information on this, and Matlab's 'Handle Graphics' which can produce advanced graphs and plots.