Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Gauss-Jordan Elimination in C++

Tags:

c++

I was using Gauss-Jordan elimination in C++ to solve a system of linear equations. Code works fine. Was wondering why Lines 1,2,3 in void gauss() can't be replaced by Line 4 (getting incorrect output after doing so)?

#include <iostream>
using namespace std;
class Gauss
{
    float a[50][50];
    int n;
public:
    void accept()
    {
        cout<<"Enter no. of variables: ";
        cin>>n;
        for(int i=0;i<n;i++)
        {
            for(int j=0;j<n+1;j++)
            {
                if(j==n)
                    cout<<"Constant no."<<i+1<<" = ";
                else
                    cout<<"a["<<i+1<<"]["<<j+1<<"] = ";
                cin>>a[i][j];
            }
        }
    }
    void display()
    {
        for(int i=0;i<n;i++)
        {
            cout<<"\n";
            for(int j=0;j<n+1;j++)
            {
                if(j==n)
                    cout<<" ";
                cout<<a[i][j]<<"\t";
            }
        }
    }

    void gauss()//converting augmented matrix to row echelon form
    {
        float temp;//Line 1
        for(int i=0;i<n;i++)
        {
            for(int j=i+1;j<n;j++)
            {
                temp=a[j][i]/a[i][i];//Line 2
                for(int k=i;k<n+1;k++)
                {
                      a[j][k]-=temp*a[i][k];//Line 3
                    //a[j][k]-=a[j][i]*a[i][k]/a[i][i];//Line 4
                }
            }
        }
    }

    void EnterJordan()//converting to reduced row echelon form
    {
        float temp;
        for(int i=n-1;i>=0;i--)
        {

            for(int j=i-1;j>=0;j--)
            {
                temp=a[j][i]/a[i][i];
                for(int k=n;k>=i;k--)
                {
                    a[j][k]-=temp*a[i][k];
                }
            }
        }

        float x[n];
        for(int i=0;i<n;i++)//making leading coefficients zero
            x[i]=0;
        for(int i=0;i<n;i++)
        {
            for(int j=0;j<n+1;j++)
            {
                if(x[i]==0&&j!=n)
                    x[i]=a[i][j];
                if(x[i]!=0)
                    a[i][j]/=x[i];
            }
        }
    }
    void credits()
    {
        for(int i=0;i<n;i++)
        {
            cout<<"\nx"<<i+1<<" = "<<a[i][n]<<endl;
        }
    }

};

int main()
{
    Gauss obj;
    obj.accept();
    cout<<"\n\nAugmented matrix: \n\n\n";
    obj.display();
    obj.gauss();
    cout<<"\n\nRow Echelon form: \n\n\n";
    obj.display();
    obj.EnterJordan();
    cout<<"\n\nReduced row echelon form:\n\n\n";
    obj.display();
    cout<<"\n\nSolution: \n\n\n";
    obj.credits();
    return 0;
}

Note: My code doesn't take into consideration the problem of division when the pivot is zero (I'm choosing the diagonal element as the pivot every time). For the particular example I tried however, such a case was not encountered.

Augmented matrix is :

 2   1  -1    8 
-3  -1   2   -11    
-2   1   2   -3

The output matrix is :

1   0   0    2  
0   1   0    3  
0   0   1    -1 

and the solution is :

x1 = 2

x2 = 3

x3 = -1

Using Line 4, the output matrix is :

1   0   0    -0.75  
0   1   -0   8  
0   0   1    -1.5   

and the solution is :

x1 = -0.75

x2 = 8

x3 = -1.5
like image 330
pius Avatar asked Sep 06 '15 19:09

pius


People also ask

What is Gauss Jordan elimination?

Gauss-Jordan Elimination is an algorithm that can be used to solve systems of linear equations and to find the inverse of any invertible matrix. It relies upon three elementary row operations one can use on a matrix: Swap the positions of two of the rows. Multiply one of the rows by a nonzero scalar.

Is Gaussian and Gauss Jordan elimination?

Difference between gaussian elimination and gauss jordan elimination. The difference between Gaussian elimination and the Gaussian Jordan elimination is that one produces a matrix in row echelon form while the other produces a matrix in row reduced echelon form.

What is the formula of Gauss elimination method?

(1) Write the given system of linear equations in matrix form AX = B, where A is the coefficient matrix, X is a column matrix of unknowns and B is the column matrix of the constants. (2) Reduce the augmented matrix [A : B] by elementary row operations to get [A' : B']. (3) We get A' as an upper triangular matrix.

Why is it called Gauss Jordan elimination?

Carl Friedrich Gauss championed the use of row reduction, to the extent that it is commonly called Gaussian elimination. It was further popularized by Wilhelm Jordan, who attached his name to the process by which row reduction is used to compute matrix inverses, Gauss-Jordan elimination.


1 Answers

Your Line #4 reads from a[j][i] many times, and the first time through the inner loop, when k == i, changes a[j][i] to 0.0f, thus breaking the next n-i iterations.

Reordering reads of a variable with a write to the same location is not safe.

like image 82
Ben Voigt Avatar answered Oct 23 '22 00:10

Ben Voigt