CHAPTER 2: MATRIX COMPUTATIONS AND LINEAR ALGEBRA
Lecture 2.3: MATLAB linear algebra
Linear systems of m equations for n unknowns:
a11 x1 + a12 x2 + … + a1n xn = b1
a21 x1 + a22 x2 + … + a2n xn = b2
. . .
am1 x1 + am2 x2 + … + amn xn = bm
· m = n: a square system with a square matrix A (it has typically a unique solution)
· m < n: underdetermined system (it has typically infinitely many solutions)
· m > n: overdetermined system (it has typically no solutions)
Example:
A = , b = . The unique solution exists: x1 = 0.2, x2 = -0.8.
MATLAB operations and functions:
· "/", "\": matrix divisions (solutions of a linear system)
A = [ 3 2 ; 1, -1]; b = [-1 ; 1];
x = A \ b % the column-oriented solution of a linear system
dif = A*x – b % check that the solution x is correct
x = 0.2000
-0.8000
dif = 0
0
AA = [3 1; 2,-1]; bb = [-1,1]; % transposed matrices A and b
xx = bb/AA % the row-oriented solution of a linear system
dif = xx*AA-bb % check that the solution x is correct
xx = 0.2000 -0.8000
dif = 0 0
· inv: matrix inversion (B is the inverse of A if B*A = I)
A = [ 3 2 ; 1, -1]; B = inv(A), BB = A^(-1) % two equivalent operations
I1 = B*A, I2 = A*B % check that B is the inverse of A
B = 0.2000 0.4000
0.2000 -0.6000
BB = 0.2000 0.4000
0.2000 -0.6000
I1 = 1.0000 -0.0000
0 1.0000
I2 = 1.0000 0
-0.0000 1.0000
· det: computes the matrix determinant (det A = a11*a22 – a12*a21 for a 2-by-2 matrix A)
· trace: computes the sum of the diagonal elements of a matrix (the trace)
· rank: computes the number of linearly independent rows or columns of a matrix (the rank)
D = det(A), T = trace(A), R = rank(A)
D = -5
T = 2
R = 2
% Alternative computations of solutions of linear systems via A^(-1)
A = [ 3 2 ; 1, -1]; b = [-1 ; 1];
x = inv(A)*b % it is less efficient because of larger computational time
x = 0.2000
-0.8000
% Alternative computations of solutions by using the Cramer's rule
A1 = [ b, A(:,2) ], A2 = [ A(:,1), b], clear x
x(1) = det(A1)/det(A), x(2) = det(A2)/det(A)
A1 = -1 2
1 -1
A2 = 3 -1
1 1
x = 0.2000
x = 0.2000 -0.8000
· rref: computes the row reduced echelon form of a matrix
· null: computes a basis of eiegenvectors for the homogeneous equation A*x = 0
· eig: computes eigenvalues and eigenvectors of the linear problem A*x = *x
% Alternative computations of solutions by using the augmented matrix [A,b]
Rrow = rref([A,b])
Rrow = 1.0000 0 0.2000
0 1.0000 -0.8000
[V,D] = eig(A)
% D: diagonal matrix of eigenvalues
% V: fundamental matrix of eigenvectors
% V^(-1)*A*V = D: diagonalization formula
Dif = V^(-1)*A*V - D
V = 0.9757 -0.4100
0.2193 0.9121
D = 3.4495 0
0 -1.4495
Dif = 1.0e-015 *
-0.8882 0.2220
0.2776 0
N = null(A) % null-space is the basis of eigenvectors for zero eigenvalues
N = Empty matrix: 2-by-0
· flipud: returns a matrix with columns preserved and rows flipped in the up/down direction
· fliplr: returns a matrix with row preserved and columns flipped in the left/right direction
· flipdim: returns a matrix with flipped dimensions (1 – rows, 2 – columns)
A = [ 1 2 3 4 ; 2 1 3 4 ; 3 2 1 4 ]
A1 = flipud(A)
A2 = fliplr(A)
A3 = flipdim(A,1)
A4 = flipdim(A,2)
A =
1 2 3 4
2 1 3 4
3 2 1 4
A1 =
3 2 1 4
2 1 3 4
1 2 3 4
A2 =
4 3 2 1
4 3 1 2
4 1 2 3
A3 =
3 2 1 4
2 1 3 4
1 2 3 4
A4 =
4 3 2 1
4 3 1 2
4 1 2 3
Types of solutions of linear systems:
· A linear square system of linear equations has a unique solution iff det(A) 0.
· When det(A) = 0, the linear system may have infinitely many solutions or no solutions at all.
· If det(A) 0, the inverse matrix A^(-1) exists and A is called a non-singular matrix.
· If det(A) = 0, inv(A) does not exist and A is called a singular matrix. The MATLAB linear algebra solvers A\b or b/A do not produce any valuable solution, if A is singular.
A1 = [ -1,1; -2,2]; b1 = [1 ; 0];
% the system is inconsistent and has no solution
x1 = A1\b1, B1 = inv(A1), D1 = det(A1)
Warning: Matrix is singular to working precision.
x1 = Inf
Inf
Warning: Matrix is singular to working precision.
B1 = Inf Inf
Inf Inf
D1 = 0
A2 = [ -1,1; -2,2]; b2 = [1 ; 2];
% the second equation is redundant
% the system has infinitely many solutions: x(2) = 1 + x(1)
x2 = A2\b2
A3 = [ -1, 1 ]; b3 = [ 1 ]; % the second equation is removed
x3 = A3\b3
Warning: Matrix is singular to working precision.
x2 = Inf
Inf
x3 = -1
0
% comparison of properties of non-singular matrix A versus singular matrix A1
S1 = rref(A) % non-singular matrix has the identity matrix in RREF
S2 = rref(A1) % a singular matrix has zeros in one or more rows in RREF
S1 = 1 0
0 1
S2 = 1 -1
0 0
R1 = rank(A1) % singular matrices have rank smaller than 2
N1 = null(A1) % the null-space of singular matrices is non-empty
[V1,D1] = eig(A1) % singular matrices have zero eigenvalues
R1 = 1
N1 = 0.7071
0.7071
V1 = -0.7071 -0.4472
-0.7071 -0.8944
D1 = 0 0
0 1
Rrow1 = rref([A2,b2]) % accurate solution of the system with singular matrices
Rrow1 = 1 -1 -1
0 0 0