-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgauss_seidel.c
64 lines (59 loc) · 1.89 KB
/
gauss_seidel.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#include<stdio.h>
float relErr(float num1, float num2){
float diff = num1-num2>=0 ? num1-num2 : num2-num1;
return (diff/num2);
}
int main(){
int n,maxItr;
float err;
printf("Enter the order of equations : ");
scanf("%d",&n);
/*
The coefficients are stored in a 2-dimensional array from a11 to a1(n+1)
and so on
*/
float coeff[n][n+1];
for(int i=0;i<n;i++){
for(int j=0;j<n+1;j++){
printf("Enter the coefficient of arr[%d][%d] : ",i,j);
scanf("%f",&coeff[i][j]);
}
}
printf("Enter the value of err and max iterations: ");
scanf("%f %d",&err, &maxItr);
//Declare an array to store the values of Xi's and setting all of them to zero
float x[n];
for(int i=0;i<n;i++) x[i]=0;
for(int k=1;k<=maxItr;k++){
float big = 0;
for(int i=1;i<=n;i++){
float sum=0;
for(int j=1;j<=n;j++){
if(i!=j){
sum = sum + (coeff[i-1][j-1]*x[j-1]);
}
}
float temp = (coeff[i-1][n+1-1]-sum)/coeff[i-1][i-1];
//printf("The relative err is: %f", relErr(x[i-1], temp));
if(relErr(x[i-1], temp)>big){
big = relErr(x[i-1], temp);
}
x[i-1] = temp;
}
if(big<=err){
printf("\nConverges to solution: ");
for(int i=1;i<=n;i++){
printf("x(%d)=%f ",i,x[i-1]);
}
int dump = 0;
scanf("%d", dump); //To hold the output on the screen
return 0; //Equivalent to stopping the program
}
}
printf("Does not converge in given iterations");
printf("Following are the corresponding values of X's obtained: ");
for(int i=1;i<=n;i++){
printf("x(%d)=%f ",i,x[i-1]);
}
return 0;
}