You are on page 1of 10

/*This program is devoloped and validated for solving linear system

of equations using Jacobi iteration method*/

#include<stdio.h>
#include<math.h>
void read2d(float a[20][20],int n); //TO READ COEFF. MATRIX
void print2d(float a[20][20],int n); //TO PRINT COEFF. MATRIX
int check(float a[20][20], int n); //TO CHECK FOR ZERO DIAGONAL
void read1d(float b[20],int n); //TO READ CONSTANT MATRIX
void solve(float a[20][20],float b[20],int n, float e);//TO SOLVE
void main()
{
float a[20][20],b[20],x[20],e;
int n,flag=0;
printf("\nEnter the number of variables : ");
scanf("%d",&n);
printf("\nEnter The coefficient matrix: \n");
read2d (a,n);
flag=check(a,n);
if(flag==1)
{
printf("\nCoefficient matrix contains diagonal elements
with value zero !!\n");
print2d(a,n);
return;
}
printf("\nEnter The constant matrix: \n");
read1d (b,n);
printf("\nEnter the value of permissible error : ");
scanf("%f",&e);
solve(a,b,n,e);
}
void read2d(float a[20][20],int n)
{
int i,j;
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
printf("\nEnter coefficient a[%d][%d] : ",i,j);
scanf("%f",(*(a+i)+j));
}
}
}

Page 1 of 10
void read1d(float b[20],int n)
{
int i;
for(i=0;i<n;i++)
{
printf("\n b[%d] : ",i);
scanf("%f",(b+i));
}
}
int check(float a[20][20], int n)
{
int i,flag;
for(i=0;i<n;i++)
{
if(*(*(a+i)+i)==0)
{
flag=1;
}
}
return flag;
}

void print2d(float a[20][20],int n)


{
int i,j;
for(i=0;i<n;i++)
{
printf("\n");
for(j=0;j<n;j++)
{
printf("%f ",(*(*(a+i)+j)));
}
}
}

void solve(float a[20][20],float b[20],int n,float e)


