1 / 49

Scilab A Short Introduction – No. 2

Scilab A Short Introduction – No. 2. Math 15 Lecture 8 University of California, Merced. Project #1 – Due 3pm on April 4 th , 2008. Projects can be performed individually or in groups of three, with following rules: Teams turn in one project report and get the same grade.

owena
Download Presentation

Scilab A Short Introduction – No. 2

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. ScilabA Short Introduction – No. 2 Math 15 Lecture 8 University of California, Merced

  2. Project #1 – Due 3pm on April 4th, 2008 • Projects can be performed individually or in groups of three, with following rules: • Teams turn in one project report and get the same grade. • A team consists of at most 3 people—no copying between teams! • Team project report must include a title page, where a team describe each team member’s contribution. • 10% bonus for projects done individually • Individual projects must not be copied from anyone else • No late project will be accepted! Project #1 has been posted at UCMCROP. UC Merced 2

  3. Friendly Reminder • Scilab Extra credit problems – Due April 7th. • You need to submit your work on paper! (Don’t user the dropbox for this extra credit.) • No homework this week! – Because of the project

  4. Any Questions? UC Merced

  5. Course Lecture Schedule

  6. How do you like Scilab so far? Great Tool!

  7. Outline Verybrief introduction Matrix Algebra • Simple Vector operation • Simple Matrix operation • Let’s solve systems of equations!

  8. What are solutions for this system of equations?

  9. Review: Array or Vector • Array (vector) is a collection of numbers ordered by row (row vector) or column (column vector). Below, a is a column vector while b is a row vector. In both cases, the list is ordered, i.e.

  10. Array or vector – cont. • A general n–vector has the form or where the is called an element of the vector.

  11. Addition and Subtraction: • To add (or subtract) two vectors, they both must have the same number elements as row or column vectors. • Then, the elements of the two vectors are simply added together, element by element, to produce the results. That is, for R = A+ B, then • We cannot add these two vectors:

  12. Matrix • A matrix is a collection of numbers ordered by rows and columns. It is customary to enclose the elements of a matrix in parentheses, brackets, or braces. For example, the following is a matrix: • This matrix has two rows and three columns, so it is referred to as a “2 by 3” matrix. The elements of a matrix are numbered in the following way: • First subscript in a matrix refers to the row and the second subscript refers to the column. It is important to remember this convention when matrix algebra is performed.

  13. Matrix – cont. • So you can think that a vector is a special type of matrix that has only one row or one column. • Also you have already been using the concept of Matrix in Excel. Element -B2. Rows Columns

  14. Matrix – cont. • A square matrix has as many rows as it has columns. Matrix Ais square but matrix B is not square: • A symmetric matrix is a square matrix in which for all i and j. Matrix Ais symmetric; matrix B is not symmetric.

  15. Matrix – cont. • An identity matrix is a diagonal matrix with 1s and only 1s on the diagonal. The identity matrix is almost always denoted as I.

  16. Any Questions? UC Merced

  17. Matrix - Addition and Subtraction • To add (or subtract) two matrices, they both must have the same number of rows and they both must have the same number of columns. • The elements of the two matrices are simply added together, element by element, to produce the results. That is, for R = A+ B, then for all i an j. • We cannot add these two matrices:

  18. Matrix Multiplication: • There are several rules for matrix multiplication. • The first concerns the multiplication between a matrix and a scalar. Here, each element in the product matrix is simply the scalar multiplied by the element in the matrix. • That is, for R = aB, then for all i an j. • Thus

  19. Matrix Multiplication – cont.: • The next rule involves the multiplication of a row vector by a column vector. • To perform this, the row vector must have as many columns as the column vector has rows. • For example, is legal. However, is not legal because the row vector has three columns while the column vector has two rows.

  20. Matrix Multiplication – cont.: • The product of a row vector multiplied by a column vector will be a scalar. This scalar is simply the sum of the first row vector element multiplied by the first column vector element plus the second row vector element multiplied by the second column vector element plus the product of the third elements, etc. In algebra, if r = AB, then

  21. Matrix Multiplication – cont.: • Multiplication of two matrices is well-defined only if the number of columns of the left matrix is the same as the number of rows of the right matrix. If A is an m-by-n matrix and B is an n-by-p matrix, then their matrix product AB is the n-by-pmatrix given by:

  22. Matrix Multiplication – cont.: • Elements of the matrix product ABcan be defined as: • Example:

  23. Matrix Multiplication – cont.: • Let’s look at the operation once more:

  24. Matrix Multiplication – cont.: • So that, a general formula for calculating a matrix Multiplication: • Example:

  25. Matrix Multiplication – cont.: • Elements of the matrix product ABcan be defined as: • However, • Note that the product AB can only be formed if the number of rows in B and the number of columns in A are the same. • In general AB and BA are not identical. Cannot be calculated

  26. Matrix Multiplication – cont.: • So that, a general formula for calculating a matrix Multiplication: • Example:

  27. Matrix Transpose: • The transpose of a matrix is denoted by a prime ( A’) or a superscript t or T ( At or AT). The first row of a matrix becomes the first column of the transpose matrix, the second row of the matrix becomes the second column of the transpose, etc. Thus, • The transpose of a row vector will be a column vector, and the transpose of a column vector will be a row vector. The transpose of a symmetric matrix is simply the original matrix.

  28. Matrix Transpose: • The transpose of a matrix is denoted by a prime ( A’) or a superscript t or T ( At or AT). The first row of a matrix becomes the first column of the transpose matrix, the second row of the matrix becomes the second column of the transpose, etc. Thus, • The transpose of a row vector will be a column vector, and the transpose of a column vector will be a row vector. The transpose of a symmetric matrix is simply the original matrix.

  29. Matrix Inverse: • In algebra, the inverse of a number is that number which, when multiplied by the original number, gives a product of 1. Example: • The inverse of 3 is • Hence, the inverse of x is simple 1/x. or, in slightly different notation, x-1. • So, in matrix algebra, the inverse of a matrix is that matrix which, when multiplied by the original matrix, gives an identity matrix, I.

  30. Matrix Inverse- Cont.: • The inverse of a matrix is denoted by the superscript “-1”. Hence, AA-1= A-1A = I • A matrix must be square to have an inverse, but not all square matrices have an inverse. In some cases, the inverse does not exist. Now AB = BA = Iso B = A−1.

  31. Solving a system of equations • Let’s solve the following system of equations: • You can solve by substitution or elimination: • Substitution:

  32. Let’s go back to the beginning and solve this. • You should be able to solve this by substitution or elimination. • Do you want to solve? I don’t. I need something quick and easy.

  33. Before doing it. • We can use Matrix algebra to solve this system of equations. • We can rewrite this with three matrices:

  34. That means that if we can find the inverse matrix of A, we should be able to calculate X. • What is an inverse matrix of A:

  35. Let’s define matrices in Scilab Environment • Define vectors » x = [1; 2; 3] // column x = 1 2 3 » y = [1, 2, 3] // row or y = [1 2 3] y = 1 2 3

  36. Defining Matrices in Scilab • When [ ] is used to set up matrices, either blanks or commas (,) can be used to separate entries in a row. Semicolons (;) are used to begin a new row. -->A=[ 1 3; 2 4] A = 1. 3. 2. 4. -->B=[ 1, 3; 2, 0; -1, 4] B = 1. 3. 2. 0. - 1. 4.

  37. Matrix operations in Scilab • For example, the following is a matrix: • In Scilab, X=[5 8 1;4 0 2] • X(2,3) // element of 2nd row and 3rd column • First subscript in a matrix refers to the row and the second subscript refers to the column. • X(:,3) //All elements in 3rd Column • X(2,:) // All elements in 2nd row. ans = 2 ans = 1 2 ans = 4. 0. 2.

  38. Matrix operations in Scilab – cont. • Addition • Subtraction -->A=[1 2;0 1]; -->B=[0 4;5 1]; -->A+B ans = 1. 6. 5. 2. -->A-B ans = 1. - 2. - 5. 0.

  39. Matrix operations in Scilab – cont. • Matrix Transpose • Matrix Inverse • Example -->A=[1 2;0 1]; -->B=[0 4;5 1]; -->A' ans = 1. 0. 2. 1. -->inv(B) // B^(-1) ans = - 0.05 0.2 0.25 0. -->B*inv(B) ans = 1. 0. 0. 1.

  40. Matrix operations in Scilab – cont. • Matrix Multiplication • Multiply scalars and matrices • Multiply matrices -->A=[1 2;0 1]; -->B=[0 4;5 1]; -->C=[-1;2]; -->5*B ans = 0. 20. 25. 5. -->A*C ans= 3. 2.

  41. Matrix operations in Scilab – cont. • Matrix Multiplication • There is a big difference between A*B and A.*B • A.*B means element-by-element multiplication -->A=[1 2;0 1]; -->B=[0 4;5 1]; -->C=[-1;2]; -->A*B ans = 10. 6. 5. 1. -->A.*B ans = 0. 8. 0. 1.

  42. Matrix operations in Scilab – cont. • Matrix Multiplication • Wrong dimensions for matrix multiplication. • Please pay a special attention to matrix dimensions! -->A=[1 2;0 1]; -->B=[0 4;5 1]; -->C=[-1;2]; -->C*A !--error 10 inconsistent multiplication

  43. Matrix operations in Scilab – cont. • Matrix Multiplication • Wrong dimensions for matrix multiplication. • How about this: -->A=[1 2;0 1]; -->B=[0 4;5 1]; -->C=[-1;2]; -->C*C !--error 10 inconsistent multiplication -->C'*C ans= 5.

  44. Solving System of Equations by Scilab

  45. Solving System of Equations by Scilab -->A=[1 3;2 4]; -->B=[7;10]; -->inv(A)*B ans= 1. 2.

  46. Let’s go back to the beginning and solve this. • First define two matrices:

  47. Let’s go back to the beginning and solve this. • Use the previously defined matrices to solve in Scilab: -->A=[1 2 2 1; -2 -3 4 1; 2 -2 -1 -1; 1 1 -1 -1]; -->B=[6;0;-2;0]; -->A^(-1)*B ans= 1. 1. 1. 1.

  48. Solving this System of Equations -->A=[3 1 -1;2 -1 1;4 2 0] A = 3. 1. - 1. 2. - 1. 1. 4. 2. 0. -->B=[2/3;1;8/3]; -->inv(A)*B ans = 0.3333333 0.6666667 1.

  49. Next Lecture • How to use Editor. • Programming in Scialb • Programming is the basic skill for implementing numerical analysis.

More Related