Using MATLAB. Linear Algebra

Similar documents
Math 307 Learning Goals. March 23, 2010

3. Array and Matrix Operations

Math 307 Learning Goals

January 18, 2008 Steve Gu. Reference: Eta Kappa Nu, UCLA Iota Gamma Chapter, Introduction to MATLAB,

Review of Linear Algebra

(Mathematical Operations with Arrays) Applied Linear Algebra in Geoscience Using MATLAB

MATH.2720 Introduction to Programming with MATLAB Vector and Matrix Algebra

A Review of Linear Algebra

SECTION 2: VECTORS AND MATRICES. ENGR 112 Introduction to Engineering Computing

Introduction to Matrices

Identity Matrix: EDU> eye(3) ans = Matrix of Ones: EDU> ones(2,3) ans =

This appendix provides a very basic introduction to linear algebra concepts.

Note: The command name is upper case in the description given by help, but must be lower case in actual use. And the backslash Anb is dierent when A i

Reduction to the associated homogeneous system via a particular solution

Math 1553, Introduction to Linear Algebra

TOPIC 2 Computer application for manipulating matrix using MATLAB

Stability of the Gram-Schmidt process

Linear Algebra Using MATLAB

EE731 Lecture Notes: Matrix Computations for Signal Processing

Index. book 2009/5/27 page 121. (Page numbers set in bold type indicate the definition of an entry.)

632 CHAP. 11 EIGENVALUES AND EIGENVECTORS. QR Method

Knowledge Discovery and Data Mining 1 (VO) ( )

Glossary of Linear Algebra Terms. Prepared by Vince Zaccone For Campus Learning Assistance Services at UCSB

Linear Algebra. The analysis of many models in the social sciences reduces to the study of systems of equations.

Basic Linear Algebra in MATLAB

(Linear equations) Applied Linear Algebra in Geoscience Using MATLAB

POLI270 - Linear Algebra

Linear Algebra. Matrices Operations. Consider, for example, a system of equations such as x + 2y z + 4w = 0, 3x 4y + 2z 6w = 0, x 3y 2z + w = 0.

Stat 159/259: Linear Algebra Notes

Matrices and Linear Algebra

1. What is the determinant of the following matrix? a 1 a 2 4a 3 2a 2 b 1 b 2 4b 3 2b c 1. = 4, then det

CITS2401 Computer Analysis & Visualisation

Mobile Robotics 1. A Compact Course on Linear Algebra. Giorgio Grisetti

CS 246 Review of Linear Algebra 01/17/19

Introduction to Mobile Robotics Compact Course on Linear Algebra. Wolfram Burgard, Bastian Steder

Hands-on Matrix Algebra Using R

Appendix A: Matrices

22.3. Repeated Eigenvalues and Symmetric Matrices. Introduction. Prerequisites. Learning Outcomes

MTH 464: Computational Linear Algebra

Chapter 16 Numerical Linear Algebra

+ MATRIX VARIABLES AND TWO DIMENSIONAL ARRAYS

LINEAR ALGEBRA SUMMARY SHEET.

FF505 Computational Science. Matrix Calculus. Marco Chiarandini

Eigenvalues and Eigenvectors

G1110 & 852G1 Numerical Linear Algebra

We wish to solve a system of N simultaneous linear algebraic equations for the N unknowns x 1, x 2,...,x N, that are expressed in the general form

PENN STATE UNIVERSITY MATH 220: LINEAR ALGEBRA

Lecture 3: Special Matrices

Linear Algebra Review. Vectors

Chapter 5 Eigenvalues and Eigenvectors

Linear Algebra, Vectors and Matrices

Cayley-Hamilton Theorem

Numerical Methods Lecture 2 Simultaneous Equations

Quantum Computing Lecture 2. Review of Linear Algebra

2. Every linear system with the same number of equations as unknowns has a unique solution.

Introduction to Matlab

Linear Algebra and Matrices

MATH2210 Notebook 2 Spring 2018

MATRICES. knowledge on matrices Knowledge on matrix operations. Matrix as a tool of solving linear equations with two or three unknowns.

Chapter 9: Gaussian Elimination

MATH 304 Linear Algebra Lecture 34: Review for Test 2.

Linear Algebra Primer

