e-Mathematics > Matrix Algebra

Plotting

The plot function produces two-dimensional plots. Many different combinations of arguments are possible. The simplest form is
plot (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. For more options and overall information, type
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 on
turns the hold state on. And the command “hold off” turns the hold state off. Or, you can clear the current figure by typing
clf
While 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 $ x$-axis, and lower, upper limit y_lower, y_upper for $ y$-axis. The command “grid on” is also useful, displaying the grid on the plot (and “grid off” removes the grid off the plot). Other useful commands for plotting are as follows:


© TTU Mathematics