fork(1) download
  1. #include <stdio.h>
  2. #include <math.h>
  3.  
  4. #define N 4
  5. #define MAX_ITER 150
  6. #define EPS 1e-7
  7.  
  8. void print_system(double A[N][N], double b[N]);
  9. void jacobi_solver(double A[N][N], double b[N]);
  10. void gauss_seidel_solver(double A[N][N], double b[N]);
  11. void verify_solution(double A[N][N], double b[N], double x[N]);
  12.  
  13. int main() {
  14. // Preprocessed diagonally dominant system matrices
  15. double A[N][N] = {
  16. { 5.0, -1.0, -2.0, 0.0},
  17. { 0.0, 8.0, 1.0, 7.0},
  18. {-2.0, -7.0, -4.0, 0.0},
  19. { 4.0, 0.0, -3.0, -25.0}
  20. };
  21. double b[N] = {-1.32, 14.41, 0.70, -0.03};
  22.  
  23. print_system(A, b);
  24.  
  25. printf("\n======================================================\n");
  26. printf(" 1. JACOBI METHOD \n");
  27. printf("======================================================\n");
  28. jacobi_solver(A, b);
  29.  
  30. printf("\n======================================================\n");
  31. printf(" 2. GAUSS-SEIDEL METHOD \n");
  32. printf("======================================================\n");
  33. gauss_seidel_solver(A, b);
  34.  
  35. return 0;
  36. }
  37.  
  38. void print_system(double A[N][N], double b[N]) {
  39. printf("Preprocessed Diagonally Dominant Linear System:\n");
  40. for (int i = 0; i < N; i++) {
  41. printf("[ ");
  42. for (int j = 0; j < N; j++) {
  43. printf("%6.1f ", A[i][j]);
  44. }
  45. printf("] [x%d] = [ %6.2f ]\n", i + 1, b[i]);
  46. }
  47. }
  48.  
  49. void jacobi_solver(double A[N][N], double b[N]) {
  50. double x[N] = {0.0, 0.0, 0.0, 0.0}; // Initial guess x^(0)
  51. double x_new[N];
  52. int converged = 0;
  53.  
  54. printf("Iter\tResidual SS\tx1\t\tx2\t\tx3\t\tx4\n");
  55. printf("------------------------------------------------------------------------\n");
  56.  
  57. for (int k = 1; k <= MAX_ITER; k++) {
  58. for (int i = 0; i < N; i++) {
  59. double sum = 0.0;
  60. for (int j = 0; j < N; j++) {
  61. if (j != i) {
  62. sum += A[i][j] * x[j];
  63. }
  64. }
  65. x_new[i] = (b[i] - sum) / A[i][i];
  66. }
  67.  
  68. // Calculate Residual Sum of Squares
  69. double rss = 0.0;
  70. for (int i = 0; i < N; i++) {
  71. rss += pow(x_new[i] - x[i], 2);
  72. }
  73.  
  74. // Display iteration row
  75. printf("%d\t%.2e\t%.5f\t%.5f\t%.5f\t%.5f\n", k, rss, x_new[0], x_new[1], x_new[2], x_new[3]);
  76.  
  77. // Update values
  78. for (int i = 0; i < N; i++) {
  79. x[i] = x_new[i];
  80. }
  81.  
  82. if (rss < EPS) {
  83. printf("\n>> Converged successfully in %d iterations.\n", k);
  84. converged = 1;
  85. break;
  86. }
  87. }
  88.  
  89. if (!converged) {
  90. printf("\nWarning: Did not reach convergence criteria.\n");
  91. }
  92.  
  93. // --- Explicit Solution Display ---
  94. printf("\n=== FINAL SOLUTION (JACOBI) ===\n");
  95. for (int i = 0; i < N; i++) {
  96. printf("x[%d] = %10.6f\n", i + 1, x[i]);
  97. }
  98.  
  99. verify_solution(A, b, x);
  100. }
  101.  
  102. void gauss_seidel_solver(double A[N][N], double b[N]) {
  103. double x[N] = {0.0, 0.0, 0.0, 0.0}; // Initial guess x^(0)
  104. double x_old[N];
  105. int converged = 0;
  106.  
  107. printf("Iter\tResidual SS\tx1\t\tx2\t\tx3\t\tx4\n");
  108. printf("------------------------------------------------------------------------\n");
  109.  
  110. for (int k = 1; k <= MAX_ITER; k++) {
  111. for (int i = 0; i < N; i++) {
  112. x_old[i] = x[i];
  113. }
  114.  
  115. for (int i = 0; i < N; i++) {
  116. double sum = 0.0;
  117. for (int j = 0; j < N; j++) {
  118. if (j != i) {
  119. sum += A[i][j] * x[j];
  120. }
  121. }
  122. x[i] = (b[i] - sum) / A[i][i];
  123. }
  124.  
  125. // Calculate Residual Sum of Squares
  126. double rss = 0.0;
  127. for (int i = 0; i < N; i++) {
  128. rss += pow(x[i] - x_old[i], 2);
  129. }
  130.  
  131. // Display iteration row
  132. printf("%d\t%.2e\t%.5f\t%.5f\t%.5f\t%.5f\n", k, rss, x[0], x[1], x[2], x[3]);
  133.  
  134. if (rss < EPS) {
  135. printf("\n>> Converged successfully in %d iterations.\n", k);
  136. converged = 1;
  137. break;
  138. }
  139. }
  140.  
  141. if (!converged) {
  142. printf("\nWarning: Did not reach convergence criteria.\n");
  143. }
  144.  
  145. // --- Explicit Solution Display ---
  146. printf("\n=== FINAL SOLUTION (GAUSS-SEIDEL) ===\n");
  147. for (int i = 0; i < N; i++) {
  148. printf("x[%d] = %10.6f\n", i + 1, x[i]);
  149. }
  150.  
  151. verify_solution(A, b, x);
  152. }
  153.  
  154. void verify_solution(double A[N][N], double b[N], double x[N]) {
  155. printf("\n--- In-Code Verification Check (Ax == b) ---\n");
  156. for (int i = 0; i < N; i++) {
  157. double lhs_calc = 0.0;
  158. for (int j = 0; j < N; j++) {
  159. lhs_calc += A[i][j] * x[j];
  160. }
  161. double error = lhs_calc - b[i];
  162. printf("Row %d: Computed Ax = %10.5f | Target b = %10.5f | Delta Error = %e\n",
  163. i + 1, lhs_calc, b[i], error);
  164. }
  165. }
  166.  
