Plotting
The plot function produces two-dimensional plots. Many different combinations of arguments are possible. The simplest form isplot (y)where the argument y is taken as the set of y coordinates and the x coordinates are taken to be the indices of the elements, starting with 1. If both x and y are vectors, the elements of y are plotted versus the elements of x. For example,
> z = -3:0.2:5; > w = 2*z.^3 - 3*z.^2 - 5*z + 1; > plot(z,w)plots the above cubic polynomial curve on the xy-plane.
For Matlab users: It has many built-in functions to deal with polynomials. For example, in order to get the values for a polynomial function we could use their polyval() in the following way.
> z = -3:0.2:5; > w = polyval([2 -3 -5 1], z);
Plotting options. If more than one argument is given, they are interpreted as
plot (x, y, 'r-')where y and 'r-' are optional. The format argument 'r-', if present, is interpreted as follows.
- '-' sets solid line plot style (default).
- '--' sets dashed line plot style.
- '.' sets dots plot style.
- If it is one of 'r', 'g', 'b', 'm', 'c', or 'w', it is interpreted as the plot color (red, green, blue, magenta, cyan, or white).
- If it is one of '+', '*', 'o', 'x', it will set the point style.
help plot
It can “hold” the current data on the plot when executing subsequent plotting commands. This allows us to execute a series of plot commands and have all the lines end up on the same figure. The default is for each new plot command to clear the plot device first. The command
hold onturns the hold state on. And the command “hold off” turns the hold state off. Or, you can clear the current figure by typing
clfWhile you “hold on” the current graph, you can specify the axis limits via
axis([x_lower,x_upper])or
axis([x_lower,x_upper,y_lower,y_upper])with lower, upper limit
x_lower
, x_upper
for 
y_lower
, y_upper
for 
- title('Title of the plot') creates a title at the top of graph.
- xlabel('x axis') and ylabel('y axis') specify x and y axis labels for the plot.
- legend('1st plot', '2nd plot', ..., pos)
puts a legend on the current plot using
'1st plot', '2nd plot',...
as labels for all the plots before calling legend.
The value pos optionally places the legend in the specified location:
- pos = -1: To the top right of the plot
- pos = 0: Don't move the legend box (default)
- pos = 1: Upper right-hand corner
- pos = 2: Upper left-hand corner
- pos = 3: Lower left-hand corner
- pos = 4: Lower right-hand corner
© TTU Mathematics