User:Eml4507.s13.team7.howe

From Wikiversity
Jump to navigation Jump to search

R1.1

Given

The MATLAB Primer document.

Find

Reproduce all of the examples in the MATLAB Primer document.

Solution

2. Entering Matrices.

There are many different ways that a matrix can be entered into MATLAB.

Both of the following ways can be used to create a matrix in MATLAB, and as can be seen, they both produce the same result.

>> 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

]


A =


    1     2     3
    4     5     6
    7     8     9


There are also different ways to input a matrix involving complex numbers into MATLAB.

Here are two different ways that produce the same result in MATLAB.


A =


  1.0000 + 5.0000i   2.0000 + 6.0000i
  3.0000 + 7.0000i   4.0000 + 8.0000i


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


A =


  1.0000 + 5.0000i   2.0000 + 6.0000i
  3.0000 + 7.0000i   4.0000 + 8.0000i


3. Matrix operations, array operations.

When doing matrix operations, the operation needs to be proceeded by a dot, whereas your usual operations do not. Different operations can be used to obtain a result. For example,

>> [1,2,3,4].*[1,2,3,4]


ans =


    1     4     9    16


produces the same result as


>> [1,2,3,4].^2


ans =


    1     4     9    16


5. Matrix building functions.

The code zeros(m,n) creates a matrix of size m by n of zeroes, and the code zeros(n) creates a matrix of size n by n. If there is a matrix called A, then the code zeros(size(A)) will produce a matrix that is the same size as A. If there is a vector called x, then the code diag(x) will create a matrix where x is the diagonal of the matrix. For example, if

>> A=[1 2; 3 4]


A =


    1     2
    3     4


then

>> diag(diag(A))


ans =


1 0

4 0


Matrices can also be built from blocks. For example, if

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


A =


    1     2     3
    4     5     6
    7     8     9


then

>> B=[A, zeros(3,2); zeros(2,3), eye(2)]


B =


    1     2     3     0     0
    4     5     6     0     0
    7     8     9     0     0
    0     0     0     1     0
    0     0     0     0     1


Thus creating a 5 by 5 matrix called B.


6. For, while, if – and relations.

MATLAB can act like most other computer programs when doing flow control statements.

When using the for statement, the code needs to be in the form of the following, however, notice how the two following statements are written to be the reverse of the other.


>> n=5;

>> x=[]; for i=1:n, 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=[]; for i=n:-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


A for statement can also be used to create a m by n Hilbert matrix.

If n=5 and m=5, then

>> for i=1:m; for j=1:n; H(i,j)=1/(i+j-1); end;end;H


H =


   1.0000    0.5000    0.3333    0.2500    0.2000
   0.5000    0.3333    0.2500    0.2000    0.1667
   0.3333    0.2500    0.2000    0.1667    0.1429
   0.2500    0.2000    0.1667    0.1429    0.1250
   0.2000    0.1667    0.1429    0.1250    0.1111


The for statement also allows a matrix to be used instead of using the code 1:n. For example, the following can compute the sum of all the elements in A by adding its column sums.

If

A =


    1     2     3
    4     5     6
    7     8     9


Then

>> s=0; for c=A; s=s+sum(c); end

>> s


s =


   45



Another loop that can be used is the while loop. A while loop keeps computing the loops statements until the while relation becomes untrue. For example, the following will loop until it discovers the smallest integer n that satisfies the statement 2n ≥ a.

If a=5, then

>> n=0; while 2^n < a; n=n+1; end; n


n =


    3


An if statement is similar to a while loop in the way that it will keep computing until the if statement’s relation becomes untrue. The following is an example of a typical if statement.

If n = -2, then

>> if n<0

parity=0

elseif rem(n,2)==0

parity=2

else

parity=1

end


parity =


    0



8. Vector functions.

There are many useful vector functions that can be used in MATLAB, such as max, min, sort, sum, prod, median, mean, std, any, and all. For example, if

A =


    1     2     3
    4     5     6
    7     8     9


then the maximum element in matrix A can be found by

>> max(max(A))


ans =


    9


9. Matrix functions.

MATLAB has many preset functions that can be used, and many of them can produce multiple outputs. For example, to find the Eigen values of the matrix A, the following code can be used.

If

A =


    1     2     3
    4     5     6
    7     8     9

then

>> y=eig(A)


y =


  16.1168
  -1.1168
  -0.0000


