lab.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #include<math.h>
  4. // Необязательный флаг для дебага (можно удалить)
  5. #define DEBUG 0
  6. // Фиксированная размерность матрицы (обрабатывается препроцессором, просто alias)
  7. #define N 10
  8. // Тип вектора
  9. typedef double vec[N];
  10. // Тип матрицы
  11. typedef double mat[N][N];
  12. // Структура для удобного сбора данных
  13. struct result {
  14. double lambda;
  15. int M;
  16. };
  17. // Скалярное произведение
  18. double dot(vec v, vec u) {
  19. double res = 0;
  20. for(int i = 0; i < N; i++) res += v[i]*u[i];
  21. return res;
  22. }
  23. // Прибавление вектора
  24. void add(vec v, vec u) {
  25. for(int i = 0; i < N; i++) {
  26. v[i] += u[i];
  27. }
  28. }
  29. // Функция для вычисления сдвига
  30. void shift(mat m, double s) {
  31. for(int i = 0; i < N; i++) {
  32. m[i][i] = m[i][i] - s;
  33. }
  34. }
  35. // Умножение матрицы на вектор (res = m*v)
  36. void apply(mat m, vec v, vec res) {
  37. for(int i = 0; i < N; i++) {
  38. res[i] = 0;
  39. for(int j = 0; j < N; j++) {
  40. res[i] += m[i][j] * v[j];
  41. }
  42. }
  43. }
  44. // Функция нормализации
  45. void normalize(vec v){
  46. double norm = sqrt(dot(v, v));
  47. for(int i = 0; i < N; i++) {
  48. v[i] /= norm;
  49. }
  50. }
  51. // Функция для вычисления ошибки между векторами
  52. double error_vector(vec x, vec x1) {
  53. double error = 0;
  54. for (int i = 0; i < N; i++) {
  55. error = fmax(error, fabs(fabs(x[i]) - fabs(x1[i])));
  56. }
  57. return error;
  58. }
  59. // Копирование вектора
  60. void copy_vec(vec v, vec copy) {
  61. for(int i = 0; i < N; i++) {
  62. copy[i] = v[i];
  63. }
  64. }
  65. // Чтение матрицы из файла
  66. void mat_from_file(FILE* f, mat m) {
  67. double d;
  68. for (int i = 0; i < N; i++) {
  69. for (int j = 0; j < N; j++) {
  70. fscanf(f, "%lf,", &d);
  71. m[i][j] = d;
  72. }
  73. }
  74. }
  75. // Чтение вектора из файла
  76. void vec_from_file(FILE* f, vec v) {
  77. double d;
  78. for (int i = 0; i < N; i++) {
  79. fscanf(f, "%lf,", &d);
  80. v[i] = d;
  81. }
  82. }
  83. // Красивая печать матрицы
  84. void print_mat(mat m) {
  85. for(int j = 0; j < N; j++) {
  86. printf("-----------|");
  87. }
  88. printf("\n");
  89. for(int i = 0; i < N; i++) {
  90. for(int j = 0; j < N; j++) {
  91. printf("%10.4lf |", m[i][j]);
  92. }
  93. printf("\n");
  94. for(int j = 0; j < N; j++) {
  95. printf("-----------|");
  96. }
  97. printf("\n");
  98. }
  99. }
  100. // Красивая печать вектора
  101. void print_vec(vec v) {
  102. for(int j = 0; j < N; j++) {
  103. printf("%10.4lf |", v[j]);
  104. }
  105. printf("\n");
  106. }
  107. struct result solve(mat m, vec x, double shift_v, double eps, vec eigen_vector) {
  108. struct result res;
  109. shift(m, shift_v);
  110. res.M = 0;
  111. vec y;
  112. double lambda2;
  113. normalize(x);
  114. for (int i = 0; i < 100; i++) {
  115. apply(m, x, y);
  116. res.lambda = shift_v + dot(x, y)/dot(x,x);
  117. res.M++;
  118. normalize(y);
  119. copy_vec(y, eigen_vector);
  120. copy_vec(y, x);
  121. if (fabs((res.lambda-lambda2)/lambda2) < eps){
  122. break;
  123. }
  124. lambda2 = res.lambda;
  125. }
  126. return res;
  127. }
  128. int main(){
  129. vec x;
  130. FILE* f;
  131. if ((f = fopen("matrix.txt", "r")) == NULL) return 1;
  132. FILE* fs;
  133. if ((fs = fopen("value_shift.txt", "r")) == NULL) return 1;
  134. FILE* vf;
  135. if ((vf = fopen("vector_shift.txt", "r")) == NULL) return 1;
  136. FILE* file;
  137. if ((file = fopen("lab_6_error.csv", "w+")) == NULL) return 1;
  138. mat m;
  139. vec eigen_vector;
  140. for (int i = 0; i < 9; i++) {
  141. for(int j = 0; j < N; j++) {
  142. x[j] = 1;
  143. }
  144. mat_from_file(f, m);
  145. vec_from_file(vf, eigen_vector);
  146. double shift_v;
  147. fscanf(fs, "%lf\n", &shift_v);
  148. vec eigen_approximate;
  149. struct result solu = solve(m, x, shift_v, pow(10,-10), eigen_approximate);
  150. print_vec(eigen_vector);
  151. print_vec(eigen_approximate);
  152. printf("\n");
  153. fprintf(file, "%e, %e, %e\n", pow(10,(i-2)), fabs(pow(10,i-1) - solu.lambda)/pow(10,i-1), error_vector(eigen_vector,eigen_approximate));
  154. }
  155. fclose(f);
  156. fclose(fs);
  157. fclose(file);
  158. }