So I wrote this piece of code for solving a system of linear equations using Gauss-Seidel’s Iterative method in the fifth semester of my undergraduate course for my Numerical Analysis Class. Hope you guys find it useful.

//Gaus-seidel (Written by: Manas Sharma - University of Delhi)
#include<iostream>
#include<iomanip>
#include<cmath>
using namespace std;
int main()
{
cout.precision(4);
cout.setf(ios::fixed);
int n,i,j,k,flag=0,count=0;
cout<<"\nEnter the no. of equations\n";
cin>>n; //Input no. of equations
double a[n][n+1]; //declare a 2d array for storing the elements of the augmented matrix
double x[n]; //declare an array to store the values of variables
double eps,y;
cout<<"\nEnter the elements of the augmented matrix row-wise:\n";
for (i=0;i<n;i++)
for (j=0;j<=n;j++)
cin>>a[i][j];
cout<<"\nEnter the initial values of the variables:\n";
for (i=0;i<n;i++)
cin>>x[i];
cout<<"\nEnter the accuracy upto which you want the solution:\n";
cin>>eps;
for (i=0;i<n;i++) //Pivotisation(partial) to make the equations diagonally dominant
for (k=i+1;k<n;k++)
if (abs(a[i][i])<abs(a[k][i]))
for (j=0;j<=n;j++)
{
double temp=a[i][j];
a[i][j]=a[k][j];
a[k][j]=temp;
}
cout<<"Iter"<<setw(10);
for(i=0;i<n;i++)
cout<<"x"<<i<<setw(18);
cout<<"\n----------------------------------------------------------------------";
do //Perform iterations to calculate x1,x2,...xn
{
cout<<"\n"<<count+1<<"."<<setw(16);
for (i=0;i<n;i++) //Loop that calculates x1,x2,...xn
{
y=x[i];
x[i]=a[i][n];
for (j=0;j<n;j++)
{
if (j!=i)
x[i]=x[i]-a[i][j]*x[j];
}
x[i]=x[i]/a[i][i];
if (abs(x[i]-y)<=eps) //Compare the ne value with the last value
flag++;
cout<<x[i]<<setw(18);
}
cout<<"\n";
count++;
}while(flag<n); //If the values of all the variables don't differ from their previious values with error more than eps then flag must be n and hence stop the loop
cout<<"\n The solution is as follows:\n";
for (i=0;i<n;i++)
cout<<"x"<<i<<" = "<<x[i]<<endl; //Print the contents of x[]
return 0;
}

I'm a physicist specializing in theoretical, computational and experimental condensed matter physics. I like to develop Physics related apps and softwares from time to time. Can code in most of the popular languages. Like to share my knowledge in Physics and applications using this Blog and a YouTube channel.

8 thoughts on “Gauss-Seidel(Iterative Method) For System of Linear Equations-C++ Program”

It’s surprising to find on bragitoff.com a resource so precious about
equations.
We will note your page as a benchmark for Gauss-Seidel(Iterative
Method) For System of Linear Equations-C++ Program.
We also invite you to link and other web resources for equations like http://equation-solver.org/ or https://en.wikipedia.org/wiki/Equation.
Thank you ang good luck!

Hi Princeton University,
Thanks for your comment. Actually I wrote that program for my Numerical Methods class. I am a Physics Postgraduate at University of Delhi.

Hi Rubik!
C is not very different from C++ .
I suppose all you need to do is replace the ‘cout< <' and 'cin>>’ statememts with the C equivalent ‘printf’ and ‘scanf’. Try to do that it’s not very hard. I am too busy to do it on my own.

Hi Manas. I found your code very useful. I just had a small doubt in Pivotisation part.. Especially when the following system of equation is considered..
0x + 2y+ z = 1
2x – y +2z = -3
x – 2y + 0z = 2

How is Partial Pivoting gonna work when we go for the third equation?

Thank you Mr. Manas.. Its a great work to explain this code

The necessary condition for Gauss-Seidel method is that the matrix should be diagonally dominant. https://en.wikipedia.org/wiki/Diagonally_dominant_matrix
So I employed the above(PP) procedure to make the matrix DD.
However, as you have just pointed out, it’s not perfect as it assumes that just swapping the rows would make the matrix DD. However, like the example you just gave, I think the matrix cannot be made DD by simply swapping rows.
So I don’t think the system is solvable by Gauss-Seidel.
The program above would preform Partial Pivoting and you can check it’s output by displaying the matrix after the PP procedure, but the Gauss-Seidel process would never terminate as the system is not DD. So I guess there should be a stricter check on the DD condition.
This is just my understanding of what’s going on here.
Would love some more input from others. I am a physicist btw.

It’s surprising to find on bragitoff.com a resource so precious about

equations.

We will note your page as a benchmark for Gauss-Seidel(Iterative

Method) For System of Linear Equations-C++ Program.

We also invite you to link and other web resources for equations like http://equation-solver.org/ or https://en.wikipedia.org/wiki/Equation.

Thank you ang good luck!

Hi Princeton University,

Thanks for your comment. Actually I wrote that program for my Numerical Methods class. I am a Physics Postgraduate at University of Delhi.

I have updated the above post now to include more resources.

You might also want to check out this video of explanation of the above code on my YouTube Channel: https://www.youtube.com/watch?v=2G13gmF-7ZE&t=9s

The Lab-Write Up of the code which includes the algorithm and flow-chart: https://www.bragitoff.com/2015/10/gauss-seidel-lab-write-up-with-algorithm-and-flowchart/

And more programs on Numerical Methods: https://www.bragitoff.com/2015/11/numerical-analysis-c-programs-for-various-techniques/

Good Day!

Hi Manas Sharma! Can you provide a version of this program in C language?

Hi Rubik!

C is not very different from C++ .

I suppose all you need to do is replace the ‘cout< <' and 'cin>>’ statememts with the C equivalent ‘printf’ and ‘scanf’. Try to do that it’s not very hard. I am too busy to do it on my own.

Hi Manas. I found your code very useful. I just had a small doubt in Pivotisation part.. Especially when the following system of equation is considered..

0x + 2y+ z = 1

2x – y +2z = -3

x – 2y + 0z = 2

How is Partial Pivoting gonna work when we go for the third equation?

Thank you Mr. Manas.. Its a great work to explain this code

Hi there!

Thanks for your feedback.

The necessary condition for Gauss-Seidel method is that the matrix should be diagonally dominant.

https://en.wikipedia.org/wiki/Diagonally_dominant_matrix

So I employed the above(PP) procedure to make the matrix DD.

However, as you have just pointed out, it’s not perfect as it assumes that just swapping the rows would make the matrix DD. However, like the example you just gave, I think the matrix cannot be made DD by simply swapping rows.

So I don’t think the system is solvable by Gauss-Seidel.

The program above would preform Partial Pivoting and you can check it’s output by displaying the matrix after the PP procedure, but the Gauss-Seidel process would never terminate as the system is not DD. So I guess there should be a stricter check on the DD condition.

This is just my understanding of what’s going on here.

Would love some more input from others. I am a physicist btw.

Happy Coding!