Links
|
1. Accessing MATLAB[return to Table of Contents]On most systems, after logging in one can enter MATLAB with the system command matlab and exit MATLAB with the command exit or quit. On a PC, for example, if properly installed, one may enter MATLAB with the command:
2. Entering matricesMATLAB works with essentially only one kind of object---a rectangular numerical matrix with possibly complex entries; all variables represent matrices. In some situations, 1-by-1 matrices are interpreted as scalars and matrices with only one row or one column are interpreted as vectors.Matrices can be introduced into MATLAB in several different ways:
When listing a number in exponential form (e.g. 2.34e-9), blank spaces must be avoided. Listing entries of a large matrix is best done in an M-file, where errors can be easily edited away (see sections 12 and 14). The built-in functions rand, magic, and hilb, for example, provide an easy way to create matrices with which to experiment. The command rand(n) will create an n x n matrix with randomly generated entries distributed uniformly between 0 and 1, while rand(m,n) will create an m x n one. magic(n) will create an integral n x n matrix which is a magic square (rows and columns have common sum); hilb(n) will create the n x n Hilbert matrix, the king of ill-conditioned matrices (m and n denote, of course, positive integers). Matrices can also be generated with a for-loop (see section 6 below). Individual matrix and vector entries can be referenced with indices inside parentheses in the usual manner. For example, A(2,3) denotes the entry in the second row, third column of matrix A and x(3) denotes the third coordinate of vector x. Try it. A matrix or a vector will only accept positive integers as indices. 3. Matrix operations, array operationsThe following matrix operations are available in MATLAB:
The "matrix division" operations deserve special comment. If A is an invertible square matrix and b is a compatible column, resp. row, vector, then
Array operations. The matrix operations of addition and subtraction already operate entry-wise but the other matrix operations given above do not---they are matrix operations. It is important to observe that these other operations, *, ^, \, and /, can be made to operate entry-wise by preceding them by a period. For example, either [1,2,3,4].*[1,2,3,4] or [1,2,3,4].\^2 will yield [1,4,9,16]. Try it. This is particularly useful when using Matlab graphics. 4. Statements, expressions, and variables; saving a sessionMATLAB is an expression language; the expressions you type are interpreted and evaluated. MATLAB statements are usually of the form
A statement is normally terminated with the carriage return. However, a statement can be continued to the next line with three or more periods followed by a carriage return. On the other hand, several statements can be placed on a single line if separated by commas or semicolons. If the last character of a statement is a semicolon, the printing is suppressed, but the assignment is carried out. This is essential in suppressing unwanted printing of intermediate results. MATLAB is case-sensitive in the names of commands, functions, and variables. For example, solveUT is not the same as solveut. The command who will list the variables currently in the workspace. A variable can be cleared from the workspace with the command clear variablename. The command clear alone will clear all nonpermanent variables. The permanent variable eps (epsilon) gives the machine precision---about 10^(-16) on most machines. It is useful in determining tolerences for convergence of iterative processes. A runaway display or computation can be stopped on most machines without leaving MATLAB with CTRL-C (CTRL-BREAK on a PC). Saving a session. When one logs out or exits MATLAB all variables are lost. However, invoking the command save before exiting causes all variables to be written to a non-human-readable diskfile named matlab.mat. When one later reenters MATLAB, the command load will restore the workspace to its former state. 5. Matrix building functionsConvenient matrix building functions are
If x is a vector, diag(x) is the diagonal matrix with x down the diagonal; if A is a square matrix, then diag(A) is a vector consisting of the diagonal of A. What is diag(diag(A))? Try it. Matrices can be built from blocks. For example, if A is a 3-by-3 matrix, then
6. For, while, if --- and relationsIn their basic forms, these MATLAB flow control statements operate like those in most computer languages.For. For example, for a given n, the statement
While. The general form of a while loop is
Relations. The relational operators in MATLAB are
A relation between matrices is interpreted by while and if to be true if each entry of the relation matrix is nonzero. Hence, if you wish to execute statement when matrices A and B are equal you could type
The for statement permits any matrix to be used instead of 1:n. See the User's Guide for details of how this feature expands the power of the for statement. 7. Scalar functionsCertain MATLAB functions operate essentially on scalars, but operate element-wise when applied to a matrix. The most common such functions are
8. Vector functionsOther MATLAB functions operate essentially on a vector (row or column), but act on an m-by-n matrix (m >= 2) in a column-by-column fashion to produce a row vector containing the results of their application to each column. Row-by-row action can be obtained by using the transpose; for example, mean(A')'. A few of these functions are
|
Jump to Bruce Driver's Homepage. Jump to Current Courses Page.
|