Skip to content

MATLAB - Gauss Elimination

Systems of linear equations are at the heart of countless problems in engineering, physics, statistics, and finance. While methods like Gaussian Elimination are fundamental for understanding the theory, MATLAB provides highly optimized and numerically robust tools that make solving these systems efficient and straightforward.

A system of linear equations is typically written in matrix form as Ax = b, where:

  • A is the matrix of known coefficients.
  • x is the column vector of unknown variables we want to find.
  • b is the column vector of known results.

Consider this system: 2x₁ + 3x₂ = 8 4x₁ + 9x₂ = 20

In matrix form, this becomes:

A = [2 3; 4 9];
b = [8; 20];
x = [x₁; x₂];

The primary, most efficient, and recommended way to solve Ax = b in MATLAB is using the backslash operator, also known as mldivide (matrix left division).

% Define the coefficient matrix A and the result vector b
A = [2 3; 4 9];
b = [8; 20];
% Solve for x using the backslash operator
x = A \ b

MATLAB executes this and returns the solution vector:

x =
2.0000
1.3333

Why use backslash? It’s not just a shortcut for inverting the matrix. A \ b is a sophisticated function that analyzes matrix A and automatically chooses the best algorithm for speed and accuracy (e.g., LU decomposition for square matrices, QR factorization for non-square systems). It is numerically more stable than calculating inv(A) * b.

For Education: Understanding Gaussian Elimination

Section titled “For Education: Understanding Gaussian Elimination”

To understand how the solution is found, we can study Gaussian Elimination. The goal is to transform the augmented matrix [A|b] into an upper triangular form (row-echelon form) using elementary row operations, and then solve for the variables using back substitution.

A = [2 3; 4 9];
b = [8; 20];
Ab = [A b]
Ab =
2 3 8
4 9 20

The goal is to create zeros below the main diagonal. To eliminate the 4 in the second row, we perform the operation: Row2 = Row2 - 2 * Row1.

Ab(2, :) = Ab(2, :) - 2 * Ab(1, :)
Ab =
2 3 8
0 3 4

The new system is: 2x₁ + 3x₂ = 8 0x₁ + 3x₂ = 4

From the second equation, we can easily solve for x₂: 3x₂ = 4 => x₂ = 4/3

Substitute this back into the first equation: 2x₁ + 3(4/3) = 8 => 2x₁ + 4 = 8 => 2x₁ = 4 => x₁ = 2

The rref (Reduced Row Echelon Form) function is an excellent educational tool that performs full Gaussian Elimination to produce an identity matrix on the left. The solution then appears directly in the last column.

A = [2 3; 4 9];
b = [8; 20];
Ab = [A b];
R = rref(Ab)
R =
1.0000 0 2.0000
0 1.0000 1.3333

This directly tells us that x₁ = 2 and x₂ = 1.3333.

  • Always prefer \: For solving Ax = b, the backslash operator is the gold standard in MATLAB.
  • Checking for Solutions: A system may have no solution or infinite solutions. You can investigate this by comparing the rank of the coefficient matrix A and the augmented matrix [A b]. If rank(A) < rank([A b]), there is no solution. If rank(A) == rank([A b]) but this is less than the number of variables, there are infinite solutions.
  • Large, Sparse Systems: For systems with very large matrices that are mostly zeros (sparse), direct methods like “ can be too slow or memory-intensive. In these cases, use iterative solvers like pcg (Preconditioned Conjugate Gradients) or gmres.