﻿ ]>

## Plotting Points in Matlab

Matlab has an extensive set of plotting tools. The best all-around two-dimensional plotting tool that we will concern ourselves with in this course is the `plot` command. You may want to investigate some of the other plotting tools that Matlab provides that we will not be discussing.

Since Matlab is a numeric computing tool, plotting is done using sets of numeric coordinates, otherwise known as points. If you wish to plot an expression, you must first calculate points using the expression and then plot the set of points you generated. Luckily, this is very easy to do with Matlab.

Suppose we want to just plot the sine function. Try entering this line:

`     y = sin(x); `

Note that you get an error message. In Matlab, you cannot use an unassigned variable on the right side of the equals sign. This is because the equal sign is the assignment operator and Matlab is trying to calculate the value of the right hand side and assign that value to the variable that is named on the left hand side. Since we haven't given `x` a value yet, it is not possible to calculate the right hand side of the assignment operator.

Before you can calculate the sine function, you need to determine what range you want to calculate it over. You can calculate the sine of a single point, but that won't be a very interesting plot. Instead, we will want many points very close together, over the desired range, to evaluate. In Matlab, the most convenient way to create a lot of points is to use the colon (`:`) operator to create a single dimension array, also known as a vector.

Let's create a vector that spans two wavelengths of a sine curve. As you know the wavelength of a sine curve is `2*pi`. Here is a good way of creating such a vector:

```     xx = 0 : 0.02 : 4*pi ;
```

Every entry in the final vector is `0.02` greater than the previous entry.

Now we find the sine of each of the values of this vector. The `sin` function works much the way it does in Maple. Enter the following:

`     yy = sin(xx); `

Since Matlab's trigonometry functions all work element-wise on arrays, this command returns a vector with the same number of values as in the `xx` vector. To plot the `yy` vs `xx`:

`plot( xx , yy ) `

Congratulations, you've plotted a graph! It isn't all that interesting though. Let's do some formatting to it, like make the line dashed and the color green. Enter this command:

`     plot( xx , yy , 'g--' ) `

The `g` makes the line green. The two dashes make the line appear a dashed line instead of solid.

To add a title at the top of your plot and label the x and y axis, add these commands after your `plot` command.

```     title('sin(x) vs x')
xlabel('x')
ylabel('sin(x)')```

### General Form of the plot Command

An xy plot with the `plot` command has the general form:

```     plot( x1 , y1 , 'x1y1_specifications' , x2 , y2 , 'x2y2_specifications' , ...
xn , yn , 'xnyn_specifications' )
title('title of the plot')
xlabel('what is measured along the x axis')
ylabel('what is measured along the y axis')```

where `x1` and `y1` are the coordinates of the first set of points to plot, `x2` and `y2` are the second set of coordinates, `...` indicates the continuation on to the next line, `xn` and `yn` are the final set of coordinates. Each set of coordinates may be scalars (single values) or vectors.

The specifications are enclosed in single quotes and they are used to specify the line type, the data markers, and the color, for each xy plot group. If you omit the specifications, the default values include a solid line and no data markers.