User:Eml4500.f08.lulz.abcd/HW1

Part 6: For, while, if - and relations
MATLAB handles most common computer code concepts like any other modern computer programming language. This includes for and while loops, if statements, and relational conditions.

For
for loops can be used very effectively in MATLAB to create matrices.

Ex 1: If you wanted to create a series of vectors where each value in the vector is the square of the index value of the position of the value in the vector, you could use this for loop:  EDU>> x = []; for i = 1:3, x=[x,i^2], end x =

1 x =

1    4 x =

1    4     9 The expression x = []; i = 3:-1:1, x=[x,i^2], end will give the same thing, except in reverse.

Ex 2: for loops can also be used to produce the Hilbert matrix. (Coding note, the semicolon prevents an intermediate printing which would be undesirable and the final H tells MATLAB to print the resulting H matrix.)  for i = 1:3 for j = 1:3 H(i,j) = 1/(i+j-1); end end H H =

1.0000   0.5000    0.3333    0.5000    0.3333    0.2500    0.3333    0.2500    0.2000

While
A while loop executes some command repeatedly so long as the condition it checks remains true.

Ex: MATLAB can calculate for a given number a, the smallest value n such that 2^n is less than or equal to a:  n = 0; a = 12; while 2^n  if n<0 parity = 0; elseif rem(n,2) == 0 parity = 2; else parity = 1; end

Relations
MATLAB uses the following relational operators in its programming language: < less than > greater than <= less than or equal to >= greater than or equal to == equal ~= not equal & and ~ not
 * or

Part 7: Scalar functions
MATLAB functions that are designed to operate on scalars can also be applied to matrixes, they will simply do so element-wise on the matrix elements. These are some common MATLAB functions that work in such a manner: sin, cos, tan, asin, acos, atan, ex, log, rem, abs, sqrt, sign, round, floor, ceil

Part 8: Vector functions
MATLAB functions that are designed to operate on vectors can also be applied to M x N matrixes (M >= 2), they will simply do so column-by-column which will produce a row vector containing the results of their operation in each column. To get these functions to operate on a row-by-row basis, use the transpose of the matrix. Common MATLAB functions that work like this are: max, min, sort, sum, prod, median, mean, std, any, all

These functions can also be nested within each other. Ex: to get the maximum single value in matrix A, use max(max(A)).

Part 9: Matrix functions
MATLAB has many matrix functions automatically built in for ease of use. These dramatically simplify programming and creating useful results from the software. These are the most useful of the functions: eig - Eigenvalues and Eigenvectors chol - Cholesky factorization svd - single value decomposition inv - inverse lu - LU factorization qr - QR factorization hess - Hessenberg form schur - Schur decomposition rref - reduced row echelon form expm - matrix exponential sqrtm - matrix square root poly - characteristic polynomial det - determinant size - size norm - 1-norm, 2-norm, F-norm, infinity-norm cond - condition number in the 2-norm rank - rank

As in other MATLAB functions, these can be used several ways. Ex: y = eig(A) and simply inputting eig(A) produce the same results. The output can also go to another matrix, [U,D] = eig(A) will produce a matrix with columns as the eigenvalues of A and a diagonal matrix D with the eigenvalues on the diagonals. This all may seem elementary, the point is that if there is a desired form of an output in MATLAB for a function, it is likely possible given the proper syntax.

Part 10: Command line editing and recall
The command line in MATLAB can be edited like most other text editors (usin up and down arrows, home and end keys, etc.). Full details of how the command line can be edited will be printed from the help cedit command.

Previous commands can be accessed using the up and down arrows on the command line. This is frequently more efficient than producing a full M-file for small problems. Commands may also be placed sequentially on the same command line using semi-colons to separate the commands.