MATLAB Linear Algebra
MATLAB 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
Common Norms:
æ u1 ö æ u1v1 u1v2 ö
– Outer product: uv = çç ÷÷(v1 v2 ) =
T
çç ÷÷
è u2 ø è u2 v1 u2 v2 ø
Matrix Product:
æ 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÷
çb c 0 ÷ lower-triangular
ç0 ÷ tri-diagonal
ç
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 ö
ç ÷ç ÷ ç ÷ç ÷
(a b c )ç 0 1 0 ÷ç b ÷ = a 2 + b 2 + c 2 (a b c )ç 0 - 1 0 ÷ç b ÷ = a 2 - b 2 + c 2
ç 0 0 1 ÷ç c ÷ ç 0 0 1 ÷ç c ÷
è øè ø è øè ø
2
V = (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
x3 = −2x1 + x2
Basis Vectors
(0,1,0)
(1,0,0)
Note:
• An orthonormal basis consists of orthogonal vectors of unit length.
(0,1,0)
(.3,1,0)
(1,0,0) (.9,.2,0)
Rank of a Matrix
– 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
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
Example:
n
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.
Determinant det (A), and A must be a square matrix.
A, B, C are matrices, and m, n, a are scalars.
Matrices
>> A*B
ans = >> A*B
6 2 ans =
14 4
>> A.*B
ans =
2 0
6 4
Eigenvalues and Eigenvectors
Note:
• How can we characterize matrices?
• The solutions to Ax = λx in the form of eigenpairs (λ,x) = (eigenvalue,eigenvector)
where x is non-zero
• To solve this, (A – λI)x = 0
• λ is an eigenvalue iff det(A – λI) = 0
Example:
æ1 4 5 ö
ç ÷
A = ç0 3 / 4 6 ÷
ç0 0 1 / 2 ÷ø
è
æ1 - l 4 5 ö
ç ÷
det( A - lI ) = ç 0 3/ 4 - l 6 ÷ = (1 - l )(3 / 4 - l )(1 / 2 - l )
ç 0 0 1 / 2 - l ÷
è ø
l = 1, l = 3 / 4, l = 1 / 2
Eigenvalues and Eigenvectors
Example:
æ 2 0ö Eigenvalues λ = 2, 1 with
A = çç ÷÷
è 0 1 ø eigenvectors (1,0), (0,1)
Eigenvectors of a linear transformation A are not rotated (but will be
scaled by the corresponding eigenvalue) when A is applied.
(0,1)
Av
v
(1,0) (2,0)
Important Results:
• Eigenvalues of nxn symmetric matrix are real but may not be distinct. There
are n orthogonal eigenvectors.
Eigenvalues and Eigenvectors
Important Results:
• Eigenvalues of nxn symmetric matrix are real but may not be distinct. There
are n orthogonal eigenvectors; e.g., Identity matrix
• Eigenvalues of a positive definite matrix are positive.
• MATLAB Command: [V,D] = eig(A)
• Suppose A has n linearly independent eigenvectors. Then
Q = [v1 v2 · · · vn ]
) AQ = [ 1 v1 2 v2 ··· n vn ]
) AQ = Q ⇤
) A = Q ⇤Q -1
) Q -1 AQ = ⇤
Note:
• The matrix Q thus provides a new basis for A.
Eigenvalues and Eigenvectors
Example:
>>Q*D*inv(Q)
>> A = [2 1 3;-2 0 -2;5 5 2]
ans =
A=
>> Q’
>> X = A'*A
ans =
X=
>> inv(Q)
Q,D] = eig(X)
ans =
Q
D=
0
Eigenvalues and Eigenvectors
Cholesky Decomposition: Matrix must be positive definite
>> A = [2 1 3;-2 0 -2;5 5 2]
A=
>> X = A'*A
X=
>> R = chol(X)
R=
>> R'*R
ans =
Solving Ax = b
Notes:
• MATLAB: x = A\b;
• How stable is the solution?
• If A or b are changed slightly, how much does it effect x?
• The condition number c of A measures this:
c = λmax/ λmin
• Values of c near 1 are good.
>> cond(A)
ans =
e+10>>
MATLAB
Ax = b
MATLAB
If Conditional:
a = zeros(3); b = zeros(3);
for j=1:3
for i=1:3
a(i,j) = rand; % use rand to generate a random number
if a(i,j) > 0.5
b(i,j) = 1;
end
end
end
MATLAB
Cell Arrays:
>> t = 0:pi/100:2*pi;
>> y = sin(t);
>> plot(t,y)
>> xlabel(‘t’);
>> ylabel(‘sin(t)’);
>> title(‘The plot of t vs sin(t)’);
Plotting
Customizing Graphical Effects
>> Z = peaks; % generate data for plot; peaks returns function values
>> surf(Z) % surface plot of Z
é1 - 1ù
Let A = êê 0 1 úú
êë 1 0 úû
Thus m=3, n=2. Its SVD is
é 0 2/ 6 1/ 3 ùé 1 0 ù
ê úê ú é1 / 2 1/ 2 ù
ê1 / 2 -1/ 6 1 / 3 ú ê0 3úê ú
ê1 / 2 ú ë1/ 2 -1/ 2 û
ë 1/ 6 - 1 / 3 û êë 0 0 úû