Graph-it Help

Back to graphing page.

Many thanks to MathWorks, Inc. for making this all possible.

Table of Contents

Basic Matlab Tutorial

Entering equations in Matlab is very similar to how you'd enter them into, say, a graphing calculator. If x was a variable, to multiply it by 2 you'd write: 2*x. To take the sine, sin(x). To store the value of sin(x) in variable z (and you don't have to declare variables ahead of time like you'd do in C or Pascal), you'd type z = sin(x) . Division is / .

Everything in Matlab is a matrix. A constant is a 1 row by 1 column matrix, and a vector either 1 by n (the default in this setting) or n by 1. Later, when things are referred to as matrices, it is meant that each of the row and column dimensions is greater than 1.

Because of this dimensionality, you have to be careful to obey the laws of matrix computation. Suppose t and s are vectors, both 1 by n (where n > 1). You can't just multiply t by s by doing t*s. Why? Because in matrix multiplication, the number of columns of the first must match the number of rows of the second (and the result has the number of rows of the first and the number of columns of the second). t*s would be a (1 by n) * (1 by n), and n is not equal to 1. However, if you wanted to multiply each element of t by its corresponding element in s, the period operator can be utilized before the operator. t.*s would result in a 1 by n vector, with the product of every similary indexed t,s pair.

The period operator is oft-utilized in Matlab. It must be used before the power operator, ^, for example, if a vector is involved. Thus, to square vector t, you'd type t.^2 .

An example of period multiplication: x = [1 2 3 4], y = [5 6 7 8], x.*y gives [5 12 21 32]

Operations such as trigonometric functions, multiplying by constants, or adding constants, work on each matrix element individually. Thus 2*x (of the above x) gives [2 4 6 8], sin(x) gives [0.8415 0.9093 0.1411 -0.7568], and x + 2 gives [3 4 5 6] . Note that the period operator was not needed, since vector operations were not being used.

The matrix transpose operator is a quote: '. Transposing a matrix turns an m by n matrix into an n by m one. Note that for two 1 by n vectors t and s, t'*s returns an n by n matrix, while t*s' returns a single value (1 by 1). (The period operator is not being used here since real matrix multiplication is being done.)

Need to create a new vector of data? You choose the starting point, step value, and the ending point, and Matlab does it for you. x = [0:.1:1] will give a 1 by 11 vector of values between 0 and 1, each separated by .1 . Note that x = [0:.1:1]' will do the same thing, except that it will be an 11 by 1 vector. If you try x = [10:10:1], it returns the empty matrix. Which makes sense, because you're saying add 10 to 10 until you get to 1, which will never happen. Notice also that [0:3:8] will return [0 3 6]; the 8 is not in the vector because the step value went over it without equalling it.

Several of the plots use complex data. To create a complex number, multiply it by i or j (both serve as the square root of -1). I.e. one way to create a vector with real and complex parts is an equation like z = x + y*i .

Finally, here are some of the most important Matlab functions and constants that you may need:


1-Parameter Plots

Back to Linear Graphing

Linear 2-D Plot

Accepted forms: plot(y), plot(x,y) A line color and style may be chosen for this plot.

