method.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. #include <math.h>
  2. #include <stdlib.h>
  3. #include <stdio.h>
  4. #include "header.h"
  5. double* create_grid_even(int n, double a, double b) {
  6. double* h = malloc(sizeof(double) * n);
  7. if (h == NULL) {
  8. exit(16);
  9. }
  10. for (int i = 0; i < n;i++) {
  11. h[i] = a + ((b - a) / (n - 1)) * i;
  12. }
  13. return h;
  14. }
  15. double solve_eqution(double* right_term) {
  16. //слева полагаем, что стоит только неизвестная с коэффициентом 1
  17. // right_term - массив из двух элементов, на первой позиции - коэф. при неизветсной, на второй - нулевой коэф.
  18. return right_term[1] / (1 - right_term[0]);
  19. }
  20. double* adams_moulten(double* start_points, double a, double b, double(*f)(double, double), int n) {
  21. double* yy = malloc(sizeof(double) * n);
  22. double h = (b - a) / (n - 1);
  23. if (yy == NULL) {
  24. exit(16);
  25. }
  26. for (int o = 0;o < 2;o++) {
  27. yy[o] = start_points[o];
  28. }
  29. for (int i = 2;i < n;i++) {
  30. double* term = f_eq_form(a + i * h);
  31. term[0] *= 5 * h / 12;
  32. term[1] *= 5 * h / 12;
  33. term[1] += yy[i - 1] + (h / 12) * (8 * f(a + (i - 1) * h, yy[i - 1]) - f(a + (i - 2) * h, yy[i - 2]));
  34. yy[i] = solve_eqution(term);
  35. }
  36. return yy;
  37. }
  38. double** runge_adams(double eps, double y0, double a, double b, double(*f)(double, double), int* n_, int* len_h) {
  39. double* x = malloc(sizeof(double)*2);
  40. double* y = malloc(sizeof(double)*2);
  41. if (x == NULL) {
  42. exit(16);
  43. }
  44. if (y == NULL) {
  45. exit(16);
  46. }
  47. long double* h_all = malloc(sizeof(long double));// регистрирует все значения шагов
  48. long double* h_made = malloc(sizeof(long double)*2);// регистрирует значения шага, соответствующие сделаным шагам
  49. if (h_all == NULL) {
  50. exit(16);
  51. }
  52. if (h_made == NULL) {
  53. exit(16);
  54. }
  55. double* x2 = malloc(sizeof(double) * 4);
  56. double* y2 = malloc(sizeof(double) * 4);
  57. if (x2 == NULL) {
  58. exit(16);
  59. }
  60. if (y2 == NULL) {
  61. exit(16);
  62. }
  63. int k = 1, i = 2;
  64. double dif, h = (b - a) / 2;
  65. double y_1 = y0 + h * f(a, y0);
  66. double y_12 = y0 + (h / 2) * f(a, y0);
  67. double y_2 = y_12 + (h / 2) * f(a + h / 2, y_12);
  68. while (fabs(y_2 - y_1) > eps / 100) {
  69. h /= 2;
  70. y_1 = y0 + h * f(a + h, y0);
  71. y_12 = y0 + (h / 2) * f(a + h, y0);
  72. y_2 = y_12 + (h / 2) * f(a + h / 2, y_12);
  73. }
  74. y[0] = y0;
  75. y[1] = y_1;
  76. y2[0] = y_12;
  77. y2[1] = y_2;
  78. x[0] = a;
  79. x[1] = a + h;
  80. x2[0] = a+ h/2;
  81. x2[1] = a + h;
  82. h_made[0] = 1;
  83. h_made[1] = h;
  84. h = (b - a) / 2;
  85. h_all[0] = h;
  86. printf("eps = %e\n", eps);
  87. double* term;
  88. while (x[i - 1] + h <= b) {
  89. h_all = realloc(h_all, sizeof(long double) * (k + 1));
  90. if (h_all == NULL) {
  91. exit(16);
  92. }
  93. h_all[k] = h;
  94. term = f_eq_form(x[i - 1] + h);
  95. term[0] *= 5 * h / 12;
  96. term[1] *= 5 * h / 12;
  97. term[1] += y[i - 1] + (h / 12) * (8 * f(x[i - 1], y[i - 1]) - f(x[i - 2], y[i - 2]));
  98. y_1 = solve_eqution(term);
  99. term = f_eq_form(x[i - 1] + h / 2);
  100. term[0] *= 5 * h / 24;
  101. term[1] *= 5 * h / 24;
  102. term[1] += y[i - 1] + (h / 24) * (8 * f(x2[1], y2[1]) - f(x2[0], y2[0]));
  103. y_12 = solve_eqution(term);
  104. term = f_eq_form(x[i - 1] + h);
  105. term[0] *= 5 * h / 24;
  106. term[1] *= 5 * h / 24;
  107. term[1] += y_12 + (h / 24) * (8 * f(x[i - 1] + h / 2, y_12) - f(x2[ 1], y2[1]));
  108. y_2 = solve_eqution(term);
  109. dif = fabs(y_2 - y_1) / 7;
  110. x2[3] = x[i - 1] + h;
  111. x2[2] = x[i - 1] + h / 2;
  112. y2[3] = y_2;
  113. y2[2] = y_12;
  114. if (dif > eps/8) {
  115. h = h / 2;
  116. }
  117. else {
  118. x = realloc(x, sizeof(double) * (i + 1));
  119. if (x == NULL) {
  120. exit(16);
  121. }
  122. x[i] = x[i - 1] + h;
  123. y = realloc(y, sizeof(double) * (i + 1));
  124. if (y == NULL) {
  125. exit(16);
  126. }
  127. y[i] = y_1;
  128. x2[0] = x2[2];
  129. x2[1] = x2[3];
  130. y2[0] = y2[2];
  131. y2[1] = y2[3];
  132. h_made = realloc(h_made, sizeof(long double) * (i + 1));
  133. if (h_made == NULL) {
  134. exit(16);
  135. }
  136. h_made[i] = h;
  137. i += 1;
  138. }
  139. k++;
  140. }
  141. h_all[k - 1] = h;
  142. *len_h = k;
  143. *n_ = i;
  144. double** ans = malloc(sizeof(double*) * 4);
  145. ans[0] = x;
  146. ans[1] = y;
  147. ans[2] = h_all;
  148. ans[3] = h_made;
  149. free(x2);
  150. free(y2);
  151. return ans;
  152. }
  153. double uglify_initial(double initial, int max_procent, int min_procent) {
  154. double procent = min_procent + (double)(max_procent - min_procent) * ((double)rand() / (RAND_MAX));
  155. return initial - procent * initial / 100;
  156. }