{
int i,j,itr=0,itrmax=1000;
float x[20],old_x[20],sum,error[20],max_error;
char permit;
for(i=0;i<n;i++)
{
old_x[i]=0;
}

do

Page 2 of 10
{
if(itr>=itrmax)
{
printf("\ncould not solve in %d iterations. Do you
still want to continue iteration (Y/N)? ",itr);
scanf(" %c",&permit);
if((permit=='n') || (permit=='N'))
{
return;
}
else if((permit=='y')||(permit=='Y'))
{
itrmax=itrmax+1000;
}
else if(permit!='y'&&permit!='Y'&&permit!='n'&&
permit!='N')
{
printf("\nInvaid input! Continuing with next
iteration");
}
}
itr++;
max_error=0;
for(i=0;i<n;i++)
{
old_x[i]=x[i];
}
for(i=0;i<n;i++)
{
sum=0;
for(j=0;j<n;j++)
{
if(j!=i)
{
sum=sum+(*(*(a+i)+j))*old_x[j];
}
}
x[i]=(*(b+i)-sum)/(*(*(a+i)+i));
error[i]=fabs(x[i]-old_x[i]);
if(error[i]>max_error)
{
max_error=error[i];
}
}

}while(max_error>e);

Page 3 of 10
printf("\nSolution of the above simultaneous algebraic
equations : \n\n");
for(i=0;i<n;i++)
{
printf("x[%d] : %f\n",i,x[i]);
}
printf("\n\nNumber of iterations used : %d\n",itr);
}

/*__________END OF FIRST PROGRAM (JACOBI ITERATION)___________*/

/*__________OUTPUT OF FIRST PROGRAM(JACOBI ITERATION)_________*/

Enter the number of variables : 3

Page 4 of 10
Enter The coefficient matrix:

Enter coefficient a[0][0] : 20

Enter coefficient a[0][1] : 1

Enter coefficient a[0][2] : -2

Enter coefficient a[1][0] : 3

Enter coefficient a[1][1] : 20

Enter coefficient a[1][2] : -1

Enter coefficient a[2][0] : 2

Enter coefficient a[2][1] : -3

Enter coefficient a[2][2] : 20

Enter The constant matrix:

b[0] : 17

b[1] : -18

b[2] : 25

Enter the value of permissible error : .0001

Solution of the above simultaneous algebraic equations :

x[0] : 1.000003
x[1] : -0.999997
x[2] : 0.999998

Number of iterations used : 21

/*______END OF OUTPUT OF FIRST PROGRAM(JACOBI ITERATION)______*/


/*This program is devoloped and validated for solving linear
system of equations using Gauss-Siedel iteration method*/

#include<stdio.h>

Page 5 of 10
#include<math.h>
void read2d(float a[20][20],int n); //TO READ COEFF. MATRIX
void print2d(float a[20][20],int n); //TO PRINT COEFF. MATRIX
int check(float a[20][20], int n); //TO CHECK FOR ZERO
DIAGONAL
void read1d(float b[20],int n); //TO READ CONSTANT MATRIX
void solve(float a[20][20],float b[20],int n, float e); //TO
SOLVE
void main()
{
float a[20][20],b[20],x[20],e;
int n,i,j,flag=0;
printf("\nEnter the number of variables : ");
scanf("%d",&n);
printf("\nEnter The coefficient matrix: \n");
read2d (a,n);
flag=check(a,n);
if(flag==1)
{
printf("\nCoefficient matrix contains diagonal elements
with value zero !!\n");
print2d(a,n);
return;
}
printf("\nEnter The constant matrix: \n");
read1d (b,n);
printf("\nEnter the value of permissible error : ");
scanf("%f",&e);
solve(a,b,n,e);
}
void read2d(float a[20][20],int n)
{
int i,j;
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
printf("\nEnter coefficient a[%d][%d] : ",i,j);
scanf("%f",(*(a+i)+j));
}
}
}

void read1d(float b[20],int n)


{
int i;
for(i=0;i<n;i++)

Page 6 of 10
{
printf("\n b[%d] : ",i);
scanf("%f",(b+i));
}
}
int check(float a[20][20], int n)
{
int i,flag;
for(i=0;i<n;i++)
{
if(*(*(a+i)+i)==0)
{
flag=1;
}
}
return flag;
}

void print2d(float a[20][20],int n)


{
int i,j;
for(i=0;i<n;i++)
{
printf("\n");
for(j=0;j<n;j++)
{
printf("%f ",(*(*(a+i)+j)));
}
}
}

void solve(float a[20][20],float b[20],int n,float e)


{
int i,j,itr=0,itrmax=1000;
float x[20],old_x[20],sum,error[20],max_error;
char permit;
for(i=0;i<n;i++)
{
old_x[i]=0;
x[i]=0;
}

do
{
if(itr>=itrmax)
{

Page 7 of 10
printf("\ncould not solve in %d iterations. Do
you still want to continue iteration (Y/N)? ",itr);
scanf(" %c",&permit);
if((permit=='n') || (permit=='N'))
{
return;
}
else if((permit=='y')||(permit=='Y'))
{
itrmax=itrmax+1000;
}

else if(permit!='y'&&permit!='Y'&&permit!='n'&&
permit!='N')
{
printf("\nInvaid input! Continuing with next
iteration");
}
}
itr++;
max_error=0;
for(i=0;i<n;i++)
{
old_x[i]=x[i];
}
for(i=0;i<n;i++)
{
sum=0;
for(j=0;j<n;j++)
{
if(j!=i)
{
sum=sum+(*(*(a+i)+j))*x[j];
}
}
x[i]=(*(b+i)-sum)/(*(*(a+i)+i));
error[i]=fabs(x[i]-old_x[i]);
if(error[i]>max_error)
{
max_error=error[i];
}
}

}while(max_error>e);

Page 8 of 10
printf("\nSolution of the above simultaneous algebraic
equations : \n\n");
for(i=0;i<n;i++)
{
printf("x[%d] : %f\n",i,x[i]);
}
printf("\n\nNumber of iterations used : %d\n",itr);
}

/*___END OF SECOND PROGRAM(GAUSS-SIEDEL ITERATION METHOD)_____*/

/*___OUTPUT OF SECOND PROGRAM(GAUSS-SIEDEL ITERATION METHOD)__*/

Page 9 of 10
Enter the number of variables : 3

Enter The coefficient matrix:

Enter coefficient a[0][0] : 20

Enter coefficient a[0][1] : 1

Enter coefficient a[0][2] : -2

Enter coefficient a[1][0] : 3

Enter coefficient a[1][1] : 20

Enter coefficient a[1][2] : -1

Enter coefficient a[2][0] : 2

Enter coefficient a[2][1] : -3

Enter coefficient a[2][2] : 20

Enter The constant matrix:

b[0] : 17

b[1] : -18

b[2] : 25

Enter the value of permissible error : .0001

Solution of the above simultaneous algebraic equations :

x[0] : 1.000001
x[1] : -1.000000
x[2] : 1.000000

Number of iterations used : 4

/*__END OF OUTPUT OF SECOND PROGRAM(GAUSS-SIEDEL ITERATION)__*/

Page 10 of 10

You might also like