A BRIEF LIST OF MATLAB COMMANDS. Some Basic Commands (Note command syntax is case-sensitive!) matlab loads the program matlab into your workspace. - PDF

Please download to get full document.

View again

of 60
All materials on our website are shared by users. If you have any questions about copyright issues, please report us to resolve them. We are always happy to assist you.
Information Report
Category:

Business & Economics

Published:

Views: 7 | Pages: 60

Extension: PDF | Download: 0

Share
Related documents
Description
A BRIEF LIST OF MATLAB COMMANDS Some Basic Commands (Note command syntax is case-sensitive!) matlab loads the program matlab into your workspace. quit exit who whos clear quits matlab, returning you to
Transcript
A BRIEF LIST OF MATLAB COMMANDS Some Basic Commands (Note command syntax is case-sensitive!) matlab loads the program matlab into your workspace. quit exit who whos clear quits matlab, returning you to the operating system. same as quit. lists all of the variables in your matlab workspace. list the variables and describes their matrix size. 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 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. Some basic commands you will need: matlab loads the program matlab into your workspace quit exit who whos quits matlab, returning you to the operating system same as quit lists all of the variables in your matlab workspace list the variables and describes their matrix size NOTE - When using the workstations, clicking on UP ARROW will recall previous commands. If you make a mistake, the DELETE key OR the backspace key may be used to correct the error; however, one of these two keys may be inoperable on particular systems. 'matlab' uses variables that are defined to be matrices. A matrix is a collection of numerical values that are organized into a specific configuration of rows and columns. The number of rows and columns can be any number, for example, 3 rows and 4 columns define a 3 x 4 matrix which has 12 elements in total. A scalar is represented by a 1 x 1 matrix in matlab. A vector of n dimensions or elements can be represented by a n x 1 matrix, in which case it is called a column vector, or a vector can be represented by a 1 x n matrix, in which case it is called a row vector of n elements. The matrix name can be any group of letters and numbers up to 19, but always beginning with a letter. Thus 'x1' can be a variable name, but '1x' is illegal. 'supercalafragilesticexpealladotious' can be a variable name; however, only the first 19 characters will be stored! Understand that 'matlab' is case sensitive , that is, it treats the name 'C' and 'c' as two different variables. Similarly, 'MID' and 'Mid' are treated as two different variables. Here are examples of matrices that could be defined in 'matlab'. Note that the set of numerical values or elements of the matrix are bounded by brackets...[ ]. c = 5.66 or c = [5.66] c is a scalar or a 1 x 1 matrix x = [ 3.5, 33.22, 24.5 ] x is a row vector or 1 x 3 matrix x1 = [ 2 x1 is column vector or 5 4 x 1 matrix 3 -1] A = [ A is a 4 x 3 matrix ] An individual element of a matrix can be specified with the notation A(i,j) or Ai,j for the generalized element, or by A(4,1)=5 for a specific element. When 'matlab' prints a matrix on the monitor, it will be organized according to the size specification of the matrix, with each row appearing on a unique row of the monitor screen and with each column aligned vertically and right-justified. The numerical values that are assigned to the individual elements of a matrix can be entered into the variable assignment in a number of ways. The simplest way is by direct keyboard entry; however, large data sets may be more conveniently entered through the use of stored files or by generating the element values using matlab expressions. First, we will look at the use of the keyboard for direct entry. KEYBOARD DEFINITION OR ENTRY FOR A MATRIX A matrix can be defined by a number of matlab expressions. Examples are listed below for a 1 x 3 row vector, x, whose elements are x(1) = 2, x(2) = 4 and x(3) = -1. x = [ ] or x=[2 4-1] or x = [ 2,4,-1 ] (A keystroke 'enter' follows each of the above matlab statements.) Notice that brackets must be used to open and close the set of numbers, and notice that commas or blanks may be used as delimiters between the fields defining the elements of the matrix. Blanks used around the = sign and the brackets are superfluous; however, they sometimes make the statement more readable. A 2x4 matrix, y, whose elements are y(1,1)=0, y(1,2) = y(1,3) = 2, y(1,4) = 3, y(2,1) = 5, y(2,2) = -3, y(2,3) = 6 and y(2,4) = 4 can be defined y = [ ] or y = [ ; ] The semicolon ; is used to differentiate the matrix rows when they appear on a single line for data entry. The elements of a matrix can be defined with algebraic expressions placed at the appropriate location of the element. Thus a = [ sin(pi/2) sqrt(2) 3+4 6/3 exp(2) ] defines the matrix a = [ ] A matrix can be defined by augmenting previously defined matrices. Recalling the matrix, x, defined earlier x1 = [ x 5 8 ] creates the result x1 = [ ] The expression x(5) = 8 creates x = [ ] Notice that the value 0 is substituted for x(4) which has not been explicitly defined. Recalling the definition of matrix, y, above, the expressions c = [ ] z = [ y;c ] creates z = [ ] Note that every time a matrix is defined and an 'enter' keystroke is executed, matlab echoes back the result. TO CANCEL THIS ECHO, THE MATLAB COMMAND LINE CAN INCLUDE A SEMICOLON AT THE END OF THE LINE BEFORE THE KEYSTROKE 'ENTER'. z = [ y ; c ] ; LINE CONTINUATION Occasionally, a line is so long that it can not be expressed in the 80 spaces available on a line, in which case a line continuation is needed. In matlab, the ellipsis defining a line continuation is three successive periods, as in ... . Thus gives the result ans = 30 Notice that in this simple arithmetic operation, no matrix was defined. When such an operation is executed in matlab, the result is assigned to the matrix titled ans . A subsequent operation without an assignment to a specific matrix name will replace the results in 'ans' by the result of the next operation. In the above, 'ans' is a 1x1 matrix, but it need not be so in general. BEFORE YOU QUIT THIS SESSION!!!!! If this is your first lesson using matlab, execute the matlab commands 'who' and whos' before you 'quit'. Note that each of these commands lists the matrices you have defined in this session on the computer. The command 'whos' also tells you the properties of each matrix, including the number of elements, the row and column size (row x column) and whether the elements are complex or real. IMPORTANT! If you execute the matlab command 'save' before you quit, all of the matrices that have been defined will be saved in a file titled matlab.mat stored in your workspace. Should you desire to save specific matrices during any session, the command 'save' followed by the name of the matrix can be executed. More detail on how to save and recall your matrices is discussed in Lesson 2. PRACTICE PROBLEMS Determine the size and result for the following matrices. Subsequently, carry out the operations on matlab that define the matrices, and check your results using the 'whos' statement. 1. a = [1,0,0,0,0,1] 2. b = [2;4;6;10] 3. c = [5 3 5; 6 2-3] 4. d= [ ] 5. e = [ ; ; ] 6. t = [4 24 9] q = [t 0 t] 7. x = [ 3 6 ] y = [d;x] z = [x;d] 8. r = [ c; x,5] 9. v = [ c(2,1); b ] 10. a(2,1) = -3 (NOTE: Recall matrix a was defined in (1) above.) New commands in this lesson: save saves all the matrices defined in the current session into the file, matlab.mat, located in the directory from which you executed matlab. load loads contents of matlab.mat into current workspace save filename x y z save 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. clear x erases the matrix 'x' from your workspace clear erases ALL matrices from your workspace NOTE - When using PROMATLAB on a workstation, files are stored in the directory from which you invoked the 'matlab' command. When using the workstation, create a matlab subdirectory titled 'matlab' or some similar name. Thereafter, store all files and conduct all matlab sessions in that subdirectory. ASCII FILES An ASCII file is a file containing characters in ASCII format, a format that is independent of 'matlab' or any other executable program. Using ASCII format, you can build a file using a screen editor or a wordprocessing program, for example, that can be read and understood by 'matlab'. The file can be read into 'matlab' using the load command described above. Using a text editor or using a wordprocessor that is capable of writing a file in ASCII format, you simply prepare a matrix file for which EACH ROW OF THE MATRIX IS A UNIQUE LINE IN THE FILE, with the elements in the row separated by blanks. An example is the 3 x 3 matrix If these elements are formatted as described above and stored in a filename titled, for example, x.dat, the 3 x 3 matrix 'x' can be loaded into 'matlab' by the command load x.dat Open the Text Editor window on your workstation. Build a 3x3 matrix in the editor that follows the format explained above. Store this in your matlab directory using the command save ~/matlab/x.dat The suffix,.dat, is not necessary; however, it is strongly recommended here to distinguish the file from other data files, such as the.mat files which will be described below. Of course if you define the file with the.dat or any other suffix, you must use that suffix when downloading the file to 'matlab' with the 'load' command. Remember, the file must be stored in the directory in which you are executing matlab. In the above example, it is assumed that this directory is titled 'matlab'. Now go to a window in which matlab is opened. We desire to load the matrix x into matlab. We might wonder if the file x.dat is actually stored in our matlab directory. To review what files are stored therein, the unix command 'ls' can be evoked in matlab by preceding the command with an exclamation mark,!. Type the command! ls Matlab will list all of the files in your directory. You should find one titled x.dat . Now, type the command load x.dat The 3x3 matrix defined above is now down-loaded into your working space in matlab. To ensure that is so, check your variables by typing 'who' or 'whos'. FILES BUILT BY MATLAB...THE.mat FILE If at any time during a matlab session you wish to store a matrix that has been defined in the workspace, the command save filename will create a file in your directory that is titled filename.mat. The file will be in binary format, understandable only by the 'matlab' program. Note that 'matlab' appends the.mat suffix and this can be omitted by the user when describing the filename. If the matrix 'x' has been defined in the workspace, it can be stored in your directory in binary matlab format with the command save x The filename need not be the same as the matrix name. The file named, for example, 'stuff' can be a file that contains the matrix 'x'. Any number of matrices can be stored under the filename. Thus the command string save stuff x y z will store the matrices x, y and z in the file titled 'stuff'. The file will automatically have the suffix.mat added to the filename. If, during a session, you type the command 'save' with no other string, all of the current matrices defined in your workspace will be stored in the file 'matlab.mat'. The load command when applied to.mat files follows the same format as discussed above for ASCII files; however, you can omit the file suffix '.mat' when loading.mat files to your workspace. COLON OPERATOR The colon operator ' : ' is understood by 'matlab' to perform special and useful operations. If two integer numbers are separated by a colon, 'matlab' will generate all of the integers between these two integers. a = 1:8 generates the row vector, a = [ ]. If three numbers, integer or non-integer, are separ
Recommended
View more...
We Need Your Support
Thank you for visiting our website and your interest in our free products and services. We are nonprofit website to share and download documents. To the running of this website, we need your help to support us.

Thanks to everyone for your continued support.

No, Thanks