AMS10 HW7 Solutions. All credit is given for effort. (-5 pts for any missing sections) Problem 1 (20 pts) Consider the following matrix 2 A =

Conceptual Questions for Review

Examples and MatLab. Vector and Matrix Material. Matrix Addition R = A + B. Matrix Equality A = B. Matrix Multiplication R = A * B.

L3: Review of linear algebra and MATLAB

Math for ML: review. ML and knowledge of other fields

(b) If a multiple of one row of A is added to another row to produce B then det(b) =det(a).

YORK UNIVERSITY. Faculty of Science Department of Mathematics and Statistics MATH M Test #1. July 11, 2013 Solutions

Introduction to Mobile Robotics Compact Course on Linear Algebra. Wolfram Burgard, Cyrill Stachniss, Maren Bennewitz, Diego Tipaldi, Luciano Spinello

MAT 343 Laboratory 3 The LU factorization

Linear Algebra (Review) Volker Tresp 2017

Columbus State Community College Mathematics Department Public Syllabus

Math Assignment 3 - Linear Algebra

Contents Software Systems for Linear Algebra Problems 531

Introduction to Mobile Robotics Compact Course on Linear Algebra. Wolfram Burgard, Cyrill Stachniss, Kai Arras, Maren Bennewitz

Quiz ) Locate your 1 st order neighbors. 1) Simplify. Name Hometown. Name Hometown. Name Hometown.

(a) II and III (b) I (c) I and III (d) I and II and III (e) None are true.

Eigenvalues and Eigenvectors

CITS2401 Computer Analysis & Visualisation

University of British Columbia Math 307, Final

Background Mathematics (2/2) 1. David Barber

MOL410/510 Problem Set 1 - Linear Algebra - Due Friday Sept. 30

Linear Algebra (Review) Volker Tresp 2018

Linear Algebra. and

We use the overhead arrow to denote a column vector, i.e., a number with a direction. For example, in three-space, we write

Linear Algebra, part 3 QR and SVD

A TOUR OF LINEAR ALGEBRA FOR JDEP 384H

Lecture 7. Econ August 18

B553 Lecture 5: Matrix Algebra Review

MATLAB BASICS. Instructor: Prof. Shahrouk Ahmadi. TA: Kartik Bulusu

2. Linear algebra. matrices and vectors. linear equations. range and nullspace of matrices. function of vectors, gradient and Hessian

EE/ACM Applications of Convex Optimization in Signal Processing and Communications Lecture 2

Chap 3. Linear Algebra

Matrix Algebra. Matrix Algebra. Chapter 8 - S&B

CSL361 Problem set 4: Basic linear algebra

Math for ML: review. CS 1675 Introduction to ML. Administration. Lecture 2. Milos Hauskrecht 5329 Sennott Square, x4-8845

AMS526: Numerical Analysis I (Numerical Linear Algebra)

MTH Linear Algebra. Study Guide. Dr. Tony Yee Department of Mathematics and Information Technology The Hong Kong Institute of Education

Introduction to Matrix Algebra

Transcription:

Using MATLAB in Linear Algebra Edward Neuman Department of Mathematics Southern Illinois University at Carbondale One of the nice features of MATLAB is its ease of computations with vectors and matrices. In this tutorial the following topics are discussed: vectors and matrices in MATLAB, solving systems of linear equations, the inverse of a matrix, determinants, vectors in n-dimensional Euclidean space, linear transformations, real vector spaces and the matrix eigenvalue problem. Applications of linear algebra to the curve fitting, message coding and computer graphics are also included. 3.1 Special characters and MATLAB functions For the reader's convenience we include lists of special characters and MATLAB functions that are used in this tutorial. Special characters ; Semicolon operator Conjugated transpose. Transpose * Times. Dot operator ^ Power operator [ ] Empty vector operator : Colon operator = Assignment == Equality \ Backslash or left division / Right division i, j Imaginary unit ~ Logical not ~= Logical not equal & Logical and Logical or { } Cell

