Gauss Seidel method in C
What is the Gauss-Seidel method?
Systems of linear equations can be solved numerically iteratively using the Gauss-Seidel method.
For a system of linear equations:
a11x1 + a12x2 + … + a1nxn = b1
a21x1 + a22x2 + … + a2nxn = b2
⠇
an1x1 + an2x2 + … + annxn = bn
We assume that the system of equations Ax = B has a unique solution. The diagonal or pivot elements of the coefficient matrix A do not include any zero entries to discover the solution to this system of equations.
Now, we will begin to solve equation 1 for x1, equation 2 for x2, and so on equation n for xn; we get
x1 = 1/a11 [b1 – a12x2 – a13x3 – … – a1nxn]
x2 = 1/a22 [b2 – a21x1 – a23x3 – … – a2nxn]
⠇
xn = 1/ann [bn – an1x1 – an3x3 – … – an n – 1xn – 1]
We can obtain first approximations for the solution x(1) = (x1(1), x2(1),..., xn(1)) by making an initial guess for the solution x(0) = (x1(0), x2(0),..., xn(0)) and substituting these values exclusively to the right-hand side of the above equations. As we repeat this process, we obtain a sequence of approximations, x(k), which converges to the exact solution of the system of equations up to a certain error limit as k increases.
Note:
- The diagonal is dominating in the matrix.
- The matrix should be symmetrical and positive.
Example matrix:
10 5 3
2 10 4
1 5 10
Algorithm:
An overview of the Gauss-Seidel algorithm is given below:
- Set up a first guess for the solution vector.
- Continue doing so until convergence is reached.
- Update the solution vector for each equation in the system using the current values of the other variables. To accomplish this, solve for the variable in the equation using the known values and the updated values as substitutes.
- Calculate the difference between the revised solution vector and the prior solution vector to verify the convergence condition. Exit the loop if the difference is less than a defined tolerance.
- Replace the outdated solution vector with the original one.
- The final solution vector contains the estimated answer to the system of equations.
Program:
#include<stdio.h>
int main()
{
float a[10][10], b[10], x[10], y[10];
int n = 0, m = 0, i = 0, j = 0;
printf("Enter size of matrix");
scanf("%d",&n);
printf("Enter the coefficients of equations\n");
for (i = 0; i < n; i++)
{
for (j = 0; j < n; j++)
{
scanf("%f",&a[i][j]);
}
}
//Entering RHS values
printf("Enter RHS values\n");
for (i = 0; i < n; i++)
{
scanf("%f",&b[i]);
}
//Input initial values of x
printf("Enter initial values of x\n");
for (i = 0; i < n; i++)
{
scanf("%f",&x[i]);
}
//Input number of iterations
printf("\nEnter the no. of iteration : ");
scanf("%d",&m);
//Iterating a loop for no of iterations
while (m > 0)
{
for (i = 0; i < n; i++)
{
y[i] = (b[i] / a[i][i]);
for (j = 0; j < n; j++)
{
if (j == i)
continue;
y[i] = y[i] - ((a[i][j] / a[i][i]) * x[j]);
x[i] = y[i];
}
printf("x%d = %f ", i + 1, y[i]);
}
printf("\n");
m--;
}
return 0;
}
Output:
![Gauss Seidel method in C](https://static.tutorialandexample.com/c/gauss-seidel-method-in-c1.png)
Advantages of the Gauss-Seidel method in C:
The Gauss-Seidel technique is a common option for resolving systems of linear equations because it has several benefits:
Memory effectiveness: The Gauss-Seidel approach does not require storing the whole coefficients matrix. Instead, it only needs to maintain track of one row at a time, lowering the memory needed. It is especially helpful for complex equation systems because memory utilization might become a bottleneck in these situations.
Parallelizability: The Gauss-Seidel method can be used parallel with other computational processes. Since each equation may be changed independently, the workload can be split among several processors or threads. This parallelizability can result in a sizable speedup on parallel architectures when solving complex equation systems.
Simpleness: The Gauss-Seidel method is comparatively simple to comprehend and use. It is approachable for those new to programming or numerical methods because it only requires straightforward iterative updates to the solution vector.
In-Place Updates: The Gauss-Seidel technique updates the solution vector in place. Therefore, no additional storage space is needed for temporarily computations. It significantly lowers memory needs and may be helpful when memory is scarce.
Successful Development: The Gauss-Seidel method's iterative structure enables continual refinement of the result. Each iteration of the algorithm advances the answer, getting it closer to the solution. When an approximative answer is accepted, subsequent iterations can be acceptable, and further refinement can be helpful.
Convergence: The Gauss-Seidel method frequently converges faster than other iterative techniques like the Jacobi method. It is especially true for diagonally dominating or symmetrically positive definite systems. The Gauss-Seidel method frequently converges quickly to the solution by updating the solution vector one equation at a time.
Disadvantages of the Gauss-Seidel method in C:
The Gauss-Seidel method has several benefits but also some drawbacks and restrictions:
Order of Updates: The solution vector is updated by the Gauss-Seidel technique sequentially, one equation at a time. The updates' chronological order may impact the method's accuracy and convergence behavior. The chosen update order may occasionally cause slower convergence or even divergence.
Convergence Problems: Convergence for all systems of linear equations is not guaranteed by the Gauss-Seidel method. The coefficient matrix must be diagonally dominant or symmetrically positive definite for it to converge. The procedure may stay consistent and yield accurate results if these requirements are met.
Dependence on Initial Guess: The Gauss-Seidel method's convergence can be considerably impacted by the initial guess for the solution vector. An incorrect initial guess could lead the algorithm to converge slowly or not at all. Finding a reliable first guess can be difficult, particularly for intricate systems of equations.
Lack of Parallelism: Compared to other iterative approaches, the Gauss-Seidel method is less parallelizable. The update of each equation depends on the current values of other variables, which makes efficient parallelization of the computations difficult. The approach may not fully use parallel architectures, resulting in less-than-ideal performance.
Slow Convergence: Even when the Gauss-Seidel technique converges, it may do so more slowly than other iterative approaches. The system's characteristics, such as the eigenvalues of the coefficient matrix, affect how quickly the system converges. In some circumstances, the procedure could need a lot of iterations to find a workable answer.
It's crucial to consider these drawbacks when determining if the Gauss-Seidel approach is appropriate for a given situation. Alternative procedures or techniques can be more suitable, depending on the features of the system and the desired precision.