Lecture 4.

Slides:



Advertisements
Similar presentations
Chapter 4 Systems of Linear Equations; Matrices
Advertisements

CS 450: COMPUTER GRAPHICS LINEAR ALGEBRA REVIEW SPRING 2015 DR. MICHAEL J. REALE.
4.3 Matrix Approach to Solving Linear Systems 1 Linear systems were solved using substitution and elimination in the two previous section. This section.
Linear Algebra Applications in Matlab ME 303. Special Characters and Matlab Functions.
Chapter 4 Systems of Linear Equations; Matrices Section 2 Systems of Linear Equations and Augmented Matrics.
1 Systems of Linear Equations & Matrices Sections 4.2 & 4.3 After today’s lesson, you will be able to Use terms associated with matrices. Set up and solve.
SOLVING SYSTEMS OF LINEAR EQUATIONS. Overview A matrix consists of a rectangular array of elements represented by a single symbol (example: [A]). An individual.
Lecture 9: Introduction to Matrix Inversion Gaussian Elimination Sections 2.4, 2.5, 2.6 Sections 2.2.3, 2.3.
Ch 7.2: Review of Matrices For theoretical and computation reasons, we review results of matrix theory in this section and the next. A matrix A is an m.
Chapter 1 Systems of Linear Equations
Linear Equations in Linear Algebra
Vectors and Matrices Class 17.1 E: Ch. 5.
Matrix Mathematics in MATLAB and Excel
Arithmetic Operations on Matrices. 1. Definition of Matrix 2. Column, Row and Square Matrix 3. Addition and Subtraction of Matrices 4. Multiplying Row.
LIAL HORNSBY SCHNEIDER
HAWKES LEARNING SYSTEMS math courseware specialists Copyright © 2011 Hawkes Learning Systems. All rights reserved. Hawkes Learning Systems College Algebra.
1 Chapter 2 Matrices Matrices provide an orderly way of arranging values or functions to enhance the analysis of systems in a systematic manner. Their.
Chapter 7 Matrix Mathematics Matrix Operations Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
5  Systems of Linear Equations: ✦ An Introduction ✦ Unique Solutions ✦ Underdetermined and Overdetermined Systems  Matrices  Multiplication of Matrices.
Table of Contents Solving Systems of Linear Equations - Gaussian Elimination The method of solving a linear system of equations by Gaussian Elimination.
Chapter 2 Systems of Linear Equations and Matrices Section 2.4 Multiplication of Matrices.
1 Systems of Linear Equations & Matrices Sections 4.2 & 4.3 After today’s lesson, you will be able to Use terms associated with matrices. Set up and solve.
Linear Algebra – Linear Equations
Matrix Solution of Linear Systems The Gauss-Jordan Method Special Systems.
1.3 Matrices and Matrix Operations.
Chapter 10 Review: Matrix Algebra
MATRICES AND DETERMINANTS
Systems and Matrices (Chapter5)
Rev.S08 MAC 1140 Module 10 System of Equations and Inequalities II.
Copyright © Cengage Learning. All rights reserved. 7.4 Matrices and Systems of Equations.
Systems of Linear Equation and Matrices
Solving Systems of Equations by matrices
Row rows A matrix is a rectangular array of numbers. We subscript entries to tell their location in the array Matrices are identified by their size.
CHAPTER 2 MATRIX. CHAPTER OUTLINE 2.1 Introduction 2.2 Types of Matrices 2.3 Determinants 2.4 The Inverse of a Square Matrix 2.5 Types of Solutions to.
4.4 & 4.5 Notes Remember: Identity Matrices: If the product of two matrices equal the identity matrix then they are inverses.
CSE123 Lecture 5 Arrays and Array Operations. Definitions Scalars: Variables that represent single numbers. Note that complex numbers are also scalars,
Lesson 13-1: Matrices & Systems Objective: Students will: State the dimensions of a matrix Solve systems using matrices.
Copyright © 2013, 2009, 2005 Pearson Education, Inc. 1 5 Systems and Matrices Copyright © 2013, 2009, 2005 Pearson Education, Inc.
1 1.3 © 2012 Pearson Education, Inc. Linear Equations in Linear Algebra VECTOR EQUATIONS.
1.3 Matrices and Matrix Operations. Definition A matrix is a rectangular array of numbers. The numbers in the array are called the entries in the matrix.
Matrices Addition & Subtraction Scalar Multiplication & Multiplication Determinants Inverses Solving Systems – 2x2 & 3x3 Cramer’s Rule.
1 C ollege A lgebra Systems and Matrices (Chapter5) 1.
Unit 3: Matrices.
Matlab for Engineers Manipulating Matlab Matrices Chapter 4.
1. Copyright © 2006 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Solving Systems of Linear Equations in Three Variables 4 1.Determine.
ES 240: Scientific and Engineering Computation. Chapter 8 Chapter 8: Linear Algebraic Equations and Matrices Uchechukwu Ofoegbu Temple University.
ME 142 Engineering Computation I Matrix Operations in Excel.
Copyright © 2011 Pearson Education, Inc. Solving Linear Systems Using Matrices Section 6.1 Matrices and Determinants.
Slide Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley A set of equations is called a system of equations. The solution.
Chapter 1 Systems of Linear Equations Linear Algebra.
Copyright ©2015 Pearson Education, Inc. All rights reserved.
H.Melikian/12101 System of Linear Equations and Augmented Matrices Dr.Hayk Melikyan Departmen of Mathematics and CS
LEARNING OUTCOMES At the end of this topic, student should be able to :  D efination of matrix  Identify the different types of matrices such as rectangular,
CS 450: COMPUTER GRAPHICS TRANSFORMATIONS SPRING 2015 DR. MICHAEL J. REALE.
Unit 3: Matrices. Matrix: A rectangular arrangement of data into rows and columns, identified by capital letters. Matrix Dimensions: Number of rows, m,
If A and B are both m × n matrices then the sum of A and B, denoted A + B, is a matrix obtained by adding corresponding elements of A and B. add these.
1-2 What is the Matlab environment? How can you create vectors ? What does the colon : operator do? How does the use of the built-in linspace function.
Matrices. Variety of engineering problems lead to the need to solve systems of linear equations matrixcolumn vectors.
Lecture 1 Linear algebra Vectors, matrices. Linear algebra Encyclopedia Britannica:“a branch of mathematics that is concerned with mathematical structures.
Chapter 4 Systems of Linear Equations; Matrices
Linear Algebra Review.
Chapter 7 Matrix Mathematics
MATHEMATICS Linear Equations
5 Systems of Linear Equations and Matrices
Matrix Algebra.
Lial/Hungerford/Holcomb/Mullins: Mathematics with Applications 11e Finite Mathematics with Applications 11e Copyright ©2015 Pearson Education, Inc. All.
Unit 3: Matrices
Matrices.
Matrix Algebra.
Matrices are identified by their size.
Presentation transcript:

