Linear Algebra
Linear algebra is MATLAB's home ground: it began as an easy interface to the LINPACK and EISPACK matrix libraries. Solving systems of equations, determinants, inverses, eigenvalues and decompositions are all one function call (or one operator) away.
Solving Ax = b
A system of linear equations such as
2x + y - z = 8
-3x - y + 2z = -11
-2x + y + 2z = -3is written as a matrix equation A·x = b, with the coefficients in A and the right-hand sides in the column vector b. The backslash operator solves it:
A = [ 2 1 -1
-3 -1 2
-2 1 2];
b = [8; -11; -3];
x = A \ b;
fprintf('x = %.4f, y = %.4f, z = %.4f\n', x);
residual = norm(A * x - b);
fprintf('residual below 1e-12: %d\n', residual < 1e-12);x = 2.0000, y = 3.0000, z = -1.0000
residual below 1e-12: 1
The solution is x = 2, y = 3, z = −1. The residual A*x - b measures how well the solution satisfies the equations. It is rarely exactly zero, because floating-point arithmetic introduces rounding errors around 10⁻¹⁵, so compare it with a small tolerance.
Tip: Read
A \ bas "A divides into b". It uses Gaussian elimination (LU decomposition) or a better-suited method automatically, and is faster and more accurate than computinginv(A) * b.
Determinant, inverse and rank
| Function | Returns |
|---|---|
det(A) |
Determinant (zero means singular) |
inv(A) |
Inverse matrix, so A * inv(A) is the identity |
rank(A) |
Number of linearly independent rows/columns |
trace(A) |
Sum of the diagonal |
norm(A) |
Matrix 2-norm (or vector length) |
cond(A) |
Condition number: large means sensitive to rounding |
A = [4 7; 2 6];
fprintf('det = %g\n', det(A));
Ainv = inv(A)
I2 = A * Ainv;
fprintf('A*inv(A) equals eye(2) within 1e-12: %d\n', all(abs(I2(:) - [1; 0; 0; 1]) < 1e-12));
fprintf('rank = %d, trace = %d\n', rank(A), trace(A));det = 10
Ainv =
0.6000 -0.7000
-0.2000 0.4000
A*inv(A) equals eye(2) within 1e-12: 1
rank = 2, trace = 10
Singular and ill-conditioned systems
If det(A) is zero, the rows are linearly dependent and the system has no unique solution. rank tells you how many independent equations there really are, and cond warns you when a matrix is almost singular:
S = [1 2; 2 4]; % second row = 2 * first row
fprintf('det = %g, rank = %d\n', det(S), rank(S));
H = [1 1/2 1/3; 1/2 1/3 1/4; 1/3 1/4 1/5];
fprintf('cond of 3x3 Hilbert matrix: %.0f\n', cond(H));
fprintf('cond of identity: %g\n', cond(eye(3)));det = 0, rank = 1
cond of 3x3 Hilbert matrix: 524
cond of identity: 1
Solving with a singular matrix produces a warning and Inf/NaN values; check rank or cond first when your data might be degenerate.
Overdetermined systems: least squares
With more equations than unknowns, there's usually no exact solution. Backslash then returns the least-squares solution, the one that minimises the total squared error. That's exactly how you fit a straight line y = m·x + c to data:
x = [0 1 2 3 4]';
y = [1.1 2.9 5.2 7.1 8.8]';
A = [x ones(size(x))]; % columns for m and c
coef = A \ y;
fprintf('best fit: y = %.3f x + %.3f\n', coef(1), coef(2));best fit: y = 1.960 x + 1.100
Eigenvalues and eigenvectors
An eigenvector v of a square matrix A keeps its direction when multiplied by A: A·v = λ·v, where λ is the eigenvalue. eig(A) returns the eigenvalues; [V, D] = eig(A) returns eigenvectors in the columns of V and eigenvalues on the diagonal of D:
A = [2 1; 1 2];
lambda = eig(A)
[V, D] = eig(A);
fprintf('A*V equals V*D within 1e-12: %d\n', norm(A * V - V * D) < 1e-12);
fprintf('sum of eigenvalues %g = trace %g\n', sum(lambda), trace(A));
fprintf('product of eigenvalues %g = det %g\n', prod(lambda), det(A));lambda =
1
3
A*V equals V*D within 1e-12: 1
sum of eigenvalues 4 = trace 4
product of eigenvalues 3 = det 3
For symmetric matrices like this one, eigenvalues are real and come out in ascending order. (The signs of eigenvectors are arbitrary, so different programs may return v or -v; both are correct.)
Decompositions
| Function | Factorisation | Use |
|---|---|---|
[L, U, P] = lu(A) |
P*A = L*U |
Solving many systems with the same A |
[Q, R] = qr(A) |
A = Q*R |
Least squares, orthogonal bases |
R = chol(A) |
A = R'*R |
Symmetric positive definite matrices |
[U, S, V] = svd(A) |
A = U*S*V' |
Rank, pseudo-inverse, compression |
A = [4 2; 2 3];
R = chol(A);
disp(R)
fprintf('R''*R reproduces A: %d\n', norm(R' * R - A) < 1e-12);
s = svd([3 0; 4 5]);
fprintf('singular values: %.4f %.4f\n', s); 2.0000 1.0000
0 1.4142
R'*R reproduces A: 1
singular values: 6.7082 2.2361
Test yourself
Which is the recommended way to solve A*x = b?
x = inv(A) * bx = A \ bx = b / Ax = A * b
What does det(A) == 0 tell you about a square matrix?
- A is the identity
- A is singular (no unique solution for A*x = b)
- A is symmetric
- A has only positive eigenvalues
What does A \ b return when A has more rows than columns?
- An error
- The least-squares solution
- Only the first equations' solution
- NaN