MATLAB - Reduced Row Echelon Form



The Reduced Row Echelon Form (RREF) is a specific form of a matrix used in linear algebra to solve systems of linear equations, perform rank computations, and analyze linear transformations. The RREF of a matrix is unique and obtained through a series of elementary row operations: row swapping, scaling rows, and adding multiples of one row to another.

Definition of RREF

A matrix is in Reduced Row Echelon Form if it satisfies the following conditions.

  • Leading 1s − The leading entry (first non-zero entry from the left) in each non-zero row is 1.
  • Zeros Above and Below leading 1s − Each leading 1 is the only non-zero entry in its column.
  • Row Ordering − The leading 1 of any row is to the right of the leading 1 in the row above it.
  • Zero Rows − Any row containing only zeros is at the bottom of the matrix.

Let us consider a simple example

Consider we have a matrix A as.

$$\mathrm{A \: = \: \begin{bmatrix} 1 & 2 & 1 \\ 2 & 4 & -3 \\ 3 & 6 & 4 \end{bmatrix}}$$

To find its RREF, perform the following steps.

Step 1 − Convert the first row to have a leading 1 (if necessary, divide by the leading coefficient).

R1 = [1 2 1].

Step 2 − Eliminate the entries below the leading 1 in the first column by subtracting multiples of the first row from subsequent rows.

$$\mathrm{R2\:=\:\begin{bmatrix}2&4&-3\end{bmatrix}\:-2\:\times\:\begin{bmatrix}1&2&1\end{bmatrix}\:=\:\begin{bmatrix}0&0&-5\end{bmatrix}}$$

$$\mathrm{R3\:=\:\begin{bmatrix}3&6&4\end{bmatrix}\:-3\:\times\:\begin{bmatrix}1&2&1\end{bmatrix}\:=\:\begin{bmatrix}0&0&1\end{bmatrix}} $$

Step 3 − Convert the new row to have a leading 1.

R3 = [ 0 0 1 ]

Step 4 − Eliminate the entries above the leading 1 in the third column by subtracting appropriate multiples of the third row.

$$\mathrm{R1\:=\:\begin{bmatrix}1&2&1\end{bmatrix}\:-1\:\times\:\begin{bmatrix}0&0&1\end{bmatrix}\:=\:\begin{bmatrix}1&2&0\end{bmatrix}}$$

$$\mathrm{R2\:=\:\begin{bmatrix}0&0&-5\end{bmatrix}\:-(-5)\:\times\:\begin{bmatrix}0&0&1\end{bmatrix}\:=\:\begin{bmatrix}0&0&0\end{bmatrix}}$$

The resulting matrix in RREF is.

$$\mathrm{R \: = \: \begin{bmatrix} 1 & 2 & 0 \\ 0 & 0 & 0 \\ 0 & 0 & 1 \end{bmatrix}}$$

Using Matlab for Reduced Row Echelon Form

In MATLAB, you can use the built-in function rref() to compute the Reduced Row Echelon Form (RREF) of a matrix. The RREF is a matrix that has undergone row reductions to reach a simplified form, making it easier to solve systems of linear equations.

Syntax

R = rref(A)
R = rref(A,tol)
[R,p] = rref(A)

Syntax Explanation

R = rref(A) gives you the simplified version of the matrix A by using a method called Gauss-Jordan elimination, which includes a step to ensure the calculations are more stable by swapping rows if needed.

R = rref(A, tol) allows you to set a tolerance level (tol) that helps the algorithm decide which columns are too small to be considered important.

Examples of Reduced Row Echelon Form Matlab

Here are a few examples demonstrating how to use this function effectively −

Example 1

Consider the following system of linear equations.

2x+4y = 10;
4x+9y = 19;

For above equation lets

Define the Augmented Matrix

A = [2 4 10; 4 9 19];

This matrix represents the system of equations, where the first two columns represent the coefficients of x and y, and the last column represents the constants on the right-hand side of the equations.

Use rref() to Simplify the Matrix

R = rref(A);

The rref() function applies the Gauss-Jordan elimination method to the matrix A. It simplifies the matrix by making the diagonal elements 1 and making all other elements in those columns 0.

If needed, the algorithm will swap rows to ensure numerical stability. This ensures that the calculations don't suffer from rounding errors or instability when dealing with small or large numbers.

disp('The Reduced Row Echelon Form is:');
disp(R);

The complete code we have is.

% Define the augmented matrix
A = [2 4 10; 4 9 19];

% Compute the Reduced Row Echelon Form
R = rref(A);

% Display the result
disp('The Reduced Row Echelon Form is:');
disp(R);

On execution the output we get is as follows

>> % Define the augmented matrix
A = [2 4 10; 4 9 19];

% Compute the Reduced Row Echelon Form
R = rref(A);

% Display the result
disp('The Reduced Row Echelon Form is:');
disp(R);

The Reduced Row Echelon Form is:
     1     0     7
     0     1    -1

>> 

The matrix is now in a simplified form called the Reduced Row Echelon Form (RREF).

The solution for linear equation is x=7 and y=1

Example 2

Consider the following matrix.

$$\mathrm{A \: = \: \begin{bmatrix} 1 & 2 & 3 \\ 2 & 4 & 6.0001 \\ 3 & 6 & 9 \end{bmatrix}}$$

This matrix has a row that is almost a linear combination of other rows, except for a small difference (6 vs. 6.0001). Using the tolerance parameter will allow us to decide whether that small difference should be ignored or considered significant.

Define the matrix

A = [1 2 3; 2 4 6.0001; 3 6 9];

This matrix is nearly rank-deficient because the second and third rows are almost linear combinations of the first row.

Use rref() with a Specified Tolerance

tol = 0.01; % Set a higher tolerance
R_high_tol = rref(A, tol);

Here, we're using a tolerance of 0.01, which means that any value smaller than 0.01 will be treated as zero. In this case, the small difference between 6 and 6.0001 will be ignored.

tol = 1e-6; % Set a lower tolerance
R_low_tol = rref(A, tol);

With a much smaller tolerance (1e-6), the algorithm will consider even small differences to be important, so the small difference between 6 and 6.0001 will be taken into account.

The final code we have is −

% Define the matrix
A = [1 2 3; 2 4 6.0001; 3 6 9];

% Compute RREF with high tolerance
tol = 0.01;
R_high_tol = rref(A, tol);

% Compute RREF with low tolerance
tol = 1e-6;
R_low_tol = rref(A, tol);

% Display the results
disp('Reduced Row Echelon Form with high tolerance:');
disp(R_high_tol);

disp('Reduced Row Echelon Form with low tolerance:');
disp(R_low_tol);

On execution the output we get is.

>> % Define the matrix
A = [1 2 3; 2 4 6.0001; 3 6 9];

% Compute RREF with high tolerance
tol = 0.01;
R_high_tol = rref(A, tol);

% Compute RREF with low tolerance
tol = 1e-6;
R_low_tol = rref(A, tol);

% Display the results
disp('Reduced Row Echelon Form with high tolerance:');
disp(R_high_tol);

disp('Reduced Row Echelon Form with low tolerance:');
disp(R_low_tol);

Reduced Row Echelon Form with high tolerance:
     1     2     3
     0     0     0
     0     0     0

Reduced Row Echelon Form with low tolerance:
     1     2     0
     0     0     1
     0     0     0

>> 
Advertisements