Lecture 4

What will I learn in this lecture? How can you create matrices ? How do the back-slash \ and * operators work with matrices? Understand how to subscript matrices. Use the built-in functions eye , ones, zeros and diag to create matrices. Learn about the size function the matrix counterpart to the length vector function Readings: Matlab by Pratap Chapters 2.3.1, 2.3.2, 5.1

Arrays - Vectors and Matrices In Matlab (for CS101) all data will be stored in arrays. An array can represent a: vector - size 1 x n (row vector with 1 row and n columns) or n x 1 (column vector with n rows and 1 column) matrix - any size m x n (m rows and n columns). Conventions Two arrays are defined to be equal if they are of the same size and at each position, they have the same value.

Creating a Matrix m11 = 2 m12 = 3 m21 = 3 m22 = -2 A matrix is like a table with rows (horizontal) and columns (vertical). Specific elements are referenced by the pair (row,column). Example: For a general 2 x 2 matrix the math notation is, And the sub-scripted variables take values such as: m11 = 2 m12 = 3 m21 = 3 m22 = -2 Example: In Matlab the above matrix is created by >> M = [2 3 ; 3 -2]; Note the use of the “ ; “ operator.

Transpose of a Matrix >> M1 = [2 3 ; 4 5] M1 = 2 3 4 5 2 3 4 5 >> M2 = M1’ M2 = 2 4 3 5 The transpose operator ‘ makes the first row of M1 become the first column of M2 and the second row of M1 become the second column of M2.

Creating a Matrix: Using eye,ones and zeros Arrays can be constructed from built-in Matlab functions (see Pratap pp. 70-71). >> A = eye(3) (eye(n) constructs an n x n “identity” matrix) A = 1 0 0 0 1 0 0 0 1 >> A = ones(3,2) (ones(m,n) constructs an m x n array of 1’s) A = 1 1 1 1 >> A = zeros(2,3) (zeros(m,n) constructs an m x n array of 0’s) A = 0 0 0

Creating a Matrix: Using diag Arrays can be constructed from the diag function and a given vector (see 2.9) >> K = [1 2 3]; >> diag(K,0) (diag(K,0) constructs a diagonal matrix with values of K down the diagonal) 1 0 0 0 2 0 (green line represents the “diagonal” of a matrix”) 0 0 3 K = [2 3]; >> diag(K,1) (diag(K,1) constructs a matrix with values of K above the diagonal) 0 2 0 0 0 0

