0% found this document useful (0 votes)
41 views2 pages

Gauss Seidel Iterative Method For Linear System Solution

This C code uses the Gauss-Seidel iterative method to solve a system of linear equations. It takes in a matrix of coefficients and right-hand side values, initializes the solution vector, and iteratively updates the solution estimates until the values converge within a set tolerance or the maximum number of iterations is reached. On each iteration, it calculates new solution estimates based on the previous estimates, prints the current estimates, and checks for convergence before repeating if needed. When finished, it prints the final converged solution vector.

Uploaded by

Rahul Roy
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
41 views2 pages

Gauss Seidel Iterative Method For Linear System Solution

This C code uses the Gauss-Seidel iterative method to solve a system of linear equations. It takes in a matrix of coefficients and right-hand side values, initializes the solution vector, and iteratively updates the solution estimates until the values converge within a set tolerance or the maximum number of iterations is reached. On each iteration, it calculates new solution estimates based on the previous estimates, prints the current estimates, and checks for convergence before repeating if needed. When finished, it prints the final converged solution vector.

Uploaded by

Rahul Roy
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 2

/* Gauss Seidel iterative Method for linear system solution */

#include<stdio.h>
#include<math.h>
#define EPS 0.000001
float a[10][10],b[10],x[10];
int n;
void seidel_f(int ,float [10][10],float [10],float [10]);
void main(void) {
int i,j;
clrscr();
printf("\nplease enter the size of the system=");
scanf("%d",&n);
printf("\nenter the co-efficients row wise-->\n");
for(i=1;i<=n;i++)
for(j=1;j<=n;j++) scanf("%f",&a[i][j]);
printf("enter the R.H.S-->\n");
for (i=1;i<=n;i++) scanf("%f",&b[i]);
seidel_f(n,a,b,x);
getch();
}
void seidel_f(int n,float a[10][10],float b[10],float x[10])
{
int i,j,flag,count=0,it=50;
float sum,x0[10];
// initial value
for(i=1;i<=n;i++) x0[i]=b[i]/a[i][i];
do {
count++;
flag=0;
for(i=1;i<=n;i++)
{
sum=b[i];
for(j=1;j<=n;j++)
{ if(i>j) sum=sum-(a[i][j]*x[j]);
if(i<j) sum=sum-(a[i][j]*x0[j]);
}
x[i]=sum/a[i][i];
printf("\n%d X[%d]=%f",count,i,x[i]);
}
// testing for accuracy
for(i=1;i<=n;i++)
{if(fabs((x[i]-x0[i])/x[i]) > EPS) flag=1;}
if(flag==1)
{
if(count==it) { printf(" no convergence in %d iteration",count);
return;
}
else {
for(i=1;i<=n;i++) x0[i]=x[i];}
}

}while(flag);
printf(" \n\nSolution -->\n");
for(i=1;i<=n;i++) printf("\nX[%d]=%f",i,x[i]);
}

You might also like