2 Function acos axis char chol cos cross det diag double eig eye fill fix fliplr flops grid hadamard hilb hold inv isempty legend length linspace logical magic max min norm null num2cell num2str ones pascal plot poly polyval rand randn rank reff rem Description Inverse cosine Control axis scaling and appearance Create character array Cholesky factorization Cosine function Vector cross product Determinant Diagonal matrices and diagonals of a matrix Convert to double precision Eigenvalues and eigenvectors Identity matrix Filled 2-D polygons Round towards zero Flip matrix in left/right direction Floating point operation count Grid lines Hadamard matrix Hilbert matrix Hold current graph Matrix inverse True for empty matrix Graph legend Length of vector Linearly spaced vector Convert numerical values to logical Magic square Largest component Smallest component Matrix or vector norm Null space Convert numeric array into cell array Convert number to string Ones array Pascal matrix Linear plot Convert roots to polynomial Evaluate polynomial Uniformly distributed random numbers Normally distributed random numbers Matrix rank Reduced row echelon form Remainder after division

3 reshape roots sin size sort subs sym tic title toc toeplitz tril triu vander varargin zeros Change size Find polynomial roots Sine function Size of matrix Sort in ascending order Symbolic substitution Construct symbolic bumbers and variables Start a stopwatch timer Graph title Read the stopwatch timer Tioeplitz matrix Extract lower triangular part Extract upper triangular part Vandermonde matrix Variable length input argument list Zeros array 3.2 Vectors and matrices in MATLAB The purpose of this section is to demonstrate how to create and transform vectors and matrices in MATLAB. This command creates a row vector a = [] a = Column vectors are inputted in a similar way, however, semicolons must separate the components of a vector b = [1;2;3] b = 1 2 3 The quote operator' is used to create the conjugate transpose of a vector (matrix) while the dot-quote operator.' creates the transpose vector (matrix). To illustrate this let us form a complex vector a + i*b' and next apply these operations to the resulting vector to obtain (a+i*b')' 1.0000-1.0000i 2.0000-2.0000i 3.0000-3.0000i

4 while (a+i*b').' 1.0000 + 1.0000i 2.0000 + 2.0000i 3.0000 + 3.0000i Command length returns the number of components of a vector length(a) 3 The dot operator. plays a specific role in MATLAB. It is used for the componentwise application of the operator that follows the dot operator a.*a 1 4 9 The same result is obtained by applying the power operator ^ to the vector a.^2 1 4 9 Componentwise division of vectors a and b can be accomplished by using the backslash operator \ together with the dot operator. a.\b' 1 1 1 For the purpose of the next example let us change vector a to the column vector a = a a = The dot product and the outer product of vectors a and b are calculated as follows dotprod = a'*b dotprod = 14

5 outprod = a*b' outprod = 2 4 6 3 6 9 The cross product of two three-dimensional vectors is calculated using command cross. Let the vector a be the same as above and let b = [-2 1 2]; Note that the semicolon after a command avoids display of the result. The cross product of a and b is cp = cross(a,b) cp = 1-8 5 The cross product vector cp is perpendicular to both a and b [cp*a cp*b'] 0 0 We will now deal with operations on matrices. Addition, subtraction, and scalar multiplication are defined in the same way as for the vectors. This creates a 3-by-3 matrix [;4 5 6;7 8 10] 4 5 6 7 8 10 Note that the semicolon operator; separates the rows. To extract a submatrix B consisting of rows 1 and 3 and columns 1 and 2 of the matrix A do the following B = A([1 3],[1 2]) B = 1 2 7 8 To interchange rows 1 and 3 of A use the vector of row indices together with the colon operator C = A([3 2 1],:) C =

