0% found this document useful (0 votes)
52 views9 pages

GEn GS

This program uses the Gauss-Seidel method to solve a system of n simultaneous linear equations. It takes the initial approximations of the solutions x1, x2, x3...xn as 0, and then iteratively calculates improved approximations until the values converge within a specified tolerance or exceed a maximum number of iterations. The coefficients and constants of the equations are input and stored in objects. Gauss-Seidel evaluation functions calculate the new approximations of x values based on the previous values until the solution is reached.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as ODT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
52 views9 pages

GEn GS

This program uses the Gauss-Seidel method to solve a system of n simultaneous linear equations. It takes the initial approximations of the solutions x1, x2, x3...xn as 0, and then iteratively calculates improved approximations until the values converge within a specified tolerance or exceed a maximum number of iterations. The coefficients and constants of the equations are input and stored in objects. Gauss-Seidel evaluation functions calculate the new approximations of x values based on the previous values until the solution is reached.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as ODT, PDF, TXT or read online on Scribd
You are on page 1/ 9

/* In this program we use Gauss elimination method to find the solution to n simultaneous

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()

OUTOUT : GAUSS ELIMINATION


phy1088_1092@physics-HP-ProBook-445-G1:~$ c++ GE.cpp -o GEout
phy1088_1092@physics-HP-ProBook-445-G1:~$ ./GEout
Enter the number of equations : 3
Enter the equation :
coefficient number 0 : 8
coefficient number 1 : -3
coefficient number 2 : 2
R.H.S : 20
coefficient number 0 : 4
coefficient number 1 : 11
coefficient number 2 : -1
R.H.S : 33
coefficient number 0 : 1
coefficient number 1 : 1
coefficient number 2 : 4
R.H.S : 9

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
{

cout<<"Enter the number of equations : ";


int c = 0;
cin>>c; cout<<endl;
//input number of equations
eq[0].n = c;
cout<<"Enter the equation : "<<endl;
int i = 0, j = 0;
float s = 0;
while(i<c) //loop to store coefficients of equations (objects) into respective data members
{
j = 0;
while(j<c)
{
cout<<"coefficient number "<<j<<" : ";
cin>>s;
eq[i].seta(s,j);
//store coefficient j of equation i
j++;
}
cout<<endl<<"R.H.S : ";
cin>>s;
eq[i].setb(s);
//store resultant of equation i
i++;
}
}
//TILL HERE WE INPUT THE COEFFICIENTS OF THE EQUATIONS
long double condition(int z)
//evaluate value of zth equation
{
float s;
int i = 0, t = 1;
while(i<100)
{
if(eq[z].getx(i) != 0)
//check if all xs are zero
{
t = 0;
}
i++;
}
while(i<100)
{
s = s + eq[z].geta(i)*eq[z].getx(i);
i++;
}
if(t == 1)
{
return 1 ;
}
s = s - eq[z].getb();
return s;
//return value of equation minus the resultant
}
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);
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];

//set new approximation

}
k++;
}
i = 1;
while(i<=n)
//display solution
{
cout<<"x["<<i<<"] :"<<x[i]<<endl;
i++;
}
}
int main()
{
input();
Gauss();
return 0;
}

OUTPUT: GAUSS SEIDEL


phy1088_1092@physics-HP-ProBook-445-G1:~$ c++ GS.cpp -o GSout
phy1088_1092@physics-HP-ProBook-445-G1:~$ ./GSout
Enter the number of equations : 3
Enter the equation :
coefficient number 0 : 8
coefficient number 1 : -3
coefficient number 2 : 2
R.H.S : 20
coefficient number 0 : 4
coefficient number 1 : 11
coefficient number 2 : -1
R.H.S : 33
coefficient number 0 : 1
coefficient number 1 : 1
coefficient number 2 : 4
R.H.S : 9

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:~$

You might also like