fork(1) download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <math.h>
  4.  
  5. #define SIZE 4
  6. #define EPSILON 1e-9
  7. #define MAX_ITER 1000
  8.  
  9. double vector_norm(double *v, int n) {
  10. double sum = 0.0;
  11. for (int i = 0; i < n; i++) {
  12. sum += v[i] * v[i];
  13. }
  14. return sqrt(sum);
  15. }
  16.  
  17. void mat_vec_mul(double A[SIZE][SIZE], double *x, double *y, int n) {
  18. for (int i = 0; i < n; i++) {
  19. y[i] = 0.0;
  20. for (int j = 0; j < n; j++) {
  21. y[i] += A[i][j] * x[j];
  22. }
  23. }
  24. }
  25.  
  26. int main() {
  27. double A[SIZE][SIZE] = {
  28. {1.0, 2.0, 3.0, 5.0},
  29. {3.0, -5.0, 1.0, 4.0},
  30. {5.0, 9.0, 2.0, -6.0},
  31. {1.0, 7.0, 4.0, 1.0}
  32. };
  33.  
  34. double x[SIZE], y[SIZE], Ax[SIZE];
  35. double lambda_old = 0.0, lambda_new = 0.0;
  36.  
  37. for (int i = 0; i < SIZE; i++) {
  38. x[i] = 1.0;
  39. }
  40.  
  41. printf("べき乗法による収束状況の確認 \n");
  42. printf("初期ベクトル x(0): [%.1f, %.1f, %.1f, %.1f]\n\n", x[0], x[1], x[2], x[3]);
  43. printf("%-5s | %-12s | %-12s | %-30s\n", "Iter", "固有値(新)", "差分値", "固有ベクトルx(k)");
  44. int actual_iters = 0;
  45. for (int iter = 1; iter <= MAX_ITER; iter++) {
  46. actual_iters = iter;
  47. mat_vec_mul(A, x, y, SIZE);
  48.  
  49. double norm_y = vector_norm(y, SIZE);
  50. if (norm_y < 1e-12) break;
  51. for (int i = 0; i < SIZE; i++) {
  52. y[i] /= norm_y;
  53. }
  54.  
  55. mat_vec_mul(A, y, Ax, SIZE);
  56. double num = 0.0, den = 0.0;
  57. for (int i = 0; i < SIZE; i++) {
  58. num += y[i] * Ax[i];
  59. den += y[i] * y[i];
  60. }
  61. lambda_new = num / den;
  62. double diff = fabs(lambda_new - lambda_old);
  63.  
  64.  
  65. if (iter <= 5 || iter % 5 == 0) {
  66. printf("%-5d | %-12.8f | %-12.4e | [%.5f, %.5f, %.5f, %.5f]\n",
  67. iter, lambda_new, diff, y[0], y[1], y[2], y[3]);
  68. }
  69.  
  70. if (diff < EPSILON) {
  71. if (iter % 5 != 0 && iter > 5) { // 最終ステップを確実に表示させる
  72. printf("%-5d | %-12.8f | %-12.4e | [%.5f, %.5f, %.5f, %.5f]\n",
  73. iter, lambda_new, diff, y[0], y[1], y[2], y[3]);
  74. }
  75. break;
  76. }
  77.  
  78. // 次のステップに向けて x を更新
  79. for (int i = 0; i < SIZE; i++) {
  80. x[i] = y[i];
  81. }
  82. lambda_old = lambda_new;
  83. }
  84.  
  85.  
  86. printf("反復回数 %d 回で収束しました。\n", actual_iters);
  87. printf("\n=== 計算結果 ===\n");
  88. printf("最大固有値 (λ1) : %.10f\n", lambda_new);
  89. printf("対応する固有ベクトル (x1) : [");
  90. for (int i = 0; i < SIZE; i++) {
  91. printf("%.10f%s", y[i], (i == SIZE - 1) ? "]\n" : ", ");
  92. }
  93.  
  94. printf("\n=== 検証: Ax と λx の比較 ===\n");
  95. mat_vec_mul(A, y, Ax, SIZE); // Axを再計算
  96. printf("Ax : [");
  97. for (int i = 0; i < SIZE; i++) {
  98. printf("%.6f%s", Ax[i], (i == SIZE - 1) ? "]\n" : ", ");
  99. }
  100. printf("λ * x : [");
  101. for (int i = 0; i < SIZE; i++) {
  102. printf("%.6f%s", lambda_new * y[i], (i == SIZE - 1) ? "]\n" : ", ");
  103. }
  104.  
  105. return 0;
  106. }
  107.  
  108.  
Success #stdin #stdout 0.01s 5312KB
stdin
Standard input is empty
stdout
べき乗法による収束状況の確認 
初期ベクトル x(0): [1.0, 1.0, 1.0, 1.0]

Iter  | 固有値(新) | 差分値    | 固有ベクトルx(k)        
1     | 7.06015038   | 7.0602e+00   | [0.55069, 0.15019, 0.50063, 0.65081]
2     | 7.56272200   | 5.0257e-01   | [0.68485, 0.48918, 0.14675, 0.51975]
3     | 7.79665790   | 2.3394e-01   | [0.53345, 0.20811, 0.56744, 0.59172]
4     | 8.36088120   | 5.6422e-01   | [0.66241, 0.41253, 0.25086, 0.57281]
5     | 8.52180634   | 1.6093e-01   | [0.58882, 0.28456, 0.47179, 0.59139]
10    | 8.69758623   | 9.4221e-03   | [0.62524, 0.34192, 0.38099, 0.58908]
15    | 8.69591999   | 7.0277e-04   | [0.62205, 0.33644, 0.39006, 0.58967]
20    | 8.69624282   | 7.2845e-05   | [0.62237, 0.33699, 0.38916, 0.58962]
25    | 8.69621204   | 7.2757e-06   | [0.62234, 0.33693, 0.38925, 0.58962]
30    | 8.69621514   | 7.2934e-07   | [0.62235, 0.33694, 0.38924, 0.58962]
35    | 8.69621483   | 7.3086e-08   | [0.62234, 0.33694, 0.38924, 0.58962]
40    | 8.69621486   | 7.3240e-09   | [0.62234, 0.33694, 0.38924, 0.58962]
45    | 8.69621486   | 7.3395e-10   | [0.62234, 0.33694, 0.38924, 0.58962]
反復回数 45 回で収束しました。

=== 計算結果 ===
最大固有値 (λ1)          : 8.6962148610
対応する固有ベクトル (x1) : [0.6223447229, 0.3369369049, 0.3892384556, 0.5896219066]

=== 検証: Ax と λx の比較 ===
Ax    : [5.412043, 2.930076, 3.384901, 5.127479]
λ * x : [5.412043, 2.930076, 3.384901, 5.127479]