A BRIEF LIST OF MATLAB COMMANDS Some Basic Commands (Note command syntax is case-sensitive!) matlab loads the program matlab into your workspace. quit quits matlab, returning you to the operating system. exit same as quit. who lists all of the variables in your matlab workspace. whos list the variables and describes their matrix size. clear deletes all matrices from active workspace. clear x deletes the matrix x from active workspace. ... the ellipsis defining a line continuation is three successive periods. save saves all the matrices defined in the current session into the file, matlab.mat. load loads contents of matlab.mat into current workspace. save filename saves the contents of workspace into filename.mat save filename x y z saves the matrices x, y and z into the file titled filename.mat. load filename loads the contents of filename into current workspace; the file can be a binary (.mat) file or an ASCII file. ! the ! preceding any unix command causes the unix command to be executed from matlab. Commands involving functions inline define a function, e.g. f = inline('x^2') fplot generate the graph of a function, e.g. fplot(f,[a,b]) plots f (a string defn) over the interval [a,b] fzero find the zero of a function, e.g. fzero(f,x0), computes zero of f near x0 or if x0 a vector then in the interval [x0_1, x0_2]. Commands Useful in Plotting. plot(x,y) creates an Cartesian plot of the vectors x & y. plot(y) creates a plot of y vs. the numerical values of the elements in the y-vector. semilogx(x,y) plots log(x) vs y. semilogy(x,y) plots x vs log(y) loglog(x,y) plots log(x) vs log(y). grid creates a grid on the graphics plot. title('text') places a title at top of graphics plot. xlabel('text') writes 'text' beneath the x-axis of a plot. ylabel('text') writes 'text' beside the y-axis of a plot. text(x,y,'text') writes 'text' at the location (x,y) . text(x,y,'text','sc') writes 'text' at point x,y assuming lower left corner is (0,0) and upper right corner is (1,1). gtext('text') writes text according to placement of mouse hold on maintains the current plot in the graphics window while executing subsequent plotting commands. hold off turns OFF the 'hold on' option. polar(theta,r) creates a polar plot of the vectors r & theta where theta is in radians. bar(x) creates a bar graph of the vector x. (Note also the command stairs(y).) bar(x,y) creates a bar-graph of the elements of the vector y, locating the bars according to the vector elements of 'x'. (Note also the command stairs(x,y).) hist(x) creates a histogram. This differs from the bargraph in that frequency is plotted on the vertical axis. mesh(z) creates a surface in xyz space where z is a matrix of the values of the function z(x,y). z can be interpreted to be the height of the surface above some xy reference plane. surf(z) similar to mesh(z), only surface elements depict the surface rather than a mesh grid. contour(z) draws a contour map in xy space of the function or surface z. meshc(z) draws the surface z with a contour plot beneath it. meshgrid [X,Y]=meshgrid(x,y) transforms the domain specified by vectors x and y into arrays X and Y that can be used in evaluating functions for 3D mesh/surf plots. print sends the contents of graphics window to printer. print filename -dps writes the contents of current graphics to 'filename' in postscript format. Equation Fitting polyfit(x,y,n) returns the coefficients of the n-degree polynomial for the vectors x and y. n must be at least 1 larger than the length of the vectors x and y. If n+1 = length(x) the result is an interpolating polynomial. If n+1 > length(x) the result is a least-squares polynomial fit. The coefficients are stored in order with that of the highest order term first and the lowest order last. polyval(c,x) calculates the values of the polynomial whose coefficients are stored in c, calculating for every value of the vector x. Data Analysis Commands max(x) returns the maximum value of the elements in a vector or if x is a matrix, returns a row vector whose elements are the maximum values from each respective column of the matrix. min (x) returns the minimum of x (see max(x) for details). mean(x) returns the mean value of the elements of a vector or if x is a matrix, returns a row vector whose elements are the mean value of the elements from each column of the matrix. median(x) same as mean(x), only returns the median value. sum(x) returns the sum of the elements of a vector or if x is a matrix, returns the sum of the elements from each respective column of the matrix. prod(x) same as sum(x), only returns the product of elements. std(x) returns the standard deviation of the elements of a vector or if x is a matrix, a row vector whose elements are the standard deviations of each column of the matrix. sort(x) sorts the values in the vector x or the columns of a matrix and places them in ascending order. Note that this command will destroy any association that may exist between the elements in a row of matrix x. hist(x) plots a histogram of the elements of vector, x. Ten bins are scaled based on the max and min values. hist(x,n) plots a histogram with 'n' bins scaled between the max and min values of the elements. hist((x(:,2)) plots a histogram of the elements of the 2nd column from the matrix x. fliplr(x) reverses the order of a vector. If x is a matrix, this reverse the order of the columns in the matrix. flipud(x) reverses the order of a matrix in the sense of exchanging or reversing the order of the matrix rows. This will not reverse a row vector! reshape(A,m,n) reshapes the matrix A into an mxn matrix from element (1,1) working column-wise. SPECIAL MATRICES zeros(n) creates an nxn matrix whose elements are zero. zeros(m,n) creates a m-row, n-column matrix of zeros. ones(n) creates a n x n square matrix whose elements are 1's ones(m,n)' creates a mxn matrix whose elements are 1's. ones(A) creates an m x n matrix of 1's, where m and n are based on the size of an existing matrix, A. zeros(A) creates an mxn matrix of 0's, where m and n are based on the size of the existing matrix, A. eye(n) creates the nxn identity matrix with 1's on the diagonal. Miscellaneous Commands length(x) returns the number elements in a vector. size(x) returns the size m(rows) and n(columns) of matrix x. rand returns a random number between 0 and 1. randn returns a random number selected from a normal distribution with a mean of 0 and variance of 1. rand(A) returns a matrix of size A of random numbers. ALGEBRAIC OPERATIONS IN MATLAB Scalar Calculations. + addition - subtraction * multiplication / right division (a/b means a ÷ b) \ left division (a\b means b ÷ a) ^ exponentiation The precedence or order of the calculations included in a single line of code follows the below order: Precedence Operation 1 parentheses 2 exponentiation, left to right 3 multiplication and division, left right 4 addition and subtraction, left right MATRIX ALGEBRA In matrix multiplication, the elements of the product, C, of two matrices A*B is calculated from Cij = ‘ (Aik * Bkj) {summation over the double index k} To form this sum, the number of columns of the first or left matrix (A) must be equal to the number of rows in the second or right matrix (B). The resulting product, matrix C, has an order for which the number of rows equals the number of rows of the first (left) matrix (A) and the product (C) has a number of columns equal to the number of columns in the second (right) matrix (B). It is clear that A*B IS NOT NECESSARILY EQUAL TO B*A! The PRODUCT OF A SCALAR AND A MATRIX is a matrix in which every element of the matrix has been multiplied by the scalar. ARRAY PRODUCTS Sometimes it is desired to simply multiply or divide each element of an matrix by the corresponding element of another matrix. These are called 'array operations" in 'matlab'. Array or element-by-element operations are executed when the operator is preceded by a '.' (period). Thus a .* b multiplies each element of a by the respective element of b a ./ b divides each element of a by the respective element of b a .\ b divides each element of b by the respective element of a a .^ b raise each element of a by the respective b element TRANSPOSE OF A MATRIX x' The transpose of a matrix is obtained by interchanging the rows and columns. The 'matlab' operator that creates the transpose is the single quotation mark, '. INNER PRODUCT OF TWO VECTORS The inner product of two row vectors G1 and G2 is G1*G2'. The inner product of two column vectors H and J is H'*J. OUTER PRODUCT OF TWO VECTORS If two row vectors exist, G1 and G2, the outer product is simply G1' * G2 {Note G1' is nx1 and G2 is 1xn} and the result is a square matrix in contrast to the scalar result for the inner product. DON'T CONFUSE THE OUTER PRODUCT WITH THE VECTOR PRODUCT IN MECHANICS! If the two vectors are column vectors, the outer product must be formed by the product of one vector times the transpose of the second! SOLUTION TO SIMULTANEOUS EQUATIONS Using the Matrix Inverse inv(a) returns the inverse of the matrix a. If ax=b is a matrix equation and a is the coefficient matrix, the solution x is x=inv(a)*b. Using Back Substitution a\b returns a column vector solution for the matrix equation ax=b where a is a coefficient matrix. b/a returns a row vector solution for the matrix equation xa=b where a is a coefficient matrix.