User:Eml4500.f08.team.figuerrez/hw1

 1. Accessing MATLAB 

After logging in, you can enter MATLAB by using the system command matlab and exit MATLAB with the command exit. On systems that     permit multiple users, such as MS Windows and a Unix system, it is suitable to leave MATLAB active.

 2. Entering matrices 

Inputing either statements: A = [1 2 3; 4 5 6; 7 8 9]

and/or

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

will construct a 3 x 3 matrix assigning it to a variable A. The elements within a row of a matrix can be separated by either commas or a     blank space. However, when listing a number in exponential form, blank spaces should be avoided.

Two ways to enter complex matrices:

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

When listing complex numbers in a matrix, blank spaces must also be avoided. Either i or j may be used as the imaginary unit. If i and j are     used as variables, a new imaginary unit may be generated. (i.e. ii = sqrt(-1).)

The built-in functions rand, magic, and hilb, provide an easy way to create matrices.

rand(n) will create an n x n matrix with randomly generated entries between 0 and 1.

rand(m,n) will create an m x n matrix.

magic(n) will create an integral n x n which is a magic square (rows, column, and diagonals have common sum).

hilb(n) will create the n x n Hilbert matrix.

 3. Matrix operations, array operations 

Matrix operations used in MATLAB:

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

These matrix operations apply to scalars (1 x 1 matrices) as well.

 Array operations 

These operations, *, ^, \, and /, can be made to operate entry-wise by placing a period prior to the operation used.

For example, either [1,2,3,4].*[1,2,3,4] or [1,2,3,4].^2 will yield [1,4,9,16].

 4. Statements, expressions, and variables; saving a session 

MATLAB statements are usually of the form:

variable = expression, or simply expression

Expressions usually consist of operators, functions, and variable names. If the variable name and = sign are omitted, a variable ans (for answer) is automatically created. MATLAB is case-sensitive in the names of commands, functions, and variables. (i.e. solveUT is not     the same as solveut).

The command who (or whos) will list the variables currently in the workspace. A variable can be cleared from the workspace with the command clear variablename.

 Saving a session 

The command save before exiting causes all variables to be written to a non-human-readable disk file named matlab.mat. The command load will restore the workspace to a previous state.

 5. Matrix building functions 

Convenient matrix building functions:

eye identity matrix zeros matrix of zeros ones matrix of ones diag create or extract diagonals triu upper triangular part of a matrix tril lower triangular part of a matrix rand random generated matrix hilb Hilbert matrix magic magic square

 6. For, while, if – and relations 

 For 

Example:

x = []; for i = 1:n, x=[x,i^2], end

or         x = [];

for i = 1:n

x = [x,i^2]

end

will produce the same vector in reverse order.

 While 

Example: n = 0;

while 2n < a n = n + 1;

end n

will, for a given number a, compute and display the smallest nonnegative integer n such that 2n > or = to a

 If 

Example:

if n < 0

parity = 0;

elseif rem(n,2) = = 0

parity = 2;

else

parity = 1;

end

In two-way branching the elseif portion would be omitted.

 Relations 

Relationship operators in MATLAB: <     	less than

>     	greater than'''

<=     less than or equal

>=     greater than or equal

==     equal

~=     not equal