Está en la página 1de 7

Title of script: Matrix Operations Author:Anuradha A,Shalini Shrivasta.

Keywords: Narration Slide 1 [Title slide] Slide 2 [Objectives slide] Welcome to the spoken tutorial on Matrix Operations. At the end of this spoken tutorial, you will be able to: 1. Access the elements of Matrix 2. Find out the determinant, inverse and eigen values of a matrix. 3. Define special matrices. 4. Perform elementary row operations. 5. Solve a system of linear equations using scilab. 1. A machine with Scilab installed. 2. Spoken Tutorial: Getting started with Scilab. 3. Spoken Tutorial: Vector Operations. Start Scilab by double-clicking on the Scilab icon present on the Desktop. It is suggested that the user should practice this tutorial in Scilab simultaneously while pausing the video at regular interval of time. Scilab Console Recall that in the Spoken Tutorial, 'Matrix operation Part 1', matrix E was defined as E = [5 19 15;8 22 36] Let us now see how to address individual elements of a matrix, separately. To access the element in the first row and second column, type: -->E(1,2) ans = 19 It is easy to extract an entire row or an entire column of a matrix in Scilab . For example, first row of E can be obtained using the following command: -->E1 = E(1,:) E1 = 5. 19. 15. The command returns all the elements of the first row in the order of their appearance in the row. Colon, when used alone, refers to all the elements of row or column, depending upon whether it appears as a first or a second entry respectively inside the bracket. Also, any subset of a matrix can be extracted using a colon (:). For example, the set of elements starting from second to third columns of E can

slide 3 [Prerequisites] Launch Scilab

Narration be obtained using the following command: -->E2 = E(:,2:3) E2 = 19. 22. 15. 36.

In the above, the second entry in the bracket, that is, "2 colon 3" makes a reference to elements from column 2 to column 3. If the size of the matrix is not known $ symbol can be used to extarct the last row or column of that matrix. For example to extract all rows of the last column of the matrix E, we will type --->Elastcol = E(:,$) Elastcol = 15. 36.

Now, let us learn how to calculate the determinant of a square matrix using the command det Recall that in the Spoken Tutorial, Matrix Operation Part 1, we had defined A as A=[1 2 - 1; - 2 - 6 4; - 1 - 3 3]

Let us calculate the determinant of A -->det(A) ans = - 2. To calculate the inverse and the eigenvalues of a matrix, the commands, inv and spec respectively, can be used. For example: --> inv(A) ans = 3. 1.5 - 1. - 1. - 1. 1. 0. - 0.5 1.

Narration --> spec(A) ans = - 3.7448261 0.3959319 1.3488942 See 'help spec' to see how eigenvectors can also be obtained using this command. Square or cube of a square matrix A can be calculated by simply typing A^2 or A^3 respectively. A caret symbol is used to raise a matrix to power, like in ordinary arithmetic operations. In our keyboard, it is obtained by pressing shift+6. -->A^2 ans = - 2. - 7. 4. 6. 20. - 10. 2. 7. - 2. -->A^3 ans = 8. 26. - 14. - 24. - 78. 44. - 10. - 32. 20.

Slide 5 [Execise Slide]

Please pause the tutorial now and attempt exercise number one given with the video. A=[1 -1 0 ; 2 3 1 ; 4 1 5 ] 1.Find A(:,:) 2.Extract the second column of A 3. Determine the determinant and eigenvalues of the matrix, A^2+2*A Certain special matrices can also be created in Scilab: For example a matrix of zeros with 3 rows and 4 columns can be created using zeros command -->zeros(3,4) ans =

