GEn GS
GEn GS
equations. First we input all the coefficients an of the equation a1x1+a2x2+.+anxn = b and the
resultant b into a matrix which we augment using the formula aij = aij aik*akj/akk and then we find
the value of xi using the formula aij = (ain+1 - j-1i=0aijxj )/ aii
*/
#include <iostream>
#include<cmath>
//include header file
using namespace std; //include namespace
class simeq
//declare class
{
float a[100];
//variable to hold n coefficeints
float b;
//variable to hold resultant
public:
static int n;
//variable to hold number of equations
void seta(float f, int i)
//sets the value of ai
{
a[i] = f;
}
void setb(float f) //sets value of resultant
{
b = f;
}
float geta(int i)
//return ai
{
return a[i];
}
float getb()
//return resultant
{
return b;
}
} eq[100];
//object array. Each represents an equation
int simeq::n = 0;
//initialize static variable
void input() //function to input value into object array
{
cout<<"Enter the number of equations : ";
int c = 0;
cin>>c; cout<<endl;
//input no. of equations
eq[0].n = c;
//store into static variable
cout<<"Enter the equation : "<<endl;
int i = 0, j = 0;
float s = 0;
while(i<c)
//loop to store coefficients of equations into respective data members
{
j = 0;
while(j<c)
{
cout<<"coefficient number "<<j<<" : ";
cin>>s;
eq[i].seta(s,j);
//store coefficients
j++;
}
cout<<endl<<"R.H.S : ";
cin>>s;
eq[i].setb(s);
i++;
//store resultant
}
}
//TILL HERE WE INPUT THE COEFFICIENTS OF THE EQUATIONS
void gauss()
//function to evaluate solution
{
float x[100];
//array to store solution
int c = eq[0].n ;
float a[101][101]; //array to store coefficients and resultant
int i = 1, j = 1;
int n = c;
cout<<endl<<c<<endl;
cout<<endl<<endl<<"INITIAL MATRIX: "<<endl<<endl;
while(i<=c)
{
j = 1;
while(j<=c)
{
a[i][j] = eq[i-1].geta(j-1);
//store coefficients of ith equation into jth row of matrix
j++;
}
a[i][n+1] = eq[i-1].getb();
//store resultant as last element of same row
i++;
}
for(i=1;i<=n;i++)
//display matrix
{
for(j=1;j<=n+1;j++)
{
cout<<a[i][j]<<" ";
}
cout<<endl;
}
//NOW TO AUGMENT THE MATRIX
cout<<endl<<endl<<"AUGMENTED MATRIX: "<<endl<<endl;
float s, aik;
int k = 0;
for(k=1;k<=n-1;k++)
// triangularize initial matrix
{
for(i=k+1;i<=n;i++)
{
aik = a[i][k]/a[k][k];
//aik = aik/akk
for(j=k;j<=n+1;j++)
{
a[i][j] = a[i][j] - aik*a[k][j] ;
cout<<a[i][j]<<" ";
//display augmented element
}
cout<<endl;
}
}
x[n] = a[n][n+1]/a[n][n] ;
//set nth element of x
for(i=n-1;i>=1;i--)
{
s = 0;
for(j=i+1;j<=n;j++)
{
s = s + a[i][j]*x[j];
}
x[i] = (a[i][n+1]-s)/a[i][i];
//evaluate xn
}
i = 1;
while(i<=n)
//display xn (solution)
{
cout<<"x["<<i<<"] :"<<x[i]<<endl;
i++;
}
}
int main()
{
input();
gauss();
return 0;
}
// call input()
//call gauss()
INITIAL MATRIX:
8 -3 2 20
4 11 -1 33
1 1 4 9
AUGMENTED MATRIX:
0 12.5 -2 23
0 1.375 3.75 6.5
0 3.97 3.97
x[1] :3
x[2] :2
x[3] :1
phy1088_1092@physics-HP-ProBook-445-G1:~$
/*This program uses Gauss Seidel method to evaluate the solutions to n simultaneous equations. We
use the equation that xi = (bi - ij aijxj )/aii . We take the initial approximations as 0 and then we
evaluate the final solution by taking further approximations untillany condition is satisfied*/
#include <iostream>
#include <cmath>
//include header
using namespace std; //include namespace
class simeq
//declare class
{
float a[100];
//variable to hold coefficients
float x[100];
//variable to hold solution
float b;
//variable to hold resultant
public:
static int n;
//variable to hold number of equations
simeq()
//constructor
{
int i = 0;
while(i<100)
{
a[i] = 0;
x[i] = 0;
i++;
}
b = 0;
}
void seta(float f, int i)
//sets ai
{
a[i] = f;
}
void setb(float f)
//sets resultant
{
b = f;
}
void setx(float f, int i)
//set xi
{
x[i] = f;
}
float geta(int i)
//return ai
{
return a[i];
}
float getx(int i)
//return xi
{
return x[i];
}
float getb()
//return resultant
{
return b;
}
} eq[100];
//declare object. Each represents an equation
int simeq::n = 0;
//initialise static variable
void input()
//function to input values into object array
{
float a[101][101];
// array to store coefficients and resultant
int i = 1, j = 1;
int n = c;
cout<<endl<<c<<endl;
cout<<endl<<endl<<"INITIAL MATRIX: "<<endl<<endl;
while(i<=c)
{
j = 1;
while(j<=c)
{
a[i][j] = eq[i-1].geta(j-1);
j++;
}
a[i][n+1] = eq[i-1].getb();
i++;
}
i = 1;
//Return the new values to the object
while(i<=c)
{
j = 1;
while(j<=c)
{
eq[i-1].seta(a[i][j],j);
j++;
}
eq[i-1].setb(a[i][n+1]);
i++;
}
for(i=1;i<=n;i++) //display matrix
{
for(j=1;j<=n+1;j++)
{
cout<<a[i][j]<<" ";
}
cout<<endl;
}
//NOW TO CALCULATE Xn
for(i=1;i<=n;i++)
//set all xis to zero
{
x[i] = 0;
}
long double eps = 0.00001;
float s = 0;
int k = 0, itrmax = 0;
cout<<"set max number of iterations: ";
cin>>itrmax;
//input maximum iterations
cout<<endl;
while(condition(0)>eps&&k<=itrmax) //check conditions
{ s = 0;
for(i=1;i<=n;i++)
//loop to change previous approximation of xi
{ s = 0;
for(j=1;j<=n;j++)
{
if(i!=j)
{
s = s + a[i][j]*x[j];
}
}
x[i] = (a[i][n+1]-s)/a[i][i];
}
k++;
}
i = 1;
while(i<=n)
//display solution
{
cout<<"x["<<i<<"] :"<<x[i]<<endl;
i++;
}
}
int main()
{
input();
Gauss();
return 0;
}
INITIAL MATRIX:
8 -3 2 20
4 11 -1 33
1 1 4 9
set max number of iterations: 20
x[1] :3
x[2] :2
x[3] :1
phy1088_1092@physics-HP-ProBook-445-G1:~$