Unraveling the Mystery of the Gauss Method

Marcos Gois
5 min readFeb 15, 2023

--

A Step-by-Step Guide with a Code Example in C++

Gauss thug life

The Gauss Method is a well-known numerical method used for solving systems of linear equations. This method is named after the German mathematician Carl Friedrich Gauss, who discovered it in the late 18th and early 19th centuries. The Gauss Method involves transforming a matrix of coefficients and constants into an upper triangular matrix, which makes it easier to solve the system of equations.

In this article, we will go over the basics of the Gauss Method and explain how it works step by step. To help illustrate the method, we will use a code example written in C++. The code example will show how to implement the Gauss Method to solve a system of linear equations.

Let’s start by discussing the concept of systems of linear equations. A system of linear equations is a set of equations that contain variables and constants. The goal is to find the values of the variables that satisfy all of the equations in the system. For example, consider the following system of two linear equations:

x + y = 3

2x + 3y = 7

This system can be written in matrix form as:

| 1 1 | x | 3 |

| 2 3 | y | 7 |

The Gauss Method involves transforming the matrix into an upper triangular matrix, which is a matrix where all the elements below the main diagonal are zero. The main diagonal of a matrix is the diagonal from the top-left corner to the bottom-right corner. The process of transforming the matrix into an upper triangular matrix is called Gaussian elimination.

Here is a step-by-step explanation of how to perform Gaussian elimination:

  1. Choose a pivot element: Choose the largest absolute value in the first column as the pivot element.
  2. Divide the first row by the pivot element: This will make the pivot element equal to 1.
  3. Subtract the first row from each of the other rows, multiplied by a suitable scalar, to make the elements below the pivot element equal to zero.
  4. Repeat the above steps for the remaining columns until the matrix is transformed into an upper triangular matrix.

Once the matrix is transformed into an upper triangular matrix, the system of linear equations can be easily solved by back substitution. The idea of back substitution is to start from the bottom row of the upper triangular matrix and solve for the variables in reverse order.

Here is a step-by-step explanation of how to perform back substitution:

  1. Solve for the last variable: The value of the last variable can be found by dividing the last element of the last row by the last element of the last column.
  2. Solve for the second-to-last variable: The value of the second-to-last variable can be found by dividing the second-to-last element of the second-to-last row by the second-to-last element of the second-to-last column and subtracting the product of the last variable and the corresponding element in the second-to-last row.
  3. Repeat the above steps for the remaining variables until all variables have been solved for.

Now that we have an understanding of the Gauss Method, let’s take a look at the code example in C++. The code example implements the Gauss Method to solve a system of linear equations. The code starts by asking the user to input the order of the matrix, which represents the number of linear equations in the system. The code then asks the user to input the elements of the augmented matrix, which is the matrix that contains the coefficients and constants of the linear equations.

int n;
float matriz[20][20];
float x[10];

cout << "Enter the order of the matrix: ";
cin >> n;

cout << "Enter the elements of the augmented matrix:\n\n";
for(int linha = 1; linha <= n; linha++)
{
for(int coluna = 1; coluna <= (n+1); coluna++)
{
cout << "Matrix[" << linha << "][" << coluna << "] : ";
cin >> matriz[linha][coluna];
}
}

Next, the code performs Gaussian elimination by zeroing out the elements below the main diagonal. This is done by dividing the first row by the pivot element, and subtracting the first row from each of the other rows, multiplied by a suitable scalar. The code then repeats the process for the remaining columns until the matrix is transformed into an upper triangular matrix.

float conta;
int linha, coluna, k;
float somador = 0;

// Zeroing elements below the main diagonal
for(coluna = 1; coluna <= n; coluna++)
{
for(linha = 1; linha <= n; linha++)
{
if(linha > coluna)
{
conta = -(matriz[linha][coluna] / matriz[coluna][coluna]);
for(k = 1; k <= (n+1); k++)
{
matriz[linha][k] = conta * matriz[coluna][k] + matriz[linha][k];
}
}
}
}

// Displaying the matrix after zeroing elements below the main diagonal
cout << "\nMatrix after zeroing elements below the main diagonal:\n";
for(linha = 1; linha <= n; linha++)
{
for(coluna = 1; coluna <= (n+1); coluna++)
{
cout << matriz[linha][coluna] << " ";
}
cout << endl;
}

The code performs back substitution to solve for the variables. Then It starts by solving for the last variable, and then moves on to the second-to-last variable, and so on, until all variables have been solved for. Then the code outputs the solution for each variable, which represents the values of the variables that satisfy the system of linear equations.

// Calculating the solution
x[n] = matriz[n][n+1] / matriz[n][n];
for(linha = (n-1); linha >= 1; linha--)
{
somador = 0;
for(coluna = (linha+1); coluna <= n; coluna++)
{
somador = somador + matriz[linha][coluna] * x[coluna];
}
x[linha] = (matriz[linha][n+1] - somador) / (matriz[linha][linha]);
}

// Displaying the solution
cout << "\nSolution:\n";
for(linha = 1; linha <= n; linha++)
{
cout << "x[" << linha << "] = " << x[linha] << endl;
}

The Gauss Method is a powerful numerical method for solving systems of linear equations. It involves transforming the matrix into an upper triangular matrix through Gaussian elimination and solving for the variables through back substitution. The code example in C++ provides a clear illustration of how to implement the Gauss Method. By understanding the basics of the Gauss Method and the code example, you should be able to solve systems of linear equations with ease.

Code complete:

References:

  1. https://www.facom.ufu.br/~dino/disciplinas/GBC051/GaussSeidel_Ruyano.pdf
  2. https://www.ime.unicamp.br/~marcia/AlgebraLinear/eliminacao_gaussiana.html
  3. https://www-di.inf.puc-rio.br/~tcosta/cap2.htm
  4. https://www.math.tecnico.ulisboa.pt/~calves/cursos/SisLin-Dir.htm#:~:text=Tendo%20uma%20matriz%20triangular%2C%20basta,)ij%5D%20a%20matriz%20inicial.&text=Se%20o%20pivot%20a(k,que%20efectuar%20troca%20de%20linhas.
  5. https://cn.ect.ufrn.br/index.php?r=conteudo%2Fsislin-gauss

--

--

Marcos Gois

Full stack developer | Data Science | Machine Learning | Python | C# | SQL