
Introduction to MATLAB and Linear Algebra Ralph C. Smith Department of Mathematics North Carolina State University Overview: 1. MATLAB examples throughout tutorial 2. Elements of linear algebra • Fundamental properties of vectors and matrices • Eigenvalues, eigenvectors and singular values Linear Algebra and Numerical Matrix Theory: • Vector properties including orthogonality • Matrix analysis, inversion and solving Ax = b for very large systems • Eigen- and singular value decompositions Linear Algebra and Numerical Matrix Theory Topics: Illustrate with MATLAB as topics are introduced • Basic concepts • Linear transformations • Linear independence, basis vectors, and span of a vector space • Fundamental Theorem of Linear Algebra • Determinants and matrix rank • Eigenvalues and eigenvectors • Solving Ax = b and condition numbers • Singular value decomposition (SVD) • Cholesky and QR decompositions Linear Algebra has become as basic and as applicable as calculus, and fortunately it is easier. --Gilbert Strang, MIT Vectors and Matrices Note: • Vector in Rn is an ordered æ1ö set of n real numbers. ç ÷ ç6÷ 4 – e.g. v = (1,6,3,4) is in R ç3÷ – “(1,6,3,4)” is a column ç ÷ ç4÷ vector: è ø – as opposed to a row vector: (1 6 3 4) • m-by-n matrix is an object with m rows and n columns, æ1 2 8ö each entry fill with a real ç ÷ number: ç4 78 6÷ ç ÷ è9 3 2ø Vector Norms Vector Norms: The norm ||x|| quantifies the ``length’’ Common Norms: Euclidean: Vector length Vector and Matrix Products Vector products: æ v ö T ç 1 ÷ – Dot product: u • v = u v = (u1 u2 )ç ÷ = u1v1 + u2v2 èv2 ø Note: A× B = A B cos(q ) If u•v=0, ||u||2 != 0, ||v||2 != 0 à u and v are orthogonal If u•v=0, ||u||2 = 1, ||v||2 = 1 à u and v are orthonormal T æ u1 ö æ u1v1 u1v2 ö – Outer product: ç ÷ ç ÷ uv = ç ÷(v1 v2 ) = ç ÷ èu2 ø èu2v1 u2v2 ø Matrix Product: æ a11 a12 ö æb11 b12 ö A = ç ÷, B = ç ÷ èa21 a22 ø èb21 b22 ø æ a11b11 + a12b21 a11b12 + a12b22 ö AB = ç ÷ èa21b11 + a22b21 a21b12 + a22b22 ø Special Matrices æa 0 0ö æa b c ö ç ÷ ç ÷ ç0 b 0÷ diagonal ç0 d e ÷ upper-triangular ç ÷ ç ÷ è0 0 cø è0 0 f ø æa b 0 0ö æ a 0 0 ö ç ÷ ç ÷ ç c d e 0÷ tri-diagonal ç b c 0 ÷ lower-triangular ç0 f g h÷ ç ÷ çd e f ÷ ç ÷ è ø è0 0 i jø æ1 0 0ö ç ÷ ç0 1 0÷ I (identity matrix) ç ÷ è0 0 1ø Special Matrices • Matrix A is symmetric if A = AT • A is positive definite if xTAx>0 for all non-zero x (positive semi-definite if inequality is not strict) æ1 0 0öæaö æ1 0 0öæaö ç ÷ç ÷ 2 2 2 ç ÷ç ÷ 2 2 2 (a b c)ç0 1 0÷çb÷ = a + b + c (a b c)ç0 - 1 0÷çb÷ = a - b + c ç ÷ç ÷ ç ÷ç ÷ è0 0 1øè c ø è0 0 1øè c ø • Useful fact: Any matrix of form ATA is positive semi-definite. To see this, xT(ATA)x = (xTAT)(Ax) = (Ax)T(Ax) ≥ 0 Recall: Covariance matrix V = σ2(X T X)-1 Matrices as Linear Transformations æ5 0öæ1ö æ5ö ç ÷ç ÷ = ç ÷ (stretching) è0 5øè1ø è5ø æ0 - 1öæ1ö æ- 1ö ç ÷ç ÷ = ç ÷ (rotation) è1 0 øè1ø è 1 ø æ0 1öæ1ö æ0ö ç ÷ç ÷ = ç ÷ è1 0øè0ø è1ø (reflection) æ1 0öæ1ö æ1ö ç ÷ç ÷ = ç ÷ è0 0øè1ø è0ø (projection) Linear Independence • A set of vectors is linearly independent if none of them can be written as a linear combination of the others. • Vectors v1,…,vk are linearly independent if c1v1+…+ckvk = 0 implies c1=…=ck=0 æ | | | öæ c ö æ0ö ç ÷ç 1 ÷ ç ÷ çv1 v2 v3 ÷çc2 ÷ = ç0÷ ç ÷ç ÷ ç ÷ è | | | øèc3 ø è0ø e.g. æ1 0ö æ0ö ç ÷æuö ç ÷ (u,v)=(0,0), i.e. the columns ç2 3÷ç ÷ = ç0÷ ç ÷è v ø ç ÷ are linearly independent. è1 3ø è0ø x3 = −2x1 + x2 Basis Vectors • If all vectors in a vector space may be expressed as linear combinations of a set of vectors v1,…,vk, then v1,…,vk spans the space. • The cardinality of this set is the dimension of the vector space. æ2ö æ1ö æ0ö æ0ö ç ÷ ç ÷ ç ÷ ç ÷ e.g. ç2÷ = 2ç0÷ + 2ç1÷ + 2ç0÷ ç ÷ ç ÷ ç ÷ ç ÷ (0,0,1) è2ø è0ø è0ø è1ø (0,1,0) (1,0,0) • A basis is a maximal set of linearly independent vectors and a minimal set of spanning vectors of a vector space Basis Vectors Note: • An orthonormal basis consists of orthogonal vectors of unit length. æ2ö æ1ö æ0ö æ0ö ç ÷ ç ÷ ç ÷ ç ÷ æ2ö æ.9ö æ.3ö æ.1ö 2 = 2 0 + 2 1 + 2 0 ç ÷ ç ÷ ç ÷ ç ÷ ç ÷ ç ÷ ç ÷ ç ÷ ç2÷ = 1.57ç.2÷ + 1.29ç 1 ÷ + 2ç.2÷ ç ÷ ç ÷ ç ÷ ç ÷ ç ÷ ç ÷ ç ÷ ç ÷ è2ø è0ø è0ø è1ø è2ø è 0 ø è 0 ø è 1 ø (0,0,1) (.1,.2,1) (0,1,0) (.3,1,0) (1,0,0) (.9,.2,0) Rank of a Matrix • The rank of A is the dimension of the column space of A. • It also equals the dimension of the row space of A (the subspace of vectors which may be written as linear combinations of the rows of A). æ1 0ö ç ÷ (1,3) = (2,3) – (1,0) ç2 3÷ ç1 3÷ Only 2 linearly independent rows, so è ø rank = 2. Fundamental Theorem of Linear Algebra: If A is m x n with rank r, Column space(A) has dimension r Nullspace(A) has dimension n-r (= nullity of A) Row space(A) = Column space(AT) has dimension r Left nullspace(A) = Nullspace(AT) has dimension m - r Rank-Nullity Theorem: rank + nullity = n Matrix Inverse Note: • To solve Ax=b, we can write a closed-form solution if we can find a matrix A-1 such that AA-1 =A-1A=I (Identity matrix) • Then Ax=b iff x=A-1b: x = Ix = A-1Ax = A-1b • A is non-singular iff A-1 exists iff Ax=b has a unique solution. • Note: If A-1,B-1 exist, then (AB)-1 = B-1A-1, and (AT)-1 = (A-1)T • For orthonormal matrices A-1 = AT Matrix Determinants Note: • If det(A) = 0, then A is singular. • If det(A) ≠ 0, then A is invertible. • To compute: – Simple example: æa b ö detç ÷ = ad - bc è c d ø – Matlab: det(A) MATLAB Interlude Special Variables: • Special variables: – ans : default variable name for the result – pi: p = 3.1415926………… – eps: Î = 2.2204e-016, smallest amount by which 2 numbers can differ. – Inf or inf : ¥, infinity – NaN or nan: not-a-number Vectors: Example: >> x = [ 0 0.25*pi 0.5*pi 0.75*pi pi ] x is a row vector. x = 0 0.7854 1.5708 2.3562 3.1416 >> y = [ 0; 0.25*pi; 0.5*pi; 0.75*pi; pi ] y is a column vector. y = 0 0.7854 1.5708 2.3562 3.1416 Vectors • Vector Addressing – A vector element is addressed in MATLAB with an integer index enclosed in parentheses. • Example: >> x(3) ans = 1.5708 ç 3rd element of vector x • The colon notation may be used to address a block of elements. (start : increment : end) start is the starting index, increment is the amount to add to each successive index, and end is the ending index. A shortened format (start : end) may be used if increment is 1. • Example: >> x(1:3) ans = 0 0.7854 1.5708 ç 1st to 3rd elements of vector x NOTE: MATLAB index starts at 1. Vectors Some useful commands: x = start:end create row vector x starting with start, counting by one, ending at end x = start:increment:end create row vector x starting with start, counting by increment, ending at or before end linspace(start,end,number) create row vector x starting with start, ending at end, having number elements length(x) returns the length of vector x y = x’ transpose of vector x dot (x, y) returns the scalar dot product of the vector x and y. Matrices § A Matrix array is two-dimensional, having both multiple rows and multiple columns, similar to vector arrays: § it begins with [, and end with ] § spaces or commas are used to separate elements in a row § semicolon or enter is used to separate rows. • Matrix Addressing: •Example: -- matrixname(row, column) •>> f = [ 1 2 3; 4 5 6] -- colon may be used in place of a row or column f = reference to select the entire row or column. 1 2 3 4 5 6 n Example: recall: >> f(2,3) f = ans = 1 2 3 6 4 5 6 >> h(:,1) h = ans = 2 4 6 2 1 3 5 1 Matrices more commands Transpose B = A’ Identity Matrix eye(n) è returns an n x n identity matrix eye(m,n) è returns an m x n matrix with ones on the main diagonal and zeros elsewhere. Addition and subtraction C = A + B C = A – B Scalar Multiplication B = aA, where a is a scalar. Matrix Multiplication C = A*B Matrix Inverse B = inv(A), A must be a square matrix in this case. rank (A) è returns the rank of the matrix A. Matrix Powers B = A.^2 è squares each element in the matrix C = A * A è computes A*A, and A must be a square matrix.
Details
-
File Typepdf
-
Upload Time-
-
Content LanguagesEnglish
-
Upload UserAnonymous/Not logged-in
-
File Pages39 Page
-
File Size-