Narration 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. A matrix of all ones can be created with ones command as follows -->ones(2,4) ans = 1. 1. 1. 1. 1. 1. 1. 1. It is easy to create an identity matrix using eye command: -->eye(4,4) ans = 1. 0. 0. 0. 0. 1. 0. 0. 0. 0. 1. 0. 0. 0. 0. 1. This is a 4 by 4 identity matrix. A user may need a matrix consisting of pseudo random numbers. It can be generated using the rand command as follows: -->p=rand(2,3) p= 0.2113249 0.0002211 0.6653811 0.7560439 0.3303271 0.6283918 In linear systems, one of the important sets of operations a user carries out on matrices are the elementary row and column operations.These operations involve executing row operations on a matrix to make entries below a nonzero number, zero. This can be done easily in Scilab. Recall that in the Spoken Tutorial, Matrix Operation Part 1, we had defined the matrix P as follows. P = [1 2 3;4 11 6] Let us consider an example where the element in the second row, first column is to be transformed to zero using elementary row and column operation. The operation can be executed by multiplying the first row by 4 and subtracting it from the second row as in the following command: -->P(2,:) = P(2,:) - 4*P(1,:)

Narration P = 1. 0. 2. 3. 3. - 6.

The procedure can be extended to larger systems and to other forms of elementary column operations. Rows and columns can be easily appended to matrices. For example, to append a row containing [5 5 -2] to P, the following command is used: -->T = [P; [5 5 -2]] T = 1. 2. 3. 0. - 3. - 6. 5. 5. - 2. The semicolon after P states that the anything after it should go to the next row. This is expected in the way a matrix is defined. As an exercise, please pause here and check if the brackets around the new row, in the command just executed, are really required. Matrix notations are used while solving equations. Let us solve the following set of linear equations: x1 + 2 x2 x3 = 1 2 x1 6 x2 + 4 x3 = 2 x1 3 x2 + 3 x3 = 1 The above set of equations can be written in the Ax = b form. The solution is then given as inverse of A times b Let us solve the set of equations. A is defined as -->A = [1 2 -1;-2 -6 4;-1 -3 3] A = 1. 2. - 1. - 2. - 6. 4. - 1. - 3. 3. -->b = [1;-2;1]

Narration b = 1. - 2. 1. The solution, x, can be obtained using -->x = inv(A)*b x = - 1. 2. 2. It is worth noting that it is a small letter 'i' in the command, 'inv'. Alternatively, the same result can be achieved using a backslash operation in Scilab. Lets do this in Scilab -->x = A\b x = - 1. 2. 2. It gives the same result. Type "help backslash" and "help inv" in Scilab to know more about individual advantages and disadvantages. The integrity of the solution can be verified by back substitution, that is, by calculating Ax-b: -->A*x-b ans = 0. 0. 0. The above exercise verifies the result achieved earlier. It is possible that in some systems the above verification exercise may not yield a matrix with *exact* zeros as its elements due to intermediate floating point operations. However, one will indeed get a very small number, typically of the order of 10-16. Slide 5 Please pause the tutorial now and attempt exercise number two given with the video.

Narration

Exercise 2: 1. Define a 3x3 matrix A with all elements equal to 1. Multiply 1 st and 2nd row with scalars, 3 and 4 respectively, and determine the determinant of the resultant matrix. 2. A = [2 3 1; 4 6 5; 1 3 6] Use a suitable sequence of row operations on A to bring A to upper triangular form. 3. Represent the following linear system as a matrix equation. Solve the system using the inverse method: x + y + 2z w=3 2x + 5y z 9w=-3 2x + y z + 3w=-11 x 3y + 2z + 7w=-5 a) Try solving the above system using the backslash method. b) Verify the solution of part (a).

Slide 7

This brings us to the end of this spoken tutorial on Matrix Operation Part 2. There are many other functions in Scilab which will be covered in other spoken tutorials. Keep watching the Scilab links. This spoken tutorial has been created by the Free and Open Source Software in Science and Engineering Education(FOSSEE). More information on the FOSSEE project could beobtained from http://fossee.in or http://scilab.in Supported by the National Mission on Eduction through ICT, MHRD, Government of India. For more information, visit: http://spoken-tutorial.org/NMEICTIntro This is Anuradha from IIT Bombay signing off. Thank you for joining. Goodbye.

También podría gustarte