## Plotting

### 2D Plotting

Plotting, like everything else in Matlab, is done with vectors. Two n-dimensional vectors represent the x- and y-coordinates, respectively. For example, the following plots a line connecting the points (1,2), (2,4), (3,6), (4,8), (5,10), and (6,12):

```>> plot( [1 2 3 4 5 6], [2 4 6 8 10 12] )
```

In order to plot a function, say on the interval [0, 10], the easiest way to do this is to:

```>> x = linspace( 0, 10, 100 );   % 100 points evenly spaced between 0 and 10
>> y = sin(x);                   % sin of each of these points
>> plot( x, y )                  % a plot of sin(x) on [0, 10]
```

If you now create a second plot, it will overwrite the first plot:

```>> plot( x, cos( x ) )           % a plot of cos(x) on [0, 10]
```

In order to see both plots, you must either plot them both at the same time, as in:

```>> plot( x, sin( x ), x, cos( x ) )     % a plot of both sin and cos
```

or, you may use the command hold:

```>> hold on
>> plot( x, x.^2 / 100 )      % a plot of the function f(x) = x^2/100
>> hold off                   % turn holding back off
```

### Plotting Options

Up to now, each set of points is plotted as blue lines. Each plot can be given its own distictive look by modifying whether the points are plotted as (1) a line, as points, or both, (2) what type of line or point to use, and (3) the colour used.

These options are specified using a 3rd argument which is a string containing letters or symbols. The colour can be changed using one of the following letters:

 'r' red 'g' green 'b' blue 'c' cyan 'm' magenta 'y' yellow 'w' white 'k' black

When plotting a line, the following can be used to change the appearence of the line:

 '-' solid ':' dotted '-.' dash dotted '--' dashed

In order to plot points rather than lines, you may use any of the following symbols:

 '.' point 'o' circle 'x' an x '+' plus '*' star 's' square 'd' diamond 'v' triangle (down) '^' triangle (up) '<' triangle (left) '>' triangle (right) 'p' pentagram 'h' hexagram

Let's say you wish to plot a set of points with red dashed lines. You would use the string 'r--' as a third argument to plot. If you wanted to plot data as magenta lines with squares indicating the points, use 'ms-'. Using only 'md' would plot only magenta diamonds without lines.

```>> x = linspace( 0, 3, 10 );
>> y = x.^2;
>> plot( x, y, 'r--' )
>> plot( x, y, 'ms-' )
>> plot( x, y, 'md' )
```

### 3D Plotting

When plotting 3D points, you can use the plot3 function which takes as its arguments 3 vectors representing the x, y and z coordinates.

The Matlab command:

```>> plot3( [1 5 3 4], [2 9 7 6], [11 0 10 9] )
```

plots a line connecting the four points (1, 2, 11), (5, 9, 0), (3, 7, 10), (4, 6, 9).

As with the plot command, you can add options to specify how the points should be plotted. The following example plots the four points as magenta crosses:

```>> plot3( [1 5 3 4], [2 9 7 6], [11 0 10 9], 'm+' )
```

Be sure to make note of the rotate arrow to the right of the zoom in and zoom out magnifying glasses. By clicking on this button, you can rotate the surface to see it from more than one direction.

### Surface Plotting

Given a matrix A, the Matlab command:

```>> surf( A )
```

plots the points of A on a grid of the appropriate size. For example, the following plots a symmetric matrix with larger points near the origin of the matrix:

```>> A = [15  3 2 1
3 10 4 1
2  4 7 0
1  1 0 2];
>> surf( A )
```

Colour, line style and point options are not available for the surf function.

Let's say we're plotting a function sin(x*y) with x ranging from 0 to 4 and y ranging from 3 to 7.

First, create vectors with a sufficient number of points:

```>> x = linspace( 0, 4, 100 );
>> y = linspace( 3, 7, 100 );
```

The simplest way to do this is to create a meshgrid:

```>> [X Y] = meshgrid( x, y );
```

This assigns X and Y two matrices, the first with the x values copied down the columns, and the second with the y values copied across the rows.

At this point, you can do:

```>> mesh( x, y, sin( X .* Y ) )
```

Similarly, if you wanted to plot x^2*y - 5*x^2 - 3*x*y + 15*x + 2*y - 9 on the same ranges, you could do:

```>> mesh( x, y, X.^2.*Y - 5*X.^2 - 3*X.*Y + 15*X + 2*Y - 9 );
```

This is a good example to see how Matlab uses colour to show the height of the function. As you can see here, a rainbow of colours from blue to red is used, blue for lwo points, red for high, and the intermediate rainbow colours inbetween.

Again, remember to click on the rotation button to rotate the plot using the mouse.

To look more closely at what is happening, consider plotting exp(x+y) [-3, 1] × [-2, 2] at the integers:

```>> x = -3:1

x =

-3    -2    -1     0     1

>> y = -2:2

y =

-3    -2    -1     0     1

>> [X Y] = meshgrid( x, y )

X =

-3    -2    -1     0     1
-3    -2    -1     0     1
-3    -2    -1     0     1
-3    -2    -1     0     1
-3    -2    -1     0     1

Y =

-2    -2    -2    -2    -2
-1    -1    -1    -1    -1
0     0     0     0     0
1     1     1     1     1
2     2     2     2     2

>> XpY = X + Y

XpY =

-5    -4    -3    -2    -1
-4    -3    -2    -1     0
-3    -2    -1     0     1
-2    -1     0     1     2
-1     0     1     2     3

>> expXpY = exp( XpY )

expXpY =

0.0067    0.0183    0.0498    0.1353    0.3679
0.0183    0.0498    0.1353    0.3679    1.0000
0.0498    0.1353    0.3679    1.0000    2.7183
0.1353    0.3679    1.0000    2.7183    7.3891
0.3679    1.0000    2.7183    7.3891   20.0855

>> mesh( x, y, expXpY )
```