Friday, June 24, 2016

GAUSS JORDAN -- INVERSE METHOD

                    
                Gauss Jordan Inverse method is one of the procedure to find the inverse of a given matrix.

The steps involved in this method is almost same as the Gauss Jordan method to solve the simulation algebraic equations but it has some extra steps. The program codes given below helps to find the inverse using Gauss Jordan method.


Program code for Gauss Jordan – Inverse Method

#include<iostream>
#include<cmath>
#include<cstdlib>
using namespace std;

void display(float array[][30], int size){
    for(int i = 0; i < size; i++){
        for (int j = 0; j<2*size; j++)
            cout<<array[i][j]<<"\t";
        cout<<endl<<endl;
    }
}
int main(){
    int size;
    float array[30][30], temp;
    cout<<"Enter the number of variables : ";
    cin>>size;
    for(int i = 0; i < size; i++){
        for (int j = 0; j<size; j++)
            cin>>array[i][j];
    }
    for (int i= 0; i<size; i++){
        for (int j = size; j<2*size; j++){
            if( (i + size) == j)
                array[i][j] = 1;
            else array[i][j] = 0;
        }
    }
    cout<<"Provided Matrix :"<<endl;
    display(array, size);
    for (int j = 0; j < size; j++){
        for (int i = 0; i < size; i++){
            if (i != j){
                temp = array[i][j]/array[j][j];
                for (int k = 0; k < 2*size; k++){
                        array[i][k] = array[i][k] - (temp*array[j][k]);
                }
            }
        }
    }
    cout<<"Final Matrix :"<<endl;
    display(array, size);

    for (int i = 0; i < size; i++){
        float temp = array[i][i];
        for (int j = 0; j< (2*size); j++){
            array[i][j] = array[i][j]/temp;
        }
    }
    cout<<"Inverse Matrix :"<<endl;
    display(array, size);

    float inverse[30][30];
    for (int i = 0; i<size; i++){
        for (int j = size; j< 2*size; j++)
            inverse[i][j-size] = array[i][j];
    }
    cout<<"Final Inveerse Matrix :"<<endl;
    for(int i = 0; i < size; i++){
        for (int j = 0; j<size; j++)
            cout<<inverse[i][j]<<"\t";
        cout<<endl<<endl;
    }

    return 0;
}

(Note: Above source code is written using C++ programming in code block)

No comments:

Post a Comment