fork download
  1. #include <stdio.h>
  2. #include <math.h>
  3. #define N 4
  4.  
  5. double beki(double A[][N], double x[N], double l);
  6. double con(double A[][N], double x[N]);
  7.  
  8. double beki(double A[][N], double x[N], double l){
  9. int n=1;
  10. while(1){
  11. double xi[N] = {0};
  12.  
  13. for(int i=0;i<N;i++){
  14. for(int j=0;j<N;j++){
  15. xi[i] += A[i][j] * x[j];
  16. }
  17. }
  18.  
  19. l = xi[0];
  20. for(int i=0;i<N;i++){
  21. xi[i] /= l;
  22. }
  23.  
  24. printf("λ%d = %f\n",n,l);
  25. printf("x%d = ",n);
  26. for(int i=0;i<N;i++){
  27. printf("%f, ", xi[i]);
  28. }
  29. printf("\n");
  30. printf("\n");
  31.  
  32. if(fabs(x[1]-xi[1])<pow(10, -5) && fabs(x[2]-xi[2])<pow(10, -5) && fabs(x[3]-xi[3])<pow(10, -5)){
  33. for(int i=0;i<N;i++){
  34. x[i] = xi[i];
  35. }
  36. break;
  37. }
  38.  
  39. for(int i=0;i<N;i++){
  40. x[i] = xi[i];
  41. }
  42. n++;
  43. }
  44. }
  45.  
  46. double con(double A[][N], double x[N]){
  47. double xi[N] = {0};
  48. double l = 0;
  49.  
  50. for(int i=0;i<N;i++){
  51. for(int j=0;j<N;j++){
  52. xi[i] += A[i][j] * x[j];
  53. }
  54. }
  55.  
  56. l = xi[0];
  57. for(int i=0;i<N;i++){
  58. xi[i] /= l;
  59. }
  60.  
  61. printf("固有値・固有ベクトル確認\n");
  62. printf("λ = %f\n", l);
  63. printf("x = ");
  64. for(int i=0;i<N;i++){
  65. printf("%f, ", xi[i]);
  66. }
  67. printf("\n");
  68. }
  69.  
  70. int main(void) {
  71. double A[N][N] = {
  72. {2, 1, 5, 3},
  73. {3, 0, 1, 6},
  74. {1, 4, 3, 3},
  75. {8, 2, 0, 1}
  76. };
  77. double x[N] = {1, 0, 0, 0};
  78. double l = 0;
  79.  
  80. beki(A, x, l);
  81. con(A, x);
  82.  
  83. return 0;
  84. }
  85.  
Success #stdin #stdout 0s 5276KB
stdin
Standard input is empty
stdout
λ1 = 2.000000
x1 = 1.000000, 1.500000, 0.500000, 4.000000, 

λ2 = 18.000000
x2 = 1.000000, 1.527778, 1.138889, 0.833333, 

λ3 = 11.722222
x3 = 1.000000, 0.779621, 1.111374, 1.014218, 

λ4 = 11.379147
x4 = 1.000000, 0.896085, 0.922324, 0.929196, 

λ5 = 10.295294
x5 = 1.000000, 0.922509, 0.984809, 1.041385, 

λ6 = 10.970711
x6 = 1.000000, 0.932767, 0.981579, 0.992315, 

λ7 = 10.817608
x7 = 1.000000, 0.918454, 0.984760, 1.003720, 

λ8 = 10.853416
x8 = 1.000000, 0.922021, 0.980268, 0.998822, 

λ9 = 10.819826
x9 = 1.000000, 0.921752, 0.982026, 1.002129, 

λ10 = 10.838272
x10 = 1.000000, 0.922177, 0.981658, 1.000679, 

λ11 = 10.832504
x11 = 1.000000, 0.921831, 0.981834, 1.001157, 

λ12 = 10.834469
x12 = 1.000000, 0.921944, 0.981709, 1.000955, 

λ13 = 10.833353
x13 = 1.000000, 0.921916, 0.981761, 1.001061, 

λ14 = 10.833904
x14 = 1.000000, 0.921932, 0.981745, 1.001014, 

λ15 = 10.833699
x15 = 1.000000, 0.921922, 0.981752, 1.001032, 

λ16 = 10.833778
x16 = 1.000000, 0.921926, 0.981748, 1.001024, 

固有値・固有ベクトル確認
λ = 10.833740
x = 1.000000, 0.921925, 0.981750, 1.001028,