Creating a Matrix: Using diag >> K = [3 4]; >> diag(K,-1) (diag(K,-1) constructs a diagonal matrix with values of K below the diagonal) 0 0 0 3 0 0 0 4 0

repmat - replicate and tile matrix Arrays can be replicated and tiled to make larger matricies.. Example: >> A = [1 2 ; 3 4] A = 1 2 3 4 Create a matrix named B that tiles A in the following manner >> B = repmat(A,3,4) (constructs an 3 x 4 tiling of A) B = 1 2 1 2 1 2 1 2 3 4 3 4 3 4 3 4

Selecting elements from a Matrix - Subscripting Example >> mat1 = [1 -2 ;5 3 ] ; >> mat1(1,1) ( first row ,first column) ans = 1 >> mat1(1,2) ( first row ,second column) -2 >> mat1(2,1) (second row, first column) 5 >> mat1(2,2) (second row, second column) 3

Selecting elements from a Matrix - Subscripting You can select an entire row or column of a matrix by using the colon “ : ” symbol. >> mat1 = [1 -2 3 -4 ; 5 3 7 8 ; 9 10 11 12] mat1 = 1 -2 3 -4 5 3 7 8 9 10 11 12 >> mat1( 1 , : ) % (first row, any column) ans = 1 -2 3 -4 >> mat1( : , 2) % (any row , second column) -2 3 10

Selecting elements from a Matrix - Subscripting >> mat1 = [1 -2 3 -4 ; 5 3 7 8 ; 9 10 11 12] mat1 = 1 -2 3 -4 5 3 7 8 9 10 11 12 >> mat1( end , end ) % (third row, fourth column) ans = 12 >> mat1( [1 2] , [1 4] ) % (first and second row, first and fourth column) ans = 1 -4 5 8

Modifing elements of a Matrix - Subscripting You can use subscripting on a Matlab variable to modify the value of the matrix. Note that the matrix is located on the left hand side of the = . >> mat1 = [1 -2 ; 5 3 ] ; >> mat1(1,:) = 4 mat = 4 4 5 3 >> mat1(2, 2) = 7 mat1 = 5 7 >> mat1(: ,1) = [ -1 -2] (or [-1 -2]’) mat1 = -1 4 -2 7

Arrays - Arithmetic operators + - * / \ ^ .* ./ .\ .^ (dot form) add sub mult right div left div power Rules for the valid operations. If A and B are arrays then it is not always true that >>A op B is defined when op is one of the operations in the table above. The size of an array is its number of rows and columns. We will consider some operators on a case by case basis.

Arrays - Addition >> A = [2 3 4; 5 6 7] ; >> B = 3; A and B must be of the same size or a scalar. >> A = [2 3 4; 5 6 7] ; >> B = 3; >> A + B ans = 5 6 7 8 9 10 >> B = [1 2 3; 4 5 6] ; >> A + B ans = 3 5 7 9 11 13

Arrays - Subtraction >> A = [2 3 4; 5 6 7; 8 9 10] ; A and B must be of the same size or a scalar. >> A = [2 3 4; 5 6 7; 8 9 10] ; >> B = 3; >> A - B ans = -1 0 1 2 3 4 5 6 7 >> B = [1 2 3; 4 5 6; 7 8 9] ; >> A - B ans = 1 1 1

Arrays - (dot) Multiplication The size of A and B must be the same or either could be a scalar . >> A = [2 3 4; 5 6 7] ; >> B = [1 2 3; 4 5 6] ; >> A .* B ans = 2 6 12 20 30 42 Note: the result is the product of the individual elements of the array. For the other operators in the table on slide 4 - 14 , try to determine what the operator does and the sizes for which A op B is valid. We will consider the “ \ “ operator in the future.

Matrix Product The dot or scalar product of two vectors F * s is: To compute the product “ * ” of two matrices A * B we will take the scalar products of the rows of A with the columns of B. If A has size (m x n) and B has size (n x p) then there are m x p possible combinations. In fact A * B is defined only when the number of columns of A equals the number of rows of B.

Matrix Product Given the two matrices (math notation):

Matrix Product C(m x p) = A (m x n) * B (n x p) The product of matrix A and B is another matrix C. The size of C is m x p (the number of combinations of scalar products of the rows of A with the columns of B) C(m x p) = A (m x n) * B (n x p)

Matrix Product The possible combinations of scalar products of rows and columns are: ( c11 = first row of A * first column of B) ( c12 = first row of A * second column of B)

Matrix Product The possible combinations of scalar products of rows and columns are: ( c21 = second row of A * first column of B) ( c22 = second row of A * second column of B)

Arrays - Multiplication Example: >> A = [2 3 4; 5 6 7] ; >> B = [1 2 3; 4 5 6; 7 8 9]; >> C = A * B C = 42 51 60 78 96 114 Note: the size of A * B is computed by: size(A) is 2 x 3 size(B) is 3 x 3 size(A * B) is 2 x 3 Note: In general A * B does not equal B * A.

