Difference between revisions of "Scilab/C2/Matrix-Operations/English"

From Script | Spoken-Tutorial
Jump to: navigation, search
(Created page with ''''Title of script''': '''Matrix Operations''' Author:Anuradha A,Shalini Shrivasta. Keywords: {| style="border-spacing:0;" ! ! Narration |- | style="border-top:none;borde…')
 
 
(One intermediate revision by the same user not shown)
Line 8: Line 8:
  
 
{| style="border-spacing:0;"
 
{| style="border-spacing:0;"
!  
+
! Visual Cue
 
! Narration
 
! Narration
  

Latest revision as of 09:59, 13 June 2014

Title of script: Matrix Operations

Author:Anuradha A,Shalini Shrivasta.

Keywords:


Visual Cue Narration
Slide 1

[Title slide]

Welcome to the spoken tutorial on Matrix Operations.
Slide 2

[Objectives slide]

At the end of this spoken tutorial, you will be able to:

1. Access the elements of Matrix

2. Determine 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.

slide 3

[Prerequisites]


The preequisites are
  1. Scilab should be installed on your system.
  1. You should have listened to the Spoken Tutorial: Getting started with Scilab and Vector Operations.
  1. I am using Windows 7 operating system and Scilab 5.2.2 for demonstration.


Launch Scilab 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 intervals of time.

Scilab Console Recall that in the Spoken Tutorial, 'Vector Operations', matrix E was defined as


E = [5 19 15;8 22 36]

and press enter

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) and press enter

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,:) and press enter

E1 =


    1. 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 be obtained using the following command:


-->E2 = E(:,2:3) close the bracket and press enter


E2 =


19. 15.

22. 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(:,$) close the bracket and press enter


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, Vector Operations, we had defined

A as


A=[1 2 - 1; - 2 - 6 4; - 1 - 3 3] close the square bracket and press enter


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) gives the inverse of A

ans =


3. 1.5 - 1.

- 1. - 1. 1.

0. - 0.5 1.

--> spec(A) gives the eigen values of 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.

  1. 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) and press enter

ans =


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) gives a matrix of all ones

ans =


1. 1. 1. 1.

1. 1. 1. 1.


It is easy to create an identity matrix using “eye” command:


-->eye(4,4) gives a 4 by 4 identity matrix


ans =

1. 0. 0. 0.

0. 1. 0. 0.

0. 0. 1. 0.

0. 0. 0. 1.


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) and press enter


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,Vector Operations, we had defined the matrix P as follows.


P = [1 2 3;4 11 6] close the square bracket and press enter


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,:) and press enter

P =


1. 2. 3.

  1. 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]] close both the square bracket and press enter

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] close the square bracket and press enter

A =


1. 2. - 1.

- 2. - 6. 4.

- 1. - 3. 3.


-->b = [1;-2;1] close the square bracket and press enter

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 and press enter.

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.


Exercise 2:

1. Define a 3x3 matrix A with all elements equal to 1. Multiply 1st 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.

There are many other functions in Scilab which will be covered in other spoken tutorials. Keep watching the Scilab links.

Summary:

In this tutorial we have learnt

  1. To access the element of the matrix using the colon operator
  2. Calculate the inverse of a matrix using the 'inv' command or by backslash
  3. Calculate the derterminant of matrix using 'det' command.
  4. Calculate eigen values of a matrix using 'spec' command.
  5. Define a matrix having all the elements one, Null Matrix, Identity matrix and a matrix with random elements by using functions ones(), zeros(), eye(), rand() respectively
  6. Solve the system of linear equations.
  • This spoken tutorial has been created by the Free and Open Source Software in Science and Engineering Education(FOSSEE).

This is Anuradha Amrutkar from IIT Bombay signing off.

Thank you for joining. Goodbye.


Contributors and Content Editors

Chandrika, PoojaMoolya