", e.g. "help polyval" or "help plot".
If you want a record of what you have done, then you can use the
diary command to send everything on your screen (what you type
and what MATLAB spits back) to a file, which you can print out later
(i.e. for turning in labs) or email to someone for help when things
are going wrong. Type "diary my_file" to turn diary on and specify
the file "my_file" for the output to go to. Type "diary off" to turn
it off. You can type "help diary" to learn more.
When you type mathematical commands to MATLAB (which includes simply
typing numbers), it will print out an answer to the screen unless you
explicitly say you want that answer suppressed. When you are first
learning to use MATLAB, it is useful to have that answer to be sure that
you are using the right command, but when you are working with big arrays
this can be unwieldy (and waste space in your "diary"). To suppress the
output, just type ";" after your command.
Defining variables
MATLAB works with vectors and matrices defined by a list of numbers using ";"
to indicate a new row, as follows:
- x = [ 1.1 3.0 4.7 5.2] for a row vector
- x = [ 1.1; 3.0; 4.7; 5.2] for a column vector
- x = [1 2 ; 3 4] for a 2x2 matrix
If you have a long vector with a regular structure, you can
define it using the ":" notation, e.g.
- x = [1:100] = [1 2 3 4 .... 99 100]
- x = [0:5:100] = [0 5 10 15 20 25 ... 95 100]
If you want a column vector instead of a row vector, simply define a
new variable that is the transpose, e.g. y = x'. Other useful things
to know are:
- x = pi gives x = 3.1416.... (x = 2*pi = 6.2832...)
- x = 1e10 and x = 10^10 are the same (as are 1e-10 and 10^-10)
Simple mathematical operations
Let x and y be matrices
- z = x * y matrix multiplication (inner product if
x is row and y is column, outer product if vice versa), must have compatible matrix dimensions
- z = x .* y point-by-point multiplication for x and y with
the same dimension (use this for multiplying vectors that correspond
to time functions)
- z= x + y point-by-point addition for x and y with
the same dimension
Operations that you might want to do on scalars include exponentiating
(x=exp(y)), taking a log (x=log(y) for natural log, x=log10(y) for
base 10 log), and finding sines and cosines (x=cos(y), x=sin(y)).
Plotting
To do a simple x-y plot, you would use the plot
command. If you wanted to plot a function f(t) vs. time t, you would
first define vector x to contain the time samples (e.g. x=[0:tau:end],
where "tau" is the time increment and "end" is the end time of the
x-axis) and then define a y vector containing the samples f(k*tau),
and then use the command "plot(x,y)". You should always use the
commands xlabel, ylabel and title to label the axes of plot that you
are going to turn in or put in a paper. If you want a single print-out
with multiple subplots on it, use the subplot command. For example,
"subplot(m,n,p)" followed by "plot(x,y)" will put your plot in picture
with the p-th location (counting left-to-right and top-to-bottom) of
an mxn set of plots. Do any plot labeling commands right after you
have done the plot command, since they will apply to the most recently
done subplot.
This page is maintained by Mari Ostendorf (mo@ee.washington.edu).
Last updated on 16 October 00.
Back EE235 Course Information Page