Matlab and Matrices: Math 45 - Linear Algebra David Arnold David-Arnold@Eureka - Redwoods.cc - Ca.us

Download as pdf or txt
Download as pdf or txt
You are on page 1of 7

Matlab and Matrices

Math 45 Linear Algebra David Arnold David-Arnold@Eureka.redwoods.cc.ca.us


Abstract In this exercise you will learn how to enter and edit matrices in Matlab. You will also experiment with some of Matlabs built-in matrix builders. You will also learn how to build matrices from vectors and blocks of matrices. Prequisites: None.

Introduction
This is an interactive document designed for online viewing. Weve constructed this onscreen documents because we want to make a conscientious eort to cut down on the amount of paper wasted at the College. Consequently, printing of the onscreen document has been purposefully disabled. However, if you are extremely uncomfortable viewing documents onscreen, we have provided a print version. If you click on the Print Doc button, you will be transferred to the print version of the document, which you can print from your browser or the Acrobat Reader. We respectfully request that you only use this feature when you are at home. Help us to cut down on paper use at the College. Much eort has been put into the design of the onscreen version so that you can comfortably navigate through the document. Most of the navigation tools are evident, but one particular feature warrants a bit of explanation. The section and subsection headings in the onscreen and print documents are interactive. If you click on any section or subsection header in the onscreen document, you will be transferred to an identical location in the print version of the document. If you are in the print version, you can make a return journey to the onscreen document by clicking on any section or subsection header in the print document. Finally, the table of contents is also interactive. Clicking on an entry in the table of contents takes you directly to that section or subsection in the document.

1.1

Working with Matlab


This document is a working document. It is expected that you are sitting in front of a computer terminal where the Matlab software is installed. You are not supposed to read this document as if it were a short story. Rather, each time your are presented with a Matlab command, it is expected that you will enter the command, then hit the Enter key to execute the command and view the result. Furthermore, it is expected that you will ponder the result. Make sure that you completely understand why you got the result you did before you continue with the reading.

Entering Matrices
Entering matrices in Matlab is easy. Enter the following at the Matlab prompt.

Matlab and Matrices

>> A=[1,2,3;4,5,6;7,8,9] Note how semicolons signal the end of a row, while commas are used to separate the entries in a row. Spaces can also be used to separate the entries in a row. >> A=[1 2 3;4 5 6;7 8 9]

2.1

Special Matrices in Matlab


Matlab has a number of built-in routines to help create matrices 1. You can create a matrix of zeros of any size 2. >> A=zeros(5) >> B=zeros(3,5) You easily can create a matrix of zeros that is the same size as a given matrix. >> C=magic(5) >> D=zeros(size(C)) You can create matrices of ones in a similar manner. >> >> >> >> A=ones(6) B=ones(2,10) C=hilb(5) D=ones(size(C))

When performing simulations in Matlab it is useful to build matrices of random numbers. You can create a matrix of uniformly distributed random numbers, each between 0 and 1 (0 inclusive), with the following commands. >> A=rand(6) >> B=rand(5,3) Scalar multiplication works with matrices in exactly the same manner as with vectors. >> C=10*rand(5) Matlab has a variety of routines that round numbers. >> >> >> >> D=floor(C) D=ceil(C) D=round(C) D=fix(C)

The identity matrix has ones on its main diagonal and zeros everywhere else. 3 >> I=eye(5)

For a list of these elementary matrices, type help elmat at the Matlab prompt. Remember, help is readily available for any Matlab function. For example, type help zeros to receive help on the zeros function. 3 The import of the identity matrix will be investigated in later activities.
2

Matlab and Matrices

Other types of diagonal matrices are possible with Matlabs diag command. >> E=diag([1,2,3,4,5]) >> F=diag([1,2,3,4,5],-1) >> G=diag(1:5,1)

2.2

The Transpose Operator


The transpose operator (single apostrophe) plays the same role with matrices as it does with vectors. Rows are changed to columns and columns are changed to rows. >> J=[1 2 3;4 5 6;7 8 9] >> J

2.3

Suppressing Output
Remember, appending a semicolon to any Matlab command will suppress the output. This is useful if the result is large and you have no desire to view it. >> K=rand(100);

2.4

Matlabs Workspace
Take a moment to examine Matlabs workspace with the whos command. >> whos Note the size of each of the variables in your workspace is given. Of course, you could also nd the size of the matrix I by entering >> size(I)

Indexing Matrices
The following notation is used to denote a matrix with 3 a11 A = a21 a31 rows and 3 columns. a12 a13 a22 a23 a32 a33

Note that a12 refers to the entry in row 1, column 2. In general, aij refers to the entry in row i, column j. Matlab uses similar notation to identify the elements of a matrix. >> A=pascal(5) >> A(1,2) >> A(3,4) In general, A(i,j) refers to the element in row i, column j of matrix A.

Matlab and Matrices

You can easily change an entry in a matrix. >> A(3,3)=11111

3.1

Advanced Indexing Techniques


