User:Eml4500.f08.delta 6.lopez/Week 1: Lecture Notes, Matlab (Contribution)

Section 6: For, While, and If Statements
The following MATLAB flow control statements operate similarly to most computer languages.

For a given n, the statement below will produce a certain n-vector, which in this case is given a value of 10. EDU>> x=[];

EDU>> for i = 1:10, x=[x,i^2],end

x =

1

x =

1    4

x =

1    4     9

x =

1    4     9    16

x =

1    4     9    16    25

x =

1    4     9    16    25    36

x =

1    4     9    16    25    36    49

x =

1    4     9    16    25    36    49    64

x =

1    4     9    16    25    36    49    64    81

x =

1    4     9    16    25    36    49    64    81   100

The next code produces another n-vector using different parameters.

EDU>> x=[];

EDU>> for i = 1:5

x=[x,i^2]

end

x =

1

x =

1    4

x =

1    4     9

x =

1    4     9    16

x =

1    4     9    16    25

This next code produces the result as above, but in reverse order.

EDU>> x=[];for i = 5:-1:1, x=[x,i^2], end

x =

25

x =

25   16

x =

25   16     9

x =

25   16     9     4

x =

25   16     9     4     1

The statement below will produce the mxn hilbert matrix. For this example, m and n are given a value of 5. This allows for any matrix to be formed, unlike the 1:n statement.

EDU>> for i=1:5

for j= 1:5

H(i,j)=1/(i*j-1);

end

end

EDU>> H

H =

Inf   1.0000    0.5000    0.3333    0.2500

1.0000   0.3333    0.2000    0.1429    0.1111

0.5000   0.2000    0.1250    0.0909    0.0714

0.3333   0.1429    0.0909    0.0667    0.0526

0.2500   0.1111    0.0714    0.0526    0.0417

Once a matrix is defined, the following statements compute the sum of matrix A by adding all of the column sums. The only difference between the two statements are the amount of time taken to complete these computations.

EDU>> A=[1 2 3 4;5 6 7 8]; EDU>> s=0; EDU>> for c=A s=s+sum(c); end EDU>> s

s =

36

EDU>> sum(sum(A))

ans =

36

The next type of MATLAB function is the WHILE statement. For a given number a, the following statement will produce the smallest nonnegative number n such that 2^n>=a.

EDU>> n=0;

EDU>> while 2^n> n

n =

4

The final MATLAB statement is the IF statement. This type of statement will only work if the statement is true. The following example illustrates multiple branching.

EDU>> if n<0

parity = 0;

elseif rem(n,2)==0

parity = 2;

else

parity = 1;

end

EDU>> parity

parity =

2

EDU>> n

n =

4

The remainder function produces the amount left over when using division. The example below shows the remainder of four divided by two.

EDU>> rem(4,2)

ans =

0

This example illustrates the result of using the remainder function without fully completing the argument.

Many relational operators are available for use in MATLAB. A chart has been formed listing the most common operations, and a short description of each.

The following statements are examples of the various relational operators in use. An answer of 1 signifies a true statement, while an answer of 0 illustrates a false statement.

EDU>> 3<5

ans =

1

EDU>> 3>5

ans =

0

EDU>> 3==5

ans =

0

EDU>> 3==3

ans =

1

When applying relational operators to matrices, the true statements are illustrated by a 1 or nonzero. The following examples depict three scenarios of using relational operators on matrices.

EDU>> a=rand(5)

a =

0.0357   0.7431    0.0318    0.6948    0.3816

0.8491   0.3922    0.2769    0.3171    0.7655

0.9340   0.6555    0.0462    0.9502    0.7952

0.6787   0.1712    0.0971    0.0344    0.1869

0.7577   0.7060    0.8235    0.4387    0.4898

EDU>> b=triu(a)

b =

0.0357   0.7431    0.0318    0.6948    0.3816

0   0.3922    0.2769    0.3171    0.7655

0        0    0.0462    0.9502    0.7952

0        0         0    0.0344    0.1869

0        0         0         0    0.4898

EDU>> a==b

ans =

1    1     1     1     1

0    1     1     1     1

0    0     1     1     1

0    0     0     1     1

0    0     0     0     1

Section 7: Scalar Functions
Some functions in MATLAB are scalar in nature, but operate element-wise when used in a matrix. The most common of these functions are found below.

=Section 8: Vector Functions=

MATLAB functions operate essentially on a vector (row or column). A row by row application can be obtained by using the transpose, i.e. mean(A’)’. As shown the mean(A’)’ function determines the mean of the matrix in a row by row fashion.

EDU>> mean(A')'

ans =

5    5     5

Moreover, to find the maximum entry in a matrix the max(max(A)) function should be used rather than max(A). Here it is shown that the maximum value for the matrix A is equal to 9.

EDU>> max(max(A))

ans =

9

=Section 9: Matrix Functions=

The matrix functions in MATLAB offer the programs greatest asset. Some of the most useful functions are tabulated below.

Once a matrix is defined, the eig(A) function is used to find the eigenvalues of a given matrix.

EDU>> A

A = 8    1     6

3    5     7

4    9     2

EDU>> eig(A)

ans =

15.0000

4.8990

-4.8990

The [U,D] = eig(A) function, on the other hand, produces a matrix U where each column signifies an eigenvector for matrix A and a matrix D containing the eigenvalues of matrix A along its diagonal. The matrix A that is used for these computations is the same as above. EDU>> [U,D]= eig(A)

U =

-0.5774  -0.8131   -0.3416

-0.5774   0.4714   -0.4714

-0.5774   0.3416    0.8131

D =

15.0000        0         0

0   4.8990         0

0        0   -4.8990