
- 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-Jordan Elimination
Gauss-Jordan elimination is a systematic method for solving systems of linear equations, finding inverses of matrices, and performing other matrix-related calculations. This technique is an extension of Gaussian elimination, aiming to transform a given matrix into its reduced row echelon form (RREF) through a series of row operations.
Gauss-Jordan elimination is widely used in various fields such as engineering, physics, computer science, and economics. Its applications include −
- Solving systems of linear equations.
- Computing the inverse of a matrix.
- Determining the rank of a matrix.
- Solving linear programming problems.
In MATLAB, Gauss-Jordan elimination can be implemented using built-in functions or by manually applying the necessary row operations to achieve the desired form. The primary steps involve.
- Forward Elimination − Transform the matrix into an upper triangular form by using row operations to create zeros below the pivot positions.
- Backward Elimination − Further row operations are performed to create zeros above the pivot positions, resulting in the reduced row echelon form.
The matrix is said to be in reduced row echelon form when the leading entry in each non-zero row is 1, and all the elements in the column containing the leading entry are zeros, except for the leading entry itself.
Heres a step-by-step breakdown of the Gauss-Jordan elimination process in MATLAB.
- Initialize the Matrix − Define the augmented matrix that combines the coefficient matrix and the constants from the linear equations.
- Perform Row Operations − Use MATLAB commands to perform row swapping, scaling, and addition to create zeros in appropriate positions.
- Transform to RREF − Continue the process until the matrix is in reduced row echelon form.
- Extract Solutions − The final matrix form will directly provide the solutions to the system of linear equations.
Gauss-Jordan Elimination is used to solve linear systems of equations and to find the inverse of a matrix. The method transforms the matrix into its reduced row echelon form (RREF) using elementary row operations.
Let's solve a system of linear equations using Gauss-Jordan Elimination. Consider the following system.
$$\mathrm{\begin{cases} x\:+\:2y\:+\:3z\:=\:9 \\ 2x\:+\:3y\:+\:4z\:=\:13 \\ 3x\:+\:4y\:+\:5z\:=\:17\end{cases}}$$
Here are the steps to solve the linear equation using Gauss-Jordan Elimination.
Step 1 − Form the Augmented Matrix.
So the augmented matrix we have from above linear equation is as follows.
$$\mathrm{A \: = \: \begin{bmatrix} 1 & 2 & 3 & | & 9 \\ 2 & 3 & 4 & | & 13 \\ 3 & 4 & 5 & | & 17\end{bmatrix}}$$
Step 2 − Perform Elementary Row Operations to get the matrix in RREF.
Let's implement this in MATLAB.
% Define the augmented matrix A = [1 2 3 9; 2 3 4 13; 3 4 5 17]; % Get the number of rows n = size(A, 1); % Gauss-Jordan Elimination with partial pivoting for i = 1:n % Pivoting: Swap rows if necessary to make the largest absolute value in the % current column the pivot element (for numerical stability) [~, pivotRow] = max(abs(A(i:n, i))); pivotRow = pivotRow + i - 1; if pivotRow ~= i A([i, pivotRow], :) = A([pivotRow, i], :); end % Make the diagonal element 1 A(i, :) = A(i, :) / A(i, i); % Make the other elements in the current column 0 for j = 1:n if i ~= j A(j, :) = A(j, :) - A(j, i) * A(i, :); end end end % Extract the solution solution = A(:, end); % Display the result disp('The solution is:'); disp(solution);
Here is an explanation for the Matlab code.
Step 1 − Define the Augmented Matrix.
A = [1 2 3 9; 2 3 4 13; 3 4 5 17];
This matrix combines the coefficients of the variables and the constants from the right-hand side of the equations.
Step 2 − Get the number of rows.
n = size(A, 1);
We determine the number of rows (which is also the number of equations).
Step 3 − Gauss-Jordan Elimination Loop.
% Gauss-Jordan Elimination with partial pivoting for i = 1:n % Pivoting: Swap rows if necessary to make the largest absolute value in the % current column the pivot element (for numerical stability) [~, pivotRow] = max(abs(A(i:n, i))); pivotRow = pivotRow + i - 1; if pivotRow ~= i A([i, pivotRow], :) = A([pivotRow, i], :); end % Make the diagonal element 1 A(i, :) = A(i, :) / A(i, i); % Make the other elements in the current column 0 for j = 1:n if i ~= j A(j, :) = A(j, :) - A(j, i) * A(i, :); end end end
The code : for i = 1:n, here the loop iterates over each row i in the matrix. The goal is to make the diagonal elements of the coefficient matrix (i.e., elements A(i, i)) equal to 1 and the other elements in the current column i equal to 0.
[~, pivotRow] = max(abs(A(i:n, i))); pivotRow = pivotRow + i - 1; if pivotRow ~= i A([i, pivotRow], :) = A([pivotRow, i], :); end
This block finds the row with the largest absolute value in the current column i (starting from row i to the end). This is done to reduce numerical instability. The row with the largest value becomes the pivot row.
If the largest element isn't already in the current row, it swaps the current row i with the pivotRow. This ensures that the largest element is used as the pivot.
A(i, :) = A(i, :) / A(i, i);
This divides the entire row i by the diagonal element A(i, i), making this diagonal element 1. This is an essential step in reducing the matrix to its row-echelon form.
for j = 1:n if i ~= j A(j, :) = A(j, :) - A(j, i) * A(i, :); end end
This loop iterates through all rows j, except the current row i. For each row j, it subtracts a multiple of row i from row j to make the element A(j, i) zero. This operation transforms the matrix into a row-reduced echelon form, where all elements in the current column, except for the pivot (diagonal element), become zero.
Step 4 − Extract the Solution
solution = A(:, end);
After the Gauss-Jordan elimination, the last column of the matrix A contains the solutions to the system of equations. This step extracts the last column as the solution vector.
Step 5 − Display the Result.
disp('The solution is:'); disp(solution);
Finally, the solution is displayed. This will show the values of x, y, and z that satisfy the system of linear equations.
When we execute the code in matlab command window the output we get is.
>> % Define the augmented matrix A = [1 2 3 9; 2 3 4 13; 3 4 5 17]; % Get the number of rows n = size(A, 1); % Gauss-Jordan Elimination with partial pivoting for i = 1:n % Pivoting: Swap rows if necessary to make the largest absolute value in the % current column the pivot element (for numerical stability) [~, pivotRow] = max(abs(A(i:n, i))); pivotRow = pivotRow + i - 1; if pivotRow ~= i A([i, pivotRow], :) = A([pivotRow, i], :); end % Make the diagonal element 1 A(i, :) = A(i, :) / A(i, i); % Make the other elements in the current column 0 for j = 1:n if i ~= j A(j, :) = A(j, :) - A(j, i) * A(i, :); end end end % Extract the solution solution = A(:, end); % Display the result disp('The solution is:'); disp(solution); The solution is: 2.3333 -1.6667 3.3333 >>