Arrays - Multiplication Example: >> A = [2 3 4; 5 6 7] ; >> B = [1 ; 2 ; 3] ; >> C = A * B C = 20 38 Note: again, the size of A * B is computed by: size(A) is 2 x 3 size(B) is 3 x 1 size(A * B) is 2 x 1

Linear Systems of Equations Consider the three equations in three unknowns(below): 100x + 10y + z = 1 225x + 15y + z = 20 400x + 20y + z = 10 This system of three equations is called linear since the powers on all the variables are one and each variable occurs separately (i.e. no xy or xyz terms). The plot of each equation in 3D is a plane. If the three planes intersect at one point in 3D space, then what is that point?

Linear Systems of Equations To solve a system of equations A*B = C , i.e. to find the point of intersection of the three planes, use the “ \ ” operator. >> A = [100 10 1; 225 15 1; 400 20 1] ; >> C = [1 ; 20 ; 10] ; >> B = A \ C B = -0.5800 18.3000 -124.0000 You can test this is the solution by typing >>A*B 1.0000 20.0000 10.0000 A is called the “coefficient matrix” for the system of linear equations. The three planes intersect at the point (-0.5800, 18.3000, -124.0000)

\ operator - How It Works If we naively wanted to solve the equation for B given A and C A * B = C (and we forgot that A is a matrix, B and C column vectors), then we might try to solve the above for B by dividing both sides by A. A\(A*B) = A\C where we read the black-slash A\C means A divided into C (A is underneath C). If A\(A*B) = (A\A)*B then since A\A is one we get B = A\C We can use this heuristic to remember how to use back-slash. The “ \ ” back-slash operator uses a combination of numerical methods including LU decomposition.

Problem: Weights suspended by springs ceiling -VVVV- K6 Problem: Weights suspended by springs W5 Hooke’s Law: F = - K*d di is the length of spring Si, Ki is the i-th spring constant, Wi is the i-th weight, hi is i-th vertical position -VVVV- K5 W4 unknowns: H -VVVV- K4 d1 + d2 + d3 + d4 + d5 +d6 = H, height of floor to ceiling. Also, h1 = d1 and hi = d1+d2+ ...+di for i = 2, ... , 5 . From Newton’s second Law: Ki+1*di+1 - Ki*di = Wi for i=1 , ... ,5 . If we can find the di values then we can compute the hi values. W3 K3 -VVVV- W2 -VVVV- K2 W1 -VVVV- K1 floor

For a system with five weights K2*d2 - K1*d1 = W1 K3*d3 - K2*d2 = W2 K4*d4 - K3*d3 = W3 K5*d5 - K4*d4 = W4 K6*d6 - K5*d5 = W5 and d1 + d2 + d3 + d4 + d5 +d6 = H In math notation:

Matlab Solution to the Spring-Weight problem Construct the following vectors. K = [K1 ... K6]; W = [W1 ... W5 H] ’ ; Form the matrix A in three steps 1) create a “diagonal” matrix >> A = diag(-K,0); A now has the form:

Matlab Solution to the Spring-Weight problem Form the matrix A in steps 2) change the bottom row to a row of 1’s. >> A(end, : ) = 1; A now has the form:

Matlab Solution to the Spring-Weight problem Form the matrix A in steps 3) Put in the values above the diagonal. >> A = A + diag( K(2:end) , 1 ); A now has the form:

Matlab Solution to the Spring-Weight problem Solve the linear system A * D = W by using the back-slash operator “ \ ” >> D = A \ W; and finally, since [h1 , h2 h3 , h4 , h5 , h6 ] = [d1 ,d1+d2 ,d1+d2+d3 , d1+d2+d3+d4 , ... ] >> H = cumsum(D);

Function - cumsum The cumsum function returns the cumulative sum of values for a vector. For a matrix, cumsum operates on the columns of the matrix. Example >> p1 = [1 -2 5]; >> mat1 = [1 -2 ;1 3 ; 2 4] ; >> cumsum(p1) ans = 1 -1 4 >> cumsum(mat1) 1 -2 2 1 4 5

Matlab Solution to the Spring-Weight problem

What have I learned in this lecture? Matlab arrays can be vectors or matrices. You can construct matrices using the semicolon operator “ ; ” which means append values to next row. The functions eye, ones, zeros and diag can also be used to create matrices. You can use subscripting on matrices. Use the colon “ : ” to denote all of the row or column. The back-slash operator “ \ ” is used in obtaining solutions to linear systems of equations. The end command can be used in subscripting to return the last row or column of the matrix (or vector).