Success #stdin #stdout 0s 5280KB
stdin
Standard input is empty
stdout
Preprocessed Diagonally Dominant Linear System:
[    5.0   -1.0   -2.0    0.0 ] [x1] = [  -1.32 ]
[    0.0    8.0    1.0    7.0 ] [x2] = [  14.41 ]
[   -2.0   -7.0   -4.0    0.0 ] [x3] = [   0.70 ]
[    4.0    0.0   -3.0  -25.0 ] [x4] = [  -0.03 ]

======================================================
 1. JACOBI METHOD 
======================================================
Iter	Residual SS	x1		x2		x3		x4
------------------------------------------------------------------------
1	3.34e+00	-0.26400	1.80125	-0.17500	0.00120
2	9.21e+00	0.02625	1.82208	-3.19519	-0.02004
3	1.81e+00	-1.17766	2.21818	-3.37676	0.38882
4	1.50e-01	-1.17107	1.88312	-3.46799	0.21799
5	3.77e-01	-1.27457	2.04401	-2.88494	0.22999
6	1.38e-01	-1.00917	1.96063	-3.11474	0.14346
7	2.78e-02	-1.11777	2.06506	-3.10151	0.21350
8	2.15e-02	-1.09159	2.00213	-3.22998	0.19454
9	1.50e-02	-1.15557	2.03478	-3.13292	0.21414
10	4.03e-03	-1.11021	2.00549	-3.15808	0.19226
11	1.67e-03	-1.12613	2.02778	-3.12950	0.20253
12	1.41e-03	-1.11024	2.01522	-3.16055	0.19656
13	5.42e-04	-1.12518	2.02433	-3.14651	0.20283
14	1.96e-04	-1.11774	2.01709	-3.15499	0.19875
15	1.30e-04	-1.12258	2.02171	-3.14604	0.20096
16	6.52e-05	-1.11807	2.01866	-3.15171	0.19911
17	2.52e-05	-1.12095	2.02099	-3.14863	0.20051
18	1.31e-05	-1.11925	2.01938	-3.15126	0.19968
19	7.24e-06	-1.12063	2.02043	-3.14929	0.20027
20	3.13e-06	-1.11963	2.01967	-3.15045	0.19981
21	1.46e-06	-1.12024	2.02022	-3.14962	0.20011
22	7.84e-07	-1.11980	2.01985	-3.15026	0.19991
23	3.72e-07	-1.12013	2.02011	-3.14984	0.20006
24	1.70e-07	-1.11992	2.01993	-3.15012	0.19996
25	8.60e-08	-1.12006	2.02005	-3.14991	0.20003

>> Converged successfully in 25 iterations.

=== FINAL SOLUTION (JACOBI) ===
x[1] =  -1.120063
x[2] =   2.020050
x[3] =  -3.149911
x[4] =   0.200028

--- In-Code Verification Check (Ax == b) ---
Row 1: Computed Ax =   -1.32054 | Target b =   -1.32000 | Delta Error = -5.434164e-04
Row 2: Computed Ax =   14.41069 | Target b =   14.41000 | Delta Error = 6.884127e-04
Row 3: Computed Ax =    0.69942 | Target b =    0.70000 | Delta Error = -5.808020e-04
Row 4: Computed Ax =   -0.03122 | Target b =   -0.03000 | Delta Error = -1.219528e-03

======================================================
 2. GAUSS-SEIDEL METHOD 
======================================================
Iter	Residual SS	x1		x2		x3		x4
------------------------------------------------------------------------
1	1.36e+01	-0.26400	1.80125	-3.19519	0.34238
2	9.66e-01	-1.18182	1.90106	-2.91095	0.16142
3	1.15e-01	-1.04817	2.02387	-3.19270	0.21662
4	1.29e-02	-1.13630	2.01080	-3.12574	0.19448
5	1.74e-03	-1.11214	2.02180	-3.15708	0.20211
6	2.22e-04	-1.12247	2.01904	-3.14709	0.19925
7	2.97e-05	-1.11903	2.02029	-3.15099	0.20027
8	3.88e-06	-1.12034	2.01988	-3.14963	0.19990
9	5.15e-07	-1.11987	2.02004	-3.15013	0.20004
10	6.77e-08	-1.12005	2.01999	-3.14995	0.19999

>> Converged successfully in 10 iterations.

=== FINAL SOLUTION (GAUSS-SEIDEL) ===
x[1] =  -1.120045
x[2] =   2.019985
x[3] =  -3.149951
x[4] =   0.199987

--- In-Code Verification Check (Ax == b) ---
Row 1: Computed Ax =   -1.32031 | Target b =   -1.32000 | Delta Error = -3.084483e-04
Row 2: Computed Ax =   14.40984 | Target b =   14.41000 | Delta Error = -1.626182e-04
Row 3: Computed Ax =    0.70000 | Target b =    0.70000 | Delta Error = -6.661338e-16
Row 4: Computed Ax =   -0.03000 | Target b =   -0.03000 | Delta Error = -2.498002e-16