Example:
t goes from -pi to pi with 1000 data points
x = t
y = tan(sin(t) - sin(tan(t))


Log-log scale plot

Accepted forms: loglog(y), loglog(x,y)

Loglog(...) is the same as plot(...) except logarithmic scales are used for both the x- and y-axes (log-log) scales. See standard 2-D plots for more information.


Logarithmic x-axis scale plot

Accepted forms: semilogx(y), semilogx(x,y)

Semilogx(...) is the same as plot(...) except logarithmic scales are used for the the x-axis and a linear scale for the y-axis. See standard 2-D plots for more information.


Logarithmic y-axis scale plot

Accepted forms: semilogy(y), semilogxy(x,y)

Semilogy(...) is the same as plot(...) except logarithmic scales are used for the the y-axis and a linear scale for the x-axis. See standard 2-D plots for more information.


Polar coordinate plot

Accepted forms: polar(t,y) A line color and style may be chosen for this plot.

Example:
t goes from 0 to 2*pi with 500 data points
y = sin(2*t).*cos(2*t)


Bar graph

Accepted forms: bar(y), bar(x,y) A line color and style may be chosen for this plot.

Example:
t goes from -2.9 to 2.9 with 30 data points
x = t
y = exp(-x.*x)


Stairstep plot

Accepted forms: stairs(y), stairs(x,y) A linestyle may not be chosen for this plot. It is suggested that the background be turned to black.

Example:
t goes from 0 to 10 with 40 data points
x = t
y = sin(x)


Histogram plot

Accepted forms: hist(y), hist(y,x) A linestyle may not be chosen for this plot. It is suggested that the background be turned to black.

Example:
t goes from -2.9 to 2.9 with 60 data points
x = t
y = randn(10000,1)


Stem plot for discrete sequence data

Accepted forms: stem(y), stem(x,y) A line color and style may be chosen for this plot.

Example:
y = randn(50,1) % ignore t in this case


Errorbar plot

Accepted forms: errorbar(x,y,z) A linestyle may not be chosen for this plot. It is suggested that the background be turned to black.

Example:
t goes from 0 to 10 with 50 data points
x = t
y = sin(t)
z = erf(x) % erf, the error function, is the integral of the Gaussian distribution function from 0 to x


Compass plot

Accepted forms: compass(y), compass(x,y) A line color and style may be chosen for this plot.

Example 1:
t goes from -10 to 10 with 50 data points
x = t
y = sin(cos(t))

Example 2:
y = eig(randn(20,20)) % ignore t, eig returns the eigenvalues of the matrix


Feather plot

Accepted forms: feather(y), feather(x,y) A line color and style may be chosen for this plot.

Example:
t goes from 0 to 2*pi with 50 data points
x = t
y = 10*randn(1,50)


Angle Histogram

Accepted forms: rose(t,y) A linestyle may not be chosen for this plot. It is suggested that the background be turned to black.

Example:
t goes from -pi to pi with 50 data points
y = log(cos(exp(t.^(-2))))


Filled 2-D polygons

Accepted forms: fill(x,y) A line color and shading may be chosen for this plot.

Example:
t goes from 0 to 2*pi with 8 data points
x = sin(t)
y = cos(t)


Plot points and lines in 3-D space

Accepted forms: plot3(x,y,z) A line color and style may be chosen for this plot.

Example:
t goes from 0 to 2*pi with 50 data points
x = sin(t)
y = cos(t)
z = t


Filled 3-D polygons in 3-space

Accepted forms: fill3(x,y,z) A line color and shading may be chosen for this plot.

Example:
t goes from 0 to 2*pi with 50 data points
x = sin(t)
y = cos(t)
z = tan(t)


2-Parameter Plots

Back to Surface Graphing.

There are a few items that need additional discussion here:


Contour plots

Accepted forms: contour(z), contour(x,y,z)

Example:
To view a contour plot of the function z = x*exp(-x.^2 - y.^2) with parameters as indicated:
t goes from -2 to 2 with 20 points
s goes from -2 to 3 with 25 points
x = t
y = s
choose yes on meshgrid
z = x.*exp(-x.^2 - y.^2) % note that meshgrid will change x and y to matrices after the meshgrid; this will not be a problem, contour will just use the first row it can find


3-D contour plots

Accepted forms: contour3(z), contour3(x,y,z)

Example:
t goes from -3 to 3 with 50 points % s will not be used
x = t
y = t
choose yes on meshgrid
z = peaks(x,y) % peaks is a built-in function that produces very interesting matrices for plotting


Pseudocolor

Accepted forms: pcolor(z), pcolor(x,y,z) A colormap and shading may be chosen for this plot.

Example:
t goes from 0 to 6 with 7 data points
s goes from -6 to 6 with 13 data points
place in optional area 1: r = t/n
place in optional area 2: theta = pi*s/n
x = r'*cos(theta) % produces a matrix 7 by 13
y = r'*sin(theta)
choose no on meshgrid
z = r'*cos(2*theta)
choose axis square


3-D mesh surface plot

Accepted forms: mesh(z), mesh(z,c), mesh(x,y,z), mesh(x,y,z,c) A colormap and shading may be chosen for this plot.

Example:
t goes from -7.5 to 7.5 with 30 data points % s is not used
x = t
y = t
choose yes on meshgrid
place in optional area 3: r = sqrt(x.^2 + y.^2) + eps % eps is used to prevent division by zero.
z = sin(r)./r


3-D mesh surface plot with contours

Accepted forms: meshc(z), meshc(z,c), meshc(x,y,z), meshc(x,y,z,c)

Meshc(...) is the same as mesh(...) except that a contour plot is drawn beneath the mesh. See mesh plots for more information.


3-D mesh surface plot with curtains

Accepted forms: meshz(z), meshz(z,c), meshz(x,y,z), meshz(x,y,z,c)

Meshz(...) is the same as mesh(...) except that a curtain plot, or reference plane, is drawn beneath the mesh. See mesh plots for more information.


3-D waterfall surface plot

Accepted forms: waterfall(z), waterfall(z,c), waterfall(x,y,z), waterfall(x,y,z,c)

Waterfall(...) is the same as mesh(...) except that it omits the column lines of the plot to produce a "waterfall" effect. See mesh plots for more information.


3-D shaded surface plot

Accepted forms: surf(z), surf(z,c), surf(x,y,z), surf(x,y,z,c)

Surf(...) is the same as mesh(...) except that whereas the other draws grid lines, this draws the actual colored parametric surface. See mesh plots for more information.


3-D shaded surface plot with contours

Accepted forms: surfc(z), surfc(z,c), surfc(x,y,z), surfc(x,y,z,c)

Surfc(...) is the same as surf(...) except that a contour plot is drawn beneath the surface. See surface plots for more information.


Surface with Lighting

Accepted forms: surfl(z), surfl(x,y,z), surfl(x,y,z,S), surfl(x,y,z,S,K)

Surfl(...) is the same as surf(...) except that it produces a shaded surface plot based on a combination of diffuse, specular, and ambient lighting models. It is recommended to view the surfaces with a grayscale or similar colormap (such as gray, copper, bone, or pink). See surface plots for more information.

Example:
z = peaks(20) % just ignore everything else
choose colormap gray
use the default lighting given
choose interp shading


Surface with Normal

Accepted forms: surfnorm(z), surfnorm(x,y,z)

Surfnorm(...) is the same as surf(...) except that the unnormalized surface normals are included in the graph as well. See surface plots for more information.

Example:
t goes from 0 to 2*pi with 20 data points
choose cylinder instead of entering equations
z = 2 + cos(t)


The Plot Control Options

Back to Plot Controls .


Examples

A hadamard-colored sphere

One of my favorite examples, and one of the best to describe parametric surfaces
choose 2-variable, and surface plot
t goes from -31 to 31 with 31 points % ignore s
place in optional area 1: theta = pi*t/n
place in optional area 2: phi = (pi/2)*t/n
x = cos(phi)'*cos(theta)
y = cos(phi)'*sin(theta)
choose no on meshgrid
z = sin(phi)'*ones(size(theta)) % must be a matrix
c = hadamard(n+1) % hadamard is a function which returns a matrix of 1's and -1's
choose axis square
Note that although s was ignored, this is still very much a function of two variables, theta and phi. s was ignored because both theta and phi use the same angle vector, t.

Also try mixing up the order; i.e. put
z = hadamard(n+1)
c = sin(phi)'*ones(size(theta))


Bibliography

Nearly every definition above came out of:
"The Student Edition of Matlab, Version 4 User's Guide", MathWorks Inc., Prentice Hall, Englewood Cliffs, NJ 07632, 1995
(although the interface is with the professional version)

The hadamard-sphere example came out of:
"MATLAB User's Guide for UNIX workstations", MathWorks, Inc., 1992.


Return to Graphing Equations .

Return to Ken's Home Page .