
- MATLAB - Home
- MATLAB - Overview
- MATLAB - Features
- MATLAB - Environment Setup
- MATLAB - Editors
- MATLAB - Online
- MATLAB - Workspace
- MATLAB - Syntax
- MATLAB - Variables
- MATLAB - Commands
- MATLAB - Data Types
- MATLAB - Operators
- MATLAB - Dates and Time
- MATLAB - Numbers
- MATLAB - Random Numbers
- MATLAB - Strings and Characters
- MATLAB - Text Formatting
- MATLAB - Timetables
- MATLAB - M-Files
- MATLAB - Colon Notation
- MATLAB - Data Import
- MATLAB - Data Output
- MATLAB - Normalize Data
- MATLAB - Predefined Variables
- MATLAB - Decision Making
- MATLAB - Decisions
- MATLAB - If End Statement
- MATLAB - If Else Statement
- MATLAB - If…Elseif Else Statement
- MATLAB - Nest If Statememt
- MATLAB - Switch Statement
- MATLAB - Nested Switch
- MATLAB - Loops
- MATLAB - Loops
- MATLAB - For Loop
- MATLAB - While Loop
- MATLAB - Nested Loops
- MATLAB - Break Statement
- MATLAB - Continue Statement
- MATLAB - End Statement
- MATLAB - Arrays
- MATLAB - Arrays
- MATLAB - Vectors
- MATLAB - Transpose Operator
- MATLAB - Array Indexing
- MATLAB - Multi-Dimensional Array
- MATLAB - Compatible Arrays
- MATLAB - Categorical Arrays
- MATLAB - Cell Arrays
- MATLAB - Matrix
- MATLAB - Sparse Matrix
- MATLAB - Tables
- MATLAB - Structures
- MATLAB - Array Multiplication
- MATLAB - Array Division
- MATLAB - Array Functions
- MATLAB - Functions
- MATLAB - Functions
- MATLAB - Function Arguments
- MATLAB - Anonymous Functions
- MATLAB - Nested Functions
- MATLAB - Return Statement
- MATLAB - Void Function
- MATLAB - Local Functions
- MATLAB - Global Variables
- MATLAB - Function Handles
- MATLAB - Filter Function
- MATLAB - Factorial
- MATLAB - Private Functions
- MATLAB - Sub-functions
- MATLAB - Recursive Functions
- MATLAB - Function Precedence Order
- MATLAB - Map Function
- MATLAB - Mean Function
- MATLAB - End Function
- MATLAB - Error Handling
- MATLAB - Error Handling
- MATLAB - Try...Catch statement
- MATLAB - Debugging
- MATLAB - Plotting
- MATLAB - Plotting
- MATLAB - Plot Arrays
- MATLAB - Plot Vectors
- MATLAB - Bar Graph
- MATLAB - Histograms
- MATLAB - Graphics
- MATLAB - 2D Line Plot
- MATLAB - 3D Plots
- MATLAB - Formatting a Plot
- MATLAB - Logarithmic Axes Plots
- MATLAB - Plotting Error Bars
- MATLAB - Plot a 3D Contour
- MATLAB - Polar Plots
- MATLAB - Scatter Plots
- MATLAB - Plot Expression or Function
- MATLAB - Draw Rectangle
- MATLAB - Plot Spectrogram
- MATLAB - Plot Mesh Surface
- MATLAB - Plot Sine Wave
- MATLAB - Interpolation
- MATLAB - Interpolation
- MATLAB - Linear Interpolation
- MATLAB - 2D Array Interpolation
- MATLAB - 3D Array Interpolation
- MATLAB - Polynomials
- MATLAB - Polynomials
- MATLAB - Polynomial Addition
- MATLAB - Polynomial Multiplication
- MATLAB - Polynomial Division
- MATLAB - Derivatives of Polynomials
- MATLAB - Transformation
- MATLAB - Transforms
- MATLAB - Laplace Transform
- MATLAB - Laplacian Filter
- MATLAB - Laplacian of Gaussian Filter
- MATLAB - Inverse Fourier transform
- MATLAB - Fourier Transform
- MATLAB - Fast Fourier Transform
- MATLAB - 2-D Inverse Cosine Transform
- MATLAB - Add Legend to Axes
- MATLAB - Object Oriented
- MATLAB - Object Oriented Programming
- MATLAB - Classes and Object
- MATLAB - Functions Overloading
- MATLAB - Operator Overloading
- MATLAB - User-Defined Classes
- MATLAB - Copy Objects
- MATLAB - Algebra
- MATLAB - Linear Algebra
- MATLAB - Gauss Elimination
- MATLAB - Gauss-Jordan Elimination
- MATLAB - Reduced Row Echelon Form
- MATLAB - Eigenvalues and Eigenvectors
- MATLAB - Integration
- MATLAB - Integration
- MATLAB - Double Integral
- MATLAB - Trapezoidal Rule
- MATLAB - Simpson's Rule
- MATLAB - Miscellenous
- MATLAB - Calculus
- MATLAB - Differential
- MATLAB - Inverse of Matrix
- MATLAB - GNU Octave
- MATLAB - Simulink
MATLAB - Gauss Elimination
Gauss Elimination, also known as Gaussian Elimination, is a method for solving systems of linear equations. It involves using a sequence of operations to transform the system's augmented matrix into a row-echelon form, and then performing back substitution to find the solutions.
Let us first understand what an augmented and row-echelon form matrix is.
An augmented matrix is a matrix used in linear algebra to solve systems of linear equations. It combines the coefficient matrix and the constant matrix of a system of equations into a single matrix. The augmented matrix is typically written by appending the columns of the constants (the right-hand side of the equations) to the columns of the coefficients.
For example, consider the system of linear equations −
$$\mathrm{2x \: + \: 3y \: = \: 5}$$
$$\mathrm{4x \: + \: 6y \: = \: 10}$$
The coefficient matrix (A) and the constant matrix (b) are −
$$\mathrm{A \: = \: \begin{pmatrix} 2 & 3 \\ 4 & 6 \end{pmatrix} \: , \: b \: = \: \left(\begin{array}{c}5\\ 10\end{array}\right)}$$
The augmented matrix (A|b) is formed by appending b to A:
$$\mathrm{\begin{pmatrix} 2 & 3 & | & 5 \\ 4 & 6 & | & 10\end{pmatrix}}$$
Row-echelon form (REF) is a specific form of a matrix used in linear algebra to simplify the process of solving systems of linear equations. A matrix is in row-echelon form if it satisfies the following conditions −
- All nonzero rows are above any rows of all zeros.
- The leading entry (also called the pivot) of each nonzero row is 1 and is to the right of the leading entry of the row above it.
- The leading entry is the only nonzero entry in its column.
Here's an example of a 34 matrix in row-echelon form −
$$\mathrm{\begin{pmatrix} 1 & 2 & 3 & 4 \\ 0 & 1 & 5 & 6 \\ 0 & 0 & 1 & 7\end{pmatrix}}$$
In this example −
- The first leading entry is 1, and it appears in the first column.
- The second leading entry is also 1, and it appears to the right of the first leading entry, in the second row and second column.
- The third leading entry is 1, and it appears to the right of the second leading entry, in the third row and third column.
- All entries below the leading entries are zeros.
- The rows are ordered such that any zero rows (if present) would be at the bottom (though there are no zero rows in this particular example).
Steps in Gauss Elimination
Consider the system of linear equations −
$$\mathrm{\begin{cases} 2x \: + \: 3y \: = \: 8\\ 4x \: + \: 9y \: = \: 20\end{cases}}$$
We can write this system in augmented matrix form −
$$\mathrm{\begin{bmatrix} 2 & 3 & | & 8 \\ 4 & 9 & | & 20\end{bmatrix}}$$
Based on above equation following are the steps in Gauss Elimination −
1.Form the augmented matrix −
$$\mathrm{\begin{bmatrix} 2 & 3 & | & 8 \\ 4 & 9 & | & 20\end{bmatrix}}$$
2.Make the leading coefficient of the first row 1 (if it is not already) −
Divide the first row by 2
$$\mathrm{\begin{bmatrix} 1 & \frac{3}{2} & | & 4 \\ 4 & 9 & | & 20\end{bmatrix}}$$
Eliminate the x-term from the second row −
Subtract 4 times the first row from the second row −
$$\mathrm{R2 \: = \: R2 \: - \: 4 \: \times \: R1}$$
$$\mathrm{\begin{bmatrix} 1 & \frac{3}{2} & | & 4 \\ 0 & 3 & | & 4\end{bmatrix}}$$
Make the leading coefficient of the second row 1 (if it is not already) −
Divide the second row by 3
$$\mathrm{\begin{bmatrix} 1 & \frac{3}{2} & | & 4 \\ 0 & 1 & | & \frac{4}{3}\end{bmatrix}}$$
Eliminate the y-term from the first row −
Subtract 3/2 times the second row from the first row.
$$\mathrm{R1 \: = \: R1 \: - \: \frac{3}{2} \: \times \: R2}$$
$$\mathrm{\begin{bmatrix} 1 & 0 & | & \frac{10}{3} \\ 0 & 1 & | & \frac{4}{3}\end{bmatrix}}$$
Write the solutions
From the final augmented matrix, we can see the solutions directly.
$$\mathrm{x \: = \: \frac{10}{3} \: , \: y \: = \: \frac{4}{3}}$$
So, the solution to the system of equations is.
$$\mathrm{x \: = \: \frac{10}{3} \: , \: y \: = \: \frac{4}{3}}$$
Now that we know the steps for Gauss Elimination , let us try to do the same in matlab.
Gauss Elimination in Matlab
The equations we are going to use is.
$$\mathrm{\begin{cases} 2x \: + \: 3y \: = \: 8\\ 4x \: + \: 9y \: = \: 20\end{cases}}$$
Step 1 − Define the coefficient matrix A and the right-hand side vector b:
A = [2 3; 4 9]; b = [8; 20];
Step 2 − Augment the matrix A with the vector b.
Ab = [A b];
Step 3 − Perform row operations to convert A to an upper triangular matrix.
MATLAB has a function called rref (Reduced Row Echelon Form) which simplifies this process. However, if you want to manually perform the steps, you can do the following −
% Forward elimination n = size(Ab, 1); for i = 1:n-1 for j = i+1:n factor = Ab(j,i) / Ab(i,i); Ab(j,:) = Ab(j,:) - factor * Ab(i,:); end end
Step 4 − Backward substitution to solve for the variables.
% Initialize the solution vector x = zeros(n,1); % Backward substitution x(n) = Ab(n,end) / Ab(n,n); for i = n-1:-1:1 x(i) = (Ab(i,end) - Ab(i,i+1:n) * x(i+1:n)) / Ab(i,i); end
Step 5 − Display the result
disp('The solution is:'); disp(x);
The complete matlab code is as follows.
% Define the coefficient matrix A and the right-hand side vector b A = [2 3; 4 9]; b = [8; 20]; % Augment the matrix A with the vector b Ab = [A b]; % Forward elimination n = size(Ab, 1); for i = 1:n-1 for j = i+1:n factor = Ab(j,i) / Ab(i,i); Ab(j,:) = Ab(j,:) - factor * Ab(i,:); end end % Initialize the solution vector x = zeros(n,1); % Backward substitution x(n) = Ab(n,end) / Ab(n,n); for i = n-1:-1:1 x(i) = (Ab(i,end) - Ab(i,i+1:n) * x(i+1:n)) / Ab(i,i); end % Display the result disp('The solution is:'); disp(x);
When the code is executed in matlab command window the output we get is :
The solution is: 2.0000 1.3333
Using rref (Reduced Row Echelon Form)
You can use MATLAB's built-in rref() function to achieve the same result as shown below.
% Define the coefficient matrix A and the right-hand side vector b A = [2 3; 4 9]; b = [8; 20]; % Augment the matrix A with the vector b Ab = [A b]; % Use rref to get the Reduced Row Echelon Form R = rref(Ab); % Extract the solutions x = R(:,end); % Display the result disp('The solution is:'); disp(x);
The output on execution is −
The solution is: 2.0000 1.3333