When indexing a matrix, your subscripts can also be vectors. This is a powerful tool which allows you to easily select a submatrix of a given matrix. >> A=magic(6) >> A([1,2],[3,4,5]) The notation A([1,2],[3,4,5]) references the submatrix formed using the elements that appear in rows 1 and 2 and in columns 3,4, and 5 of the matrix A. The command >> A([1,3,5],[1,2,3,4,5,6]) produces a submatrix composed of rows 1, 3, and 5 of matrix A. If you recall that the notation 1:6 represents the vector [1,2,3,4,5,6], so that A(1,1:6) is equivalent to A(1,[1,2,3,4,5,6]). >> A(1,1:6) Using a colon by itself in place of a subscript denotes all of the corresponding row or column. Thus, >> A(:,1) produces the rst column of the matrix A, and >> A(3,:) produces the third row of the matrix A. In a sense, you can read the notation A(3,:) as follows: Third row, every column. The command >> A(1:3,:) produces a submatrix composed of the rst three rows of the matrix A, while the command >> A(:,1:2:6) produces a submatrix composed of columns 1, 3, and 5 of matrix A.

Building Matrices
Matlab allows you to build complex matrices out of vectors and matrices.

4.1

Building Matrices From Vectors


Create three row vectors with the following commands.

Matlab and Matrices

>> v1=1:3 >> v2=4:6 >> v3=7:9 The command >> M=[v1;v2;v3] builds a matrix with the vectors v1, v2, and v3 each forming a row of the matrix M. The command >> N=[v1,v2,v3] produces a signicantly dierent but logical result. Change the vectors v1, v2, and v3 into column vectors with the transpose operator. >> v1=v1 >> v2=v2 >> v3=v3 The command >> P=[v1,v2,v3] builds a matrix with the vectors v1,v2, and v3 each forming a column of the matrix P. Of course, you could have achieved the same result by taking the transpose of the matrix M. >> M

4.2

The Dimensions Need To Match


When building matrices, be sure that each row and column has the same number of elements. For example, the following sequence of commands will produce an error. >> w1=1:3,w2=4:6,w3=7:10 >> Q=[w1;w2;w3]

4.3

Building Matrices From Other Matrices


It is a simple matter to augment a row or column vector to a matrix. Remember, each row and column of a matrix must have the same number of elements. Thus, >> A=[1,2,3,4;5,6,7,8;9,10,11,12] >> b=[1,1,1] >> M=[A,b] is valid, but >> M=[A;b]

Matlab and Matrices

is not. You can also augment two or more matrices. Remember, each row and column of a matrix must have the same number of elements. Thus, >> A=magic(3),B=ones(3,4) >> M=[A,B] is valid, but >> N=[A;B] is not.

4.4

Your Imagination Is Your Only Limit


Matlabs matrix building capability is extremely exible. Here is an interesting example. >> A=zeros(3),B=ones(3),C=2*ones(3),D=3*ones(3) >> M=[A,B;C,D] Here is something called a Vandermonde matrix. >> x=[1,2,3,4,5] >> N=[ones(size(x)),x,x.2,x.3,x.4] And here is one nal piece of chicanery. >> B=zeros(8) >> B(1:3,1:3)=[1 2 3;4 5 6;7 8 9] >> B(4:8,4:8)=magic(5)

Homework
Answer questions 1-11 on regular notebook paper. Be sure to test your responses at the computer. 1. Write the Matlab commands that generate each of the following matrices. 1 1 3 a. 3 1 6 4 4 10 0 8 7

10 5 6 5 8 4 b. 5 10 3 8 8 5 2. 3. Write a simple command that will create a 3 5 matrix where each entry is the number 3. Craft a Hilbert matrix with the following commands.

Matlab and Matrices

>> format rat >> H=hilb(5) >> format Develop a formula for the entry in row i, column j of the matrix H. That is, nd an expression for H(i, j) in terms of i and j. 4. Explain the dierence between the commands floor, ceil, round, and fix. Demonstrate with examples in Matlab. Write a Matlab command that will generate a 4 4 matrix lled with random integers between 5 and 5. Technically, is Matlabs conjugate transpose operator. To see this, enter the matrix A=[1, 2+i, sqrt(2); 3, 3-i, sqrt(-1)], then type A. Describe the result. What happens when you enter A.? Explain the dierence between the transpose operators and .. What is the entry in row 5 column 7 of matrix pascal(10)? What Matlab commands do you use to nd this entry? Let T=toeplitz(1:7). Write a Matlab command that will a. b. c. 9. place the even rows of matrix T into the matrix B. place the odd columns of matrix T in matrix C. place the last column of the matrix T in the vector b.

5. 6.

7.

8.

The system of equations x1 + 2x2 3x3 = 4 2x1 3x3 = 2 x2 + x3 = 0 has coecient matrix and answer vector

1 A = 2 0

2 3 0 3 1 1

4 and b = 2 , 0

respectively. Form the augmented matrix M=[A,b] and place the augmented matrix in reduced row echelon form with the command rref(M). 10. Let x=(0:pi/2:2*pi). What Matlab commands will create a matrix whose rst row is x, whose second row is the sine of each entry in x, and whose third row is the cosine of each entry in x? 11. Let x=linspace(0,10). What Matlab commands will create a matrix whose rst column is x, whose second column is the square of each entry in x, and whose third column is the reciprocal of each entry in x?

You might also like