User:Eml4500.f08.wiki1.ambrosio/hw1

Section 1 How to Access MATLAB
After logging onto a computer with MATLAB installed, run the system command matlab or click on the desktop icon. To exit MATLAB, enter quit or exit in the MATLAB command window, or click on the File menu and select Exit MATLAB.

When the Editor in MATLAB is opened, it is convenient to have the Editor and Command windows opened separately. The specifics of the Editor are covered later on in this tutorial.

Section 2 Entering Matrices
MATLAB is based around operating with matrices. In fact, MATLAB is an abbreviation for Matrix Laboratory. The program can interpret these matrices as either scalars or vectors.

Matrices can be created by entering an explicit list of elements, generating built in statements and functions, creating a disk file with a local editor, or loading external data files or applications.

The following are all examples for creating matrix A, which is a 3x3 matrix. A = [1 2 3; 4 5 6; 7 8 9]

A = [1,2,3;4,5,6;7,8,9]

A = [ 1 2 3	4 5 6	7 8 9]

Notice that the elements in the rows of the matrix can be separated by commas or blank spaces.

The following is an example of how to enter a number in exponential form.

x = 2.34e-9

Notice when the number in entered, there are no spaces in between the e.

MATLAB is capable of doing computations with complex numbers. The following are examples of how to create complex matrices.

A = [1 2;3 4] + i*[5 6;7 8]

A = [1+5i 2+6i;3+7i 4+8i]

Notice when the complex numbers were listed inside the matrix, no blank spaces were used. It should also be noted that either i or j can be used as the imaginary unit. Also, if i or j are assigned as variables to represent a new value, they will not retain their value as an imaginary number. For example if the user enters the command

i = 5

then i will no longer be equal to 'sqrt(-1)', it will be equal to 5.

If a large matrix must be entered, creating an ASCII file is a good option because it can make it easier to correct errors. If an ASCII file is created and named data.ext, simply type load data.ext into the MATLAB command line and press enter. This command will read the file and save it as the variable data. MATLAB also has built in functions such as rand, magic, and hilb. If rand(n) is typed into the command line, MATLAB will create a n x n matrix with randomly generated numbers distributed uniformly between 0 and 1. If rand(m,n) is typed into the command line, MATLAB will create a m x n matrix with the same criteria as before. If magic(n) is typed into the command line, MATLAB will create an integral n x n matrix which is a magic square. This means all the rows, columns, and diagonals have a common sum. If hilb(n) is typed into the command line, MATLAB will create a n x n Hilbert matrix.

Individual entries in a matrix can be easily referenced. For example,

A(2,3)

selects the value in the second row, third column of matrix A. Also

X(3)

Would select the third coordinate of vector X.

Section 3 Matrix and Array Operations
The following matrix operations are available in MATLAB:

+	addition -	Subtraction ^	power ‘	conjugate transpose \	left division /	right division
 * multiplication

Since a scalar value entered into MATLAB is stored as a 1 x 1 matrix, these operations also apply to those values. If MATLAB is told to perform an operation that is not possible for a specific matrix, an error message will appear on the screen.

Matrix division can sometimes be confusing in MATLAB. The difference in right and left division is shown below.

x = A\b is the solution of A*x = b

x = b/A is the solution of x*A = b (another way of writing this could be x = (A’\b’)’ )

It is also important to note that addition and subtraction operate entry wise, but multiplication, power, and left and right division operate as matrix operations. These matrix operations can operate as entry wise operations by placing a period in front of them. An example is

[1,2,3,4].*[1,2,3,4]	or 	[1,2,3,4].^2

Both of these operations will give [1,4,9,16] as the solution.

Section 4 Statements, Expressions, and Variables and Saving Sessions
MATLAB uses expression language. This means when values are typed in, they are entered in the form of variable = expression or just as expression. For example,

x = 5*4

The evaluation of this expression is created as a matrix and displayed on the screen, such as

x = 20

If a variable and equal sign are not used, the result is automatically saved as the variable ans.

The user can also decide to continue the statement to the next line by using three or more periods (…) followed by enter, or several statements can be placed on a single line by separating them with commas (,) or semicolons (;).

If the variable is rather large, it may be useful to suppress the statement. By placing a semicolon (;) at the end of a statement, the assignment is carried out, but the result will not be printed on the screen.

It is also important to note that MATLAB is not case sensitive. For example, alpha is not the same as Alpha.

In order to see a list of variables that are currently being used in the workspace, type who (or whos) in the command line. If a variable needs to be cleared from the workspace, enter clear variablename'. If clear is entered into the command line, all nonpermanent variables will be cleared.

If the unit round off needs to be changed, type in eps (epsilon) to have MATLAB use a round off of 10^-16. This can be useful in specifying tolerances for convergence of iterative processes.

Sometimes a computer will enter a loop that does not stop or takes too long to calculate. These computations can be stopped in MATLAB by pressing CTRL-c.

When MATLAB is closed, all of the variables in the workspace are deleted. If these variables need to be saved, enter save in the command line before closing the program. These variables are written to a disk file named matlab.mat. When MATLAB is started again, enter the command load and the variables will be restored to the workspace.

Section 5 Matrix Building Functions
The following are convenient matrix building functions:

eye		identity matrix zeros 		matrix of zeros ones		matrix of ones diag		create or extract diagnols triu		upper triangular part of a matrix tril		lower triangular part of a matrix rand		randomly generated matrix hilb		Hilbert matrix magic		magic square toeplitz	see help toeplitz

For example, zeros(m,n) creates a m x n matix of zeros, and zeros(n) produces a n x n matrix of zeros. If A has already been defined as a matrix, then zeros(size(A)) will create a matrix of zeros that is the same size as matrix A.

If x has already been defined as a vector, diag(x) creates a diagonal matrix with x down the diagonal. If A has already been defined as a square matrix, then diag(A) creates a vector consisting of the diagonal of A.

Matrices can also be built from blocks consisting of other matrices. For example, if A is defined as a 3 x 3 matrix, then B = [A, zeros(3,2); zeros(2,3), eye(2)] will be 5 x 5 matrix.