#include <iostream>
using namespace std;
class GaussSiedel
{
public:
GaussSiedel()
{
x0=x1=x2 = 0;
}
void askEqn();
void no_Iterate();
void solve();
void calcX0();
void calcX1();
void calcX2();
private:
double a0,b0,c0,a1,b1,c1,a2,b2,c2,d0,d1,d2;
double x0,x1,x2;
double num;
};
void GaussSiedel::askEqn()
{
cout << "For the eqn a0x0 + b0x1 + c0x2 = d0 , Enter a0,b0,c0,d0:\n";
cin >>a0 >>b0>>c0>>d0;
cout << "For the eqn a1x0 + b1x1 + c1x2 = d1 , Enter a1,b1,c1,d1:\n";
cin >>a1 >>b1>>c1>>d1;
cout << "For the eqn a2x0 + b2x1 + c2x2 = d2 , Enter a2,b2,c2,d2:\n";
cin >>a2>>b2>>c2>>d2;
}
void GaussSiedel::no_Iterate()
{
cout << "Enter up to how much iteration do you want to go: ";
cin >> num;
}
void GaussSiedel::calcX0()
{
x0 = ( d0 - b0 * x1 - c0 * x2) /a0;
}
void GaussSiedel::calcX1()
{
x1 = ( d1 - a1 * x0 - c1 * x2) /b1;
}
void GaussSiedel::calcX2()
{
x2 = ( d2 - b2 * x1 - a2 * x0) / c2;
}
void GaussSiedel::solve()
{
for(int i = 0;i<num;i++)
{
calcX0();
calcX1();
calcX2();
cout << endl << endl <<"Iteration " << i+1 << ": \n";
cout << "X0 = " << x0 << endl << "X1 = " << x1 << endl << "X2 = " << x2 ;
}
}
int main()
{
GaussSiedel g1;
g1.askEqn();
g1.no_Iterate();
g1.solve();
return 0;
}
C and C++ programs, games, softwares. Easy programming guide. Learn to code and enjoy coding in CoderNepal
Home » Linear Algebric Equation
Showing posts with label Linear Algebric Equation. Show all posts
Showing posts with label Linear Algebric Equation. Show all posts
Monday, April 17, 2017
C++ code for Gauss Siedel method
This is the solution for Gauss Siedel method in C++
Saturday, April 15, 2017
C++ code for Gauss Jordan method
This is the C++ source code for Gaussian Jordan or Gauss Jordan Method.
#include <iostream>
#include <iomanip>
#include <iostream>
#include <conio.h>
#include <math.h>
using namespace std;
class GaussJordan
{
public:
GaussJordan();
void setMatA();
void setMatB();
void solve();
void setRowCol();
bool pivotZero(int row);
void normalize(int row);
void exchangeRow(int row);
void makeBelowPivotZero(int row);
void makeAbovePivotZero(int row);
void display();
protected:
private:
double a[5][5],x[5];
int rowA,colA;
};
GaussJordan::GaussJordan()
{
//ctor
}
void GaussJordan::setRowCol()
{
cout << "Enter row of Matrix A: ";
cin >> rowA;
cout << "Enter column of Matrix A: ";
cin >> colA;
}
void GaussJordan::setMatA()
{
cout << "For Matrix A : " << endl;
for(int i = 0; i < rowA; i++ )
{
for(int j = 0; j < colA; j++)
{
cout << "Enter A[" << i << "]" << "[" << j << "] : " ;
cin >> a[i][j];
}
}
}
void GaussJordan::setMatB()
{
cout << "For Matrix B: " << endl;
for(int i = 0; i < rowA; i++ )
{
cout << "Enter B[" << i << "]" << "[" << 0 << "] : " ;
cin >> a[i][colA];
}
}
void GaussJordan::solve()
{
for(int i = 0; i < rowA; i++)
{
if(!pivotZero(i))
{
normalize(i);
}
else
{
exchangeRow(i);
normalize(i);
}
makeBelowPivotZero(i);
}
for(int i = rowA-1; i > 0; i--)
{
makeAbovePivotZero(i);
}
}
bool GaussJordan::pivotZero(int row)
{
if(a[row][row] == 0)
{
return true;
}
else
return false;
}
void GaussJordan::exchangeRow(int row)
{
//cout << "Exchanfged\n";
int k = row+1;
int great = row+1;
while (k<rowA)
{
if(a[great][row]<a[k][row])
great = k;
k++;
}
//cout << great << endl;
for (int j = 0; j < colA+1; j++)
{
double temp;
temp = a[row][j];
a[row][j] = a[great][j];
a[great][j] = temp;
}
display();
}
void GaussJordan::normalize(int row)
{
//cout << "normalized \n";
double normal = a[row][row];
for(int j = 0; j < colA+1; j++)
{
a[row][j] /= normal;
}
display();
}
void GaussJordan::makeBelowPivotZero(int row)
{
//cout << "made zero" << endl;
for(int i = row+1; i < rowA; i++)
{
double zeroFactor = a[i][row];
// cout << "zero = " << zeroFactor<< endl;
for(int j = 0; j < colA+1; j++)
{
a[i][j] -= zeroFactor*a[row][j];
}
display();
}
}
void GaussJordan::makeAbovePivotZero(int row)
{
//cout << "made zero" << endl;
for(int i = row-1; i >= 0; i--)
{
double zeroFactor = a[i][row];
// cout << "zero = " << zeroFactor<< endl;
for(int j = row; j < colA+1; j++)
{
a[i][j] -= zeroFactor*a[row][j];
}
display();
}
}
void GaussJordan::display()
{
for(int i = 0; i < rowA; i++)
{
for (int j = 0; j < colA+1; j++)
{
cout << a[i][j] << " ";
}
cout << endl;
}
cout << endl << endl;
}
int main()
{
GaussJordan g1;
cout << setprecision(4);
g1.setRowCol();
g1.setMatA();
g1.setMatB();
g1.solve();
getch();
return 0;
}
C++ code for Gauss Elimination method
This is the C++ Program source code for Gaussian Elimination to find the system of Linear Algebraic equations
#include <iostream>
#include <iomanip>
#include <iostream>
#include <conio.h>
#include <math.h>
using namespace std;
class GaussElimination
{
public:
void setMatA();
void setMatB();
void solve();
void setRowCol();
bool pivotZero(int row);
void normalize(int row);
void exchangeRow(int row);
void makeBelowPivotZero(int row);
void substitute();
void display();
protected:
private:
double a[5][5],x[5];
int rowA,colA;
};
void GaussElimination::setRowCol()
{
cout << "Enter row of Matrix A: ";
cin >> rowA;
cout << "Enter column of Matrix A: ";
cin >> colA;
}
void GaussElimination::setMatA()
{
cout << "For Matrix A : " << endl;
for(int i = 0; i < rowA; i++ )
{
for(int j = 0; j < colA; j++)
{
cout << "Enter A[" << i << "]" << "[" << j << "] : " ;
cin >> a[i][j];
}
}
}
void GaussElimination::setMatB()
{
cout << "For Matrix B: " << endl;
for(int i = 0; i < rowA; i++ )
{
cout << "Enter B[" << i << "]" << "[" << 0 << "] : " ;
cin >> a[i][colA];
}
}
void GaussElimination::solve()
{
for(int i = 0; i < rowA; i++)
{
if(!pivotZero(i))
// this is done because if the pivot in any row is zero which creates error
{
normalize(i);
}
else
{
exchangeRow(i);
normalize(i);
}
makeBelowPivotZero(i);
}
substitute();
}
void GaussElimination::substitute()
{
x[rowA-1] = a[rowA-1][colA];
for(int i = rowA-2; i>=0 ;i--)
{
double s = 0;
for(int j = i+1; j < colA; j++)
{
s+= a[i][j] * x[j];
}
x[i] = a[i][colA] - s;
}
cout << endl << "The Required values are:\n";
for(int i = 0; i < rowA;i++)
cout << "X["<<i<<"]: "<< x[i] << endl;
}
bool GaussElimination::pivotZero(int row)
{
if(a[row][row] == 0)
{
return true;
}
else
return false;
}
void GaussElimination::exchangeRow(int row)
{
// cout << "Exchanfged\n";
int k = row+1;
int great = row+1;
while (k<rowA)
{
if(a[great][row]<a[k][row])
great = k;
k++;
}
cout << great << endl;
for (int j = 0; j < colA+1; j++)
{
double temp;
temp = a[row][j];
a[row][j] = a[great][j];
a[great][j] = temp;
}
display();
}
void GaussElimination::normalize(int row)
{
//cout << "normalized \n";
double normal = a[row][row];
for(int j = 0; j < colA+1; j++)
{
a[row][j] /= normal;
}
display();
}
void GaussElimination::makeBelowPivotZero(int row)
{
// cout << "made zero" << endl;
for(int i = row+1; i < rowA; i++)
{
double zeroFactor = a[i][row];
// cout << "zero = " << zeroFactor<< endl;
for(int j = 0; j < colA+1; j++)
{
a[i][j] -= zeroFactor*a[row][j];
}
display();
}
}
void GaussElimination::display()
{
cout << setprecision(4) << endl;
for(int i = 0; i < rowA; i++)
{
for (int j = 0; j < colA+1; j++)
{
cout << a[i][j] << " ";
}
cout << endl;
}
cout << endl << endl;
}
int main()
{
GaussElimination g1;
g1.setRowCol();
g1.setMatA();
g1.setMatB();
g1.solve();
getch();
return 0;
}
Tuesday, April 11, 2017
C++ code of Gauss Elimination with Pivoting
Solution for Gauss Elimination with pivoting using C++ Program Code
#include <iostream>
#include <iomanip>
#include <iostream>
#include <conio.h>
#include <math.h>
using namespace std;
class GaussPivot
{
public:
void setMatA();
void setMatB();
void solve();
void setRowCol();
bool greatPivot(int row);
void normalize(int row);
void exchangeRow(int row);
void makeBelowPivotZero(int row);
void substitute();
void display();
protected:
private:
double a[5][5],x[5];
int rowA,colA;
};
void GaussPivot::setRowCol()
{
cout << "Enter row of Matrix A: ";
cin >> rowA;
cout << "Enter column of Matrix A: ";
cin >> colA;
}
void GaussPivot::setMatA()
{
cout << "For Matrix A : " << endl;
for(int i = 0; i < rowA; i++ )
{
for(int j = 0; j < colA; j++)
{
cout << "Enter A[" << i << "]" << "[" << j << "] : " ;
cin >> a[i][j];
}
}
}
void GaussPivot::setMatB()
{
cout << "For Matrix B: " << endl;
for(int i = 0; i < rowA; i++ )
{
cout << "Enter B[" << i << "]" << "[" << 0 << "] : " ;
cin >> a[i][colA];
}
}
void GaussPivot::solve()
{
for(int i = 0; i < rowA; i++)
{
if(greatPivot(i))
{
normalize(i);
}
else
{
exchangeRow(i);
normalize(i);
}
makeBelowPivotZero(i);
}
substitute();
}
void GaussPivot::substitute()
{
x[rowA-1] = a[rowA-1][colA];
for(int i = rowA-2; i>=0 ;i--)
{
double s = 0;
for(int j = i+1; j < colA; j++)
{
s+= a[i][j] * x[j];
}
x[i] = a[i][colA] - s;
}
cout << endl << "The Required values are:\n";
for(int i = 0; i < rowA;i++)
cout << "X["<<i<<"]: "<< x[i] << endl;
}
bool GaussPivot::greatPivot(int row)
{
// cout << "pivot Check" << endl;
for(int i = row+1; i < rowA; i++)
{
if(fabs(a[row][row]) < fabs(a[i][row]))
{
// cout << a[i][row]<<"great" << endl;
display();
return false;
}
}
return true;
}
void GaussPivot::exchangeRow(int row)
{
// cout << "Exchanfged\n";
int k = row+1;
int great = row+1;
while (k<rowA)
{
if(a[great][row]<a[k][row])
great = k;
k++;
}
cout << great << endl;
for (int j = 0; j < colA+1; j++)
{
double temp;
temp = a[row][j];
a[row][j] = a[great][j];
a[great][j] = temp;
}
display();
}
void GaussPivot::normalize(int row)
{
//cout << "normalized \n";
double normal = a[row][row];
for(int j = 0; j < colA+1; j++)
{
a[row][j] /= normal;
}
display();
}
void GaussPivot::makeBelowPivotZero(int row)
{
// cout << "made zero" << endl;
for(int i = row+1; i < rowA; i++)
{
double zeroFactor = a[i][row];
// cout << "zero = " << zeroFactor<< endl;
for(int j = 0; j < colA+1; j++)
{
a[i][j] -= zeroFactor*a[row][j];
}
display();
}
}
void GaussPivot::display()
{
cout << setprecision(4) << endl;
for(int i = 0; i < rowA; i++)
{
for (int j = 0; j < colA+1; j++)
{
cout << a[i][j] << " ";
}
cout << endl;
}
cout << endl << endl;
}
int main()
{
GaussPivot g1;
g1.setRowCol();
g1.setMatA();
g1.setMatB();
g1.solve();
getch();
return 0;
}
Subscribe to:
Posts (Atom)