P a g e | 40
DATE: 17/07/2025
ASSIGNMENT – 15
Problem Statement:
Write a program in C to find solve the following equations using Gauss
Seidel Method:
10x1 + x2 + x3 = 12
x1 + 10x2 + x3 = 12
x1 + x2 + 10x3 = 12
Algorithm:
Step 1: START
Step 2: Input the number of equations (n).
Step 3: Display the entered system of linear equations.
Step 4: Check for diagonal dominance in matrix A to ensure convergence of
the Gauss-Seidel method.
• For each row, verify that the magnitude of the diagonal element
is greater than the sum of the magnitudes of the other elements
in that row.
• If diagonal dominance is not satisfied, terminate the program with
a warning.
Step 5: Accept initial guesses for the solution vector x[ ] and an error
tolerance e.
Step 6: Iteratively update each variable:
• For every variable x[i], calculate its new value using the latest
available values (including updates from the current iteration).
• Update x[i] immediately after computation.
• Print the values of all variables for each iteration.
• Track convergence by counting variables where the change is less
than the error tolerance.
Step 7: Repeat the iteration until all variables satisfy the error tolerance.
Step 8: Output the approximate solution for all variables.
Step 9: END
Source Code:
/* Gauss Seidel Method */
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main()
{
float a[10][10], b[10], x[10], sum, e;
int i, j, n, flag = 0, key, iter = 1;
printf("\nThis program illustrates Gauss-Seidel method to solve system
AX = B\n");
P a g e | 41
// Input size
printf("\nEnter the dimensions of coefficient matrix n (n<=10): ");
scanf("%d", &n);
// Input coefficient matrix A
printf("\nEnter the elements of matrix A (%d x %d):\n", n, n);
for (i = 0; i < n; i++) {
for (j = 0; j < n; j++) {
scanf("%f", &a[i][j]);
}
}
// Input RHS vector B
printf("\nEnter the elements of matrix B (%d elements):\n", n);
for (i = 0; i < n; i++) {
scanf("%f", &b[i]);
}
// Display system
printf("\nThe system of linear equations:\n");
for (i = 0; i < n; i++) {
for (j = 0; j < n; j++) {
printf("(%.2f)x%d", a[i][j], j + 1);
if (j < n - 1) printf(" + ");
}
printf(" = (%.2f)\n", b[i]);
}
// Check diagonal dominance
for (i = 0; i < n; i++) {
sum = 0;
for (j = 0; j < n; j++) {
sum += fabs(a[i][j]);
}
sum -= fabs(a[i][i]);
if (fabs(a[i][i]) < sum) {
flag = 1;
break;
}
}
if (flag == 1) {
printf("\nThe system is NOT diagonally dominant, Gauss-Seidel may
not converge.\n");
return 0;
} else {
printf("\nThe system IS diagonally dominant (good for Gauss-
Seidel).\n");
// Initial guess
printf("\nEnter the initial approximations:\n");
P a g e | 42
for (i = 0; i < n; i++) {
printf("x[%d] = ", i + 1);
scanf("%f", &x[i]);
}
// Error tolerance
printf("\nEnter the error tolerance: ");
scanf("%f", &e);
}
// Iterations
printf("\nIteration results:\n");
for (i = 0; i < n; i++) {
printf(" x[%d]\t", i + 1);
}
printf("\n");
key = 0;
while (1) {
key = 0;
printf("%3d:", iter++);
for (i = 0; i < n; i++) {
sum = b[i];
for (j = 0; j < n; j++) {
if (j != i)
sum -= a[i][j] * x[j]; // uses latest updated x[]
}
float x_new = sum / a[i][i];
if (fabs(x_new - x[i]) < e) {
key++;
}
x[i] = x_new; // update immediately (Gauss-Seidel feature)
printf(" %10.6f ", x[i]);
}
printf("\n");
if (key == n)
break;
}
// Final solution
printf("\nAn approximate solution is:\n");
for (i = 0; i < n; i++) {
printf("x[%d] = %.6f\n", i + 1, x[i]);
}
return 0;
}
P a g e | 43
Input/Output:
This program illustrates Gauss-Seidel method to solve system AX = B
Enter the dimensions of coefficient matrix n (n<=10): 3
Enter the elements of matrix A (3 x 3):
10 1 1
1 10 1
1 1 10
Enter the elements of matrix B (3 elements):
12 12 12
The system of linear equations:
(10.00)x1 + (1.00)x2 + (1.00)x3 = (12.00)
(1.00)x1 + (10.00)x2 + (1.00)x3 = (12.00)
(1.00)x1 + (1.00)x2 + (10.00)x3 = (12.00)
The system IS diagonally dominant (good for Gauss-Seidel).
Enter the initial approximations:
x[1] = 0
x[2] = 0
x[3] = 0
Enter the error tolerance: 0.001
Iteration results:
x[1] x[2] x[3]
1: 1.200000 1.080000 0.972000
2: 0.994800 1.003320 1.000188
3: 0.999649 1.000016 1.000033
4: 0.999995 0.999997 1.000001
An approximate solution is:
x[1] = 0.999995
x[2] = 0.999997
x[3] = 1.000001
Discussion:
This program efficiently implements the Gauss-Seidel iterative method to
solve a system of linear equations, making use of the property of diagonal
dominance for guaranteed convergence. Unlike the Jacobi method, the
Gauss-Seidel method updates each variable as soon as a new value is
calculated, allowing subsequent variables in the same iteration to use the
most recent values. This typically results in faster convergence for well-
conditioned systems. The algorithm provides step-by-step iteration outputs,
enabling users to observe the progression and accuracy of the solution. It is
particularly useful for solving moderately sized linear systems in engineering
and scientific computations where direct methods are impractical.