In this laboratory session we will learn how to1. Create matrices and vectors.2. Manipulate matrices and create matrices of special types3. Add and multiply matricesPreliminariesThe MATLAB Desktop DisplayThe MATLAB default desktop consists of four windows: the command window, the Current Directory Browser, the Workspace Browser, and the Command History.• The command window is where MATLAB commands are entered and executed.• The Current Directory Browser allows you to view MATLAB and other files and to perform file operations such as opening and editing or searching for files.• The Workspace Browser allows you to view and make changes to the contents of the workspace.• The Command History allows you to view a log of all the commands that have been entered in the command window. To repeat a previous command, just click on the command to highlight it and then double-click to execute it. You can also recall an edit command directly from the command window by using the arrow keys. From the command window, you can use the up arrow to recall previous commands. The commands can then be edited using the left and right arrow keys. Press the Enter key of your computer to execute the edited command.Any of the MATLAB windows can be closed by clicking on the × in the upper right corner of the window. To detach a window from the MATLAB desktop, click on the arrow that is next to the x in the upper right corner of the window.Help Facility MATLAB includes a HELP facility that provides help on all MATLAB features.• helpbrowser: if you type helpbrowser in the command window the MATLAB’s help browserwill open (alternatively you can click on the help button in the toolbar (this is the button with ?Symbol)• help : if you know the exact name of a function, you can get help on it by typing help functioname.For example, typing help help provides help on the function help itself.• look for: If you are looking for a function, use lookfor keyword to get a list of functions with thestring keyword in them. For example, typing lookfor ‘identity matrix’ lists functions (there are two of them) that create identity matrices.If you have never used MATLAB before, we suggest you type demo at the MATLAB prompt. Click on Getting Started with MATLAB and run the file. Then move on to the demo on Working in the Development Environment and the demo on Working with Arrays.2 MATLAB sessions: Laboratory 1Matrices in MATLABEntering matrices in MATLAB is easy. For example, to enter the matrixA =1 2 3 45 6 7 89 10 11 1213 14 15 16type A=[1, 2, 3, 4; 5, 6, 7, 8; 9, 10, 11, 12; 13, 14, 15, 16]or the matrix could be entered one row at a time:A=[ 1 2 3 45 6 7 89 10 11 1213 14 15 16]Once a matrix has been entered, you can edit it. Here are some examples:Input OutputA(1,3) = 5 A =1 2 5 45 6 7 89 10 11 1213 14 15 16Changes the third entry in thefirst row of A to 5C = A(2:3,2:4) C =6 7 810 11 12Submatrix consisting of the entriesin rows 2 and 3 andcolumns 2 through 4A(:,2:3) ans =2 36 710 1114 15Submatrix of A consisting ofall the elements in the secondand third columnsA(4,:) ans =13 14 15 16Fourth row of AE = A([1,3],[2,4]) E =2 410 12Matrix whose entries are thosewhich appear only in the firstand third rows and second andfourth column of AVectorsVectors are special cases of matrices, with just one row or one column. They are entered the same wayas a matrix. For exampleu = [1, 3, 9] produces a row vectorv = [1; 3; 9] produces a column vector.Row vectors of equally spaced points can be generated with MATLAB’s : operation or using thelinspace command.MATLAB sessions: Laboratory 1 3Input Outputx = 2:6x =2 3 4 5 6row vector with integerentries goingfrom 2 to 6x = 1.2:0.2:2x =1.2000 1.4000 1.6000 1.8000 2.0000Row vector withstepsize 0:2x = linspace(1.2,2,5)x =1.2000 1.4000 1.6000 1.8000 2.0000Row vector with 5equally spaced entriesbetween 1:2and 2Generating MatricesWe can also generate matrices by using the built-in MATLAB functions. For example, the command B=rand(4) generates a 4 × 4 matrix whose entries are random numbers between 0 and 1. Here is a list of the mostcommon built-in matrices:rand(m,n) m by n matrix with random numbers between 0 and 1 eye(m,n) m by n matrix with 1’s on the main diagonal zeros(m,n) m by n matrix of zeros
ones(m,n) m by n matrix of onestriu(A) extracts the upper triangular part of the matrix Atril(A) extracts the lower triangular part of the matrix Adiag(v,k) square matrix with the vector v on the kth diagonalThe first four commands above with a single argument, e.g. ones(m), produce a square matrix ofdimension m.More special matrices:There is also a set of built-in special matrices such as magic, hilb, pascal, toeplitz, and vander.The matrix building commands can be used to generate block of partitioned matrices. Here is anexample:Input OutputE=[eye(2),ones(2,3);zeros(2),[1:3; 3:-1:1]]E =1 0 1 1 10 1 1 1 10 0 1 2 30 0 3 2 1Addition and Multiplication of MatricesMatrix arithmetic in MATLAB is straightforward. We can multiply our original matrix A times Bsimply by typing A*B. The sum and difference of A and B are given by A + B and A – B, respectively.The transpose of the real matrix A is given by A’.4 MATLAB sessions: Laboratory 1ExponentiationPowers of matrices are easily generated. The matrix A5 is computed in MATLAB by typing A^5. Wecan also perform operations element-wise by preceding the operand by a period.For instance, if V=[1,2; 3,4], thenInput OutputV^2ans =7 1015 22V.^2ans =1 49 16component-wise exponentiationAppending a row or a columnA row can be easily appended to an existing matrix provided the row has the same length of the rowsof the existing matrix. The same thing goes for the columns. The command A=[A, v] appends thecolumn vector v to the columns of A, while A = [A; u] appends the row vector u to the rows of A.Examples: If A =1 0 00 1 00 0 1, u =[5 6 7], and v =234, then• C = [A; u] produces C =1 0 00 1 00 0 15 6 7, a 4 × 3 matrix• D = [A, v] produces D =1 0 0 20 1 0 30 0 1 4, a 3 × 4 matrix.Deleting a row or columnLet A=[1, 2, 3, 4, 5; 6, 7, 8, 9, 10; 11, 12, 13, 14, 15], thenInput OutputA(2,:) = [] A =1 2 3 4 511 12 13 14 15deletes the 2nd row of matrix AA(:,3:5) = [] A =1 26 711 12deletes the 3rd through 5thcolumns of AA([1,3],:) = [] A =6 7 8 9 10deletes the 1st and 3rd row of AMATLAB sessions: Laboratory 1 5Columnwise Array OperatorsMATLAB has a number of functions that, when applied to either a row or column vector x, returnsa single number. For example, the command max(x) will compute the maximum entry of x , and thecommand sum(x) will return the value of the sum of the entries of x. Other functions of this form aremin, prod, mean. When used with a matrix argument, these functions are applied to each column vectorand the results are returned as a row vector.For example if A =−3 2 5 41 3 8 0−6 3 1 3, thenInput Outputmin(A) ans =-6 2 1 0minimum entry in each columnof Amax(A) ans =1 3 8 4maximum entry in each columnof Asum(A) ans =-8 8 14 7sum of the entries in each columnof Aprod(A) ans =18 18 40 0product of the entries in eachcolumn of AEXERCISESInstructionsYou will need to record the results of your MATLAB session to generate your lab report. Create adirectory (folder) on your computer to save your MATLAB work in. Then use the Current Directoryfield in the desktop toolbar to change the directory to this folder. Now typediary lab1.txtfollowed by the Enter key. Now each computation you make in MATLAB will be save in your directoryin a text file named lab1.txt. When you have finished your MATLAB session you can turn off therecording by typing diary off at the MATLAB prompt. You can then edit this file using your favoritetext editor (e.g. MS Word).Lab Write-up: Now that your diary file is open, enter the command format compact (so that whenyou print out your diary file it will not have unnecessary spaces), and the comment line% MAT 343 MATLAB Assignment # 1Put labels to mark the beginning of your work on each part of each question, so that your edited labwrite-up has the format% Question 1..% Question 2 (a)6 MATLAB sessions: Laboratory 1Final Editing of Lab Write-up: After you have worked through all the parts of the lab assignmentyou will need to edit your diary file.• Remove all typing errors.• Unless otherwise specified, your write-up should contain the MATLAB input commands, the corresponding output,and the answers to the questions that you have written.• If the question asks you to write an M-file, copy and paste the file into your diary file in theappropriate position (after the problem number and before the output generated by the file).• If the question asks for a graph, copy the figure and paste it into your diary file in the appropriateposition. Crop and resize the figure so that it does not take too much space. Use “;” to suppressthe output from the vectors used to generate the graph. Makes sure you use enough points foryour graphs so that the resulting curves are nice and smooth.• Clearly separate all questions. The questions numbers should be in a larger format and in boldface.Preview the document before printing and remove unnecessary page breaks and blank spaces.• Put your name and class time on each page.Important: An unedited diary file without comments submitted as a lab writeup is notacceptable.1. Entering matrices: Enter the following matrices:A =[2 63 9]; B =[1 23 4]; C =[−5 55 3]2. Check some linear algebra rules:(a) Is matrix addition commutative? Compute A + B and then B + A. Are the results thesame?(b) Is matrix addition associative? Compute (A + B) + C and A + (B + C) in the orderprescribed. Are the results the same?(c) Is multiplication with a scalar distributive? Compute (A + B) and A + B, taking= 5 and show that the results are the same.(d) Is multiplication with a matrix distributive? Compute A(B + C) and compare withAB + AC.(e) Matrices are different from scalars!(i) For scalars, ab = ac implies that b = c if a ̸= 0. Is that true for matrices? Check bycomputing AB and AC for the matrices given above.(ii) In general, matrix products do not commute either (unlike scalar products). Check if ABand BA give different results.3. Create matrices with zeros, eye, ones, and triu: Create the following matrices with thehelp of the matrix generation functions zeros, eye , ones, and triu. See the on-line help on thesefunctions if required (i.e. help eye)M =[0 0 00 0 0]; N =5 0 00 5 00 0 5; P =[3 33 3]Q =1 1 10 1 10 0 1:MATLAB sessions: Laboratory 1 74. Create a big matrix with submatrices: The following matrix G is created by putting matricesA, B, and C from Exercise 1, on its diagonal and inserting 2×2 zeros matrices and 2×2 identitymatrices in the appropriate position. Create the matrix using submatrices A, B, C, zeros andeye (that is, you are not allowed to enter the numbers explicitly).G =2 6 0 0 1 03 9 0 0 0 10 0 1 2 0 00 0 3 4 0 01 0 0 0 −5 50 1 0 0 5 35. Manipulate a matrix: Do the following operations on matrix G created above in Problem 4.(a) Extract the first 4×4 submatrix from G and store it in the matrix H, that is, create a matrixH =2 6 0 03 9 0 00 0 1 20 0 3 4by extracting the appropriate rows and columns from the matrix G.(b) Replace G(5,5) with 4.(c) What happens if you type G(:,:) and hit return? Do not include the output in your labreport, but include a statement describing the output in words.What happens if you type G(:) and hit return? Do not include the output in your lab report,but include a statement describing the output in words.(d) What do you get if you type G(7) and hit return? Can you explain how MATLAB got thatanswer? Try G(16) to confirm your answer.(e) What happens if you type G(12,1) and hit return?(f) What happens if you type G(G>5) and hit return? Can you explain how MATLAB got thatanswer? What happens if you type G(G>5) = 100 and hit return? Can you explain howMATLAB got that answer?(g) Delete the last row and the third column of the matrix G.6. See the structure of a matrix: Create a 20 × 20 matrix with the command A = ones(20);Now replace the 10 × 10 submatrix between rows 6:15 and columns 6:15 with zeros. See thestructure of the matrix (in terms of nonzero entries) with the command spy(A).Set the 5 × 5 submatrices in the top right corner and bottom left corner to zeros and see thestructure again.NOTE: Use semicolon to suppress the output for all the matrices in this problem. In your labwriteup include the pictures obtained with the spy command. To include the pictures, open yourdiary file using a word processor such as MS Word then, on the MATLAB figure, select “Edit”and “Copy Figure”, and paste the picture into the Word file. Make sure you crop and resize thepicture so that it does not take up too much space.7. Create a symmetric matrix: Create an upper triangular matrix with the following command:A = diag(1:6) + diag(7:11, 1) + diag(12:15, 2)Make sure you understand how this command works (see the on-line help on diag). Now use theupper off-diagonal terms of A to make A a symmetric matrix with the following command:A = A + triu(A,1)’8 MATLAB sessions: Laboratory 1This command takes the upper triangular part of A above the main diagonal, flips it (transpose),and adds to the original matrix A, thus creating a symmetric matrix A. See the on-line help ontriu.8. Do some cool operations: Create a 10×10 random matrix with the command A = rand(10);Now do the following operations:(a) Multiply all elements of A by 100 and store the result in the matrix A. Then round off allelements of the matrix to integers with the command A = fix(A).(b) Replace all elements of A that are less than 10 with zeros (Hint: see exercise 5(f))(c) Replace all elements of A > 90 with infinity (inf)(d) Extract all 30 ≤ aij ≤ 50 in a vector b, that is, find all elements of A that are between 30and 50 and put them in a vector b. (Hint: the logical operator & (AND) may be useful).
Reviews
There are no reviews yet.