Two matrices can also be created called U and D, where U is a matrix whose columns are the eigenvectors of A, and where D is a diagonal matrix that has its diagonal consist of the eigenvalues of matrix A.


>> [U,D]=eig(A)


U =


  -0.2320   -0.7858    0.4082
  -0.5253   -0.0868   -0.8165
  -0.8187    0.6123    0.4082



D =


  16.1168         0         0
        0   -1.1168         0
        0         0   -0.0000



10. Command line editing and recall.

Using MATLAB, it is also useful to compares functions and graphs at times, and to repeatedly recall functions to tweak and change small values in. For example, the following code is useful if one wanted to compare the graphs of y=sin(nx) and y=sin(mx). Then these functions could be recalled and repeated comparisons could be done if m and n values were changed and experimented with.


>> m=2;

>> n=3;

>> x=0:.01:2*pi;

>> y=sin(m*x);

>> z=cos(n*x);

>> plot(x,y,x,z)



11. Submatrices and colon notation.
Submatrices and colon notation are both very useful operations in MATLAB because they both reduce the need for loops, which is important because loops can slow down MATLAB if large files of data are being used. Colon notation is a useful way for representing matrices. For example, the code 1:5 actually represents a vector as can be seen below.

>> 1:5

ans =

    1     2     3     4     5

By default, MATLAB will make the spacing between elements of a vector equal to one. However, the increment between elements can be changed. For example,

>> 0.2:0.2:1.2

ans =

   0.2000    0.4000    0.6000    0.8000    1.0000    1.2000

>> 5:-1:1

ans =

    5     4     3     2     1

This way of coding can then be expanded into creating tables. For example, to create a table of sines can be shown below.

>> x=[0:.1:2]';
>> y=sin(x);
>> [x y]

ans =

        0         0
0.1000 0.0998
0.2000 0.1987
0.3000 0.2955
0.4000 0.3894
0.5000 0.4794
0.6000 0.5646
0.7000 0.6442
0.8000 0.7174
0.9000 0.7833
1.0000 0.8415
1.1000 0.8912
1.2000 0.9320
1.3000 0.9636
1.4000 0.9854
1.5000 0.9975
1.6000 0.9996
1.7000 0.9917
1.8000 0.9738
1.9000 0.9463
2.0000 0.9093

Colon notation is also very useful in accessing the submatrices of a matrix. For example, if there was a matrix A.

>> A=[1:5;1:5;1:5;1:5;1:5]

A =

    1     2     3     4     5
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5

Then, for example, you could access the first four entries of the third column.
>> A(1:4,3)

ans =

    3
3
3
3

A colon all by itself represents an entire row or column. For example,
>> A(:,3)

ans =

    3
3
3
3
3

>> A(1:4,:)

ans =

    1     2     3     4     5
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5

Furthermore, arbitrary integral vectors can be used as subscripts. For example,

>> A(:,[2 4])

ans =

    2     4
2 4
2 4
2 4

0 4

Subscripts like these can be used on both sides of an assignment. For example, if there was a matrix B, then the following code could replace the columns 2,4,5 of A with the first three columns of B.

>> B=A'

B =

    1     1     1     1     1
2 2 2 2 2
3 3 3 3 3
4 4 4 4 4
5 5 5 5 5

>> A(:,[2 4 5])=B(:,1:3)

A =

    1     1     3     1     1
1 2 3 2 2
1 3 3 3 3
1 4 3 4 4
1 5 3 5 5

Parts of a matrix can also be multiplied by other matrices. For example, the columns 2 and 4 of A can be multiplied by a matrix.

>> A(:,[2,4])=A(:,[2,4])*[1 2;3 4]

A =

    1     4     3     6     1
1 8 3 12 2
1 12 3 18 3
1 16 3 24 4
1 20 3 30 5

Furthermore, the order of elements in a matrix can be flipped and a horizontal matrix can be flipped into a vertical matrix and vice versa. This can be done by using a number of different commands, like the following.

>> n=5;
>> x=[1 2 3 4 5]

x =

    1     2     3     4     5

>> x=x(n:-1:1)

x =

    5     4     3     2     1

>> y=fliplr(x)

y =

    1     2     3     4     5

>> y=flipud(x')

y =

    1
2
3
4
5

On our honor, we did this assignment on our own, without looking at the solutions in previous semesters or other online solutions.