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
Advertisements