MATLAB - Gauss Elimination
Solving Linear Systems in MATLAB
Section titled “Solving Linear Systems in MATLAB”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.
The Problem: Ax = b
Section titled “The Problem: Ax = b”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 MATLAB Way: The Backslash Operator ()
Section titled “The MATLAB Way: The Backslash Operator ()”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 bA = [2 3; 4 9];b = [8; 20];
% Solve for x using the backslash operatorx = A \ bMATLAB executes this and returns the solution vector:
x = 2.0000 1.3333Why 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.
Step 1: Form the Augmented Matrix
Section titled “Step 1: Form the Augmented Matrix”A = [2 3; 4 9];b = [8; 20];Ab = [A b]
Ab = 2 3 8 4 9 20Step 2: Forward Elimination
Section titled “Step 2: Forward Elimination”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 4Step 3: Back Substitution
Section titled “Step 3: Back Substitution”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
Verification with rref
Section titled “Verification with rref”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.3333This directly tells us that x₁ = 2 and x₂ = 1.3333.
Best Practices and Advanced Topics
Section titled “Best Practices and Advanced Topics”- Always prefer
\: For solvingAx = 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
Aand the augmented matrix[A b]. Ifrank(A) < rank([A b]), there is no solution. Ifrank(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) orgmres.