6 7 8 10 4 5 6 The colon operator: stands for all columns or all rows. For the matrix A from the last example the following command A(:) 1 4 7 2 5 8 3 6 10 creates a vector version of the matrix A. We will use this operator on several occasions. To delete a row (column) use the empty vector operator A(:, 2) = [] 1 3 4 6 7 10 Second column of the matrix A is now deleted. To insert a row (column) we use the technique for creating matrices and vectors [A(:,1) [2 5 8]' A(:,2)] 4 5 6 7 8 10 Matrix A is now restored to its original form. Using MATLAB commands one can easily extract those entries of a matrix that satisfy an imposed condition. Suppose that one wants to extract all entries of that are greater than one. First, we define a new matrix A [-; 0 5 1] - 0 5 1 Command A > 1 creates a matrix of zeros and ones A > 1

7 0 1 1 0 1 0 with ones on these positions where the entries of A satisfy the imposed condition and zeros everywhere else. This illustrates logical addressing in MATLAB. To extract those entries of the matrix A that are greater than one we execute the following command A(A > 1) 2 5 3 The dot operator. works for matrices too. Let now [; 3 2 1] ; The following command A.*A 1 4 9 9 4 1 computes the entry-by-entry product of A with A. However, the following command A*A "??? Error using ==> * Inner matrix dimensions must agree. generates an error message. Function diag will be used on several occasions. This creates a diagonal matrix with the diagonal entries stored in the vector d d = []; D = diag(d) D = 1 0 0 0 2 0 0 0 3 To extract the main diagonal of the matrix D we use function diag again to obtain d = diag(d) d = 1 2 3 What is the result of executing of the following command? diag(diag(d));

8 In some problems that arise in linear algebra one needs to calculate a linear combination of several matrices of the same dimension. In order to obtain the desired combination both the coefficients and the matrices must be stored in cells. In MATLAB a cell is inputted using curly braces{ }. This c = {1,-2, 3} c = [1] [-2] [3] is an example of the cell. 3.3 Solving systems of linear equations MATLAB has several tool needed for computing a solution of the system of linear equations. Let A be an m-by-n matrix and let b be an m-dimensional (column) vector. To solve the linear system Ax = b one can use the backslash operator \, which is also called the left division. 1. Case m = n In this case MATLAB calculates the exact solution (modulo the roundoff errors) to the system in question. Let [; 4 5 6; 7 8 10] 4 5 6 7 8 10 and let b = ones(3,1); Then x = A\b x = -1.0000 1.0000 0.0000 In order to verify correctness of the computed solution let us compute the residual vector r r = b - A*x r = 1.0e-015 *

9 0.1110 0.6661 0.2220 Entries of the computed residual r theoretically should all be equal to zero. This example illustrates an effect of the roundoff erros on the computed solution. 2. Case m > n If m > n, then the system Ax = b is overdetermined and in most cases system is inconsistent. A solution to the system Ax = b, obtained with the aid of the backslash operator \, is the least-squares solution. Let now [2-1; 1 10; 1 2]; and let the vector of the right-hand sides will be the same as the one in the last example. Then x = A\b x = 0.5849 0.0491 The residual r of the computed solution is equal to r = b - A*x r = -0.1208-0.0755 0.3170 Theoretically the residual r is orthogonal to the column space of A. We have r'*a 1.0e-014 * 0.1110 0.6994 3. Case m < n If the number of unknowns exceeds the number of equations, then the linear system is underdetermined. In this case MATLAB computes a particular solution provided the system is consistent. Let now [; 4 5 6]; b = ones(2,1); Then x = A\b

10 x = -0.5000 0 0.5000 A general solution to the given system is obtained by forming a linear combination of x with the columns of the null space of A. The latter is computed using MATLAB function null z = null(a) z = 0.4082-0.8165 0.4082 3.4 The inverse of a matrix MATLAB function inv is used to compute the inverse matrix. Let the matrix A be defined as follows A= [ ; 4 5 6; 7 8 9] Then 4 5 6 7 8 9 B = inv(a) B = -0.6667-0.6667 1.0000-1.3333 3.6667-2.0000 1.0000-2.0000 1.0000 In order to verify that B is the inverse matrix of A it sufficies to show that A*B = I and B* I, where I is the 3-by-3 identity matrix.

11 References [1] B.D. Hahn, Essential MATLAB for Scientists and Engineers, John Wiley & Sons, New York, NY, 1997. [2] D.R. Hill and D.E. Zitarelli, Linear Algebra Labs with MATLAB, Second edition, Prentice Hall, Upper Saddle River, NJ, 1996. [3] B. Kolman, Introductory Linear Algebra with Applications, Sixth edition, Prentice Hall, Upper Saddle River, NJ, 1997. [4] R.E. Larson and B.H. Edwards, Elementary Linear Algebra, Third edition, D.C. Heath and Company, Lexington, MA, 1996. [5] S.J. Leon, Linear Algebra with Applications, Fifth edition, Prentice Hall, Upper Saddle River, NJ, 1998. [6] G. Strang, Linear Algebra and Its Applications, Second edition, Academic Press, Orlando, FL, 1980.