main.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. #define _CRT_SECURE_NO_WARNINGS
  2. #include <stdlib.h>
  3. #include <stdio.h>
  4. #include <string.h>
  5. #include <math.h>
  6. #include "header.h"
  7. int main() {
  8. FILE* fp;
  9. FILE* fg;
  10. FILE* fw;
  11. double a;
  12. double b;
  13. int n;
  14. char funcc[2][3] = { "fs","fg" };
  15. char path_grid[100];
  16. char path_weights[100];
  17. char path_poly[100];
  18. double* xh;
  19. double* yh;
  20. double** h;
  21. double* p;
  22. double* coefs;
  23. int start_n, end_n;
  24. start_n = 100;
  25. end_n = 100;
  26. int start_pow, end_pow;
  27. start_pow = 4;
  28. end_pow = 4;
  29. int change = 0;
  30. int change_amount = 3;
  31. for (int c = 0;c <= 3*change;c++) {
  32. for (int f_i = 0; f_i < 2; f_i++) {
  33. if (f_i == 0) {
  34. a = -2;
  35. b = 4;
  36. }
  37. if (f_i == 1) {
  38. a = 0;
  39. b = 5;
  40. }
  41. for (int pow = start_pow;pow <= end_pow;pow++) {
  42. for (n = start_n;n <= end_n;n += 10) {
  43. if (change) {
  44. snprintf(path_poly, 100, "lab9_interpol/a_poly_%s_%d_%d.txt", funcc[f_i], pow, c);
  45. snprintf(path_grid, 100, "lab9_interpol/a_grid_%s_%d_%d.txt", funcc[f_i], pow, c);
  46. snprintf(path_weights, 100, "lab9_interpol/a_weights_%s_%d.txt", funcc[f_i],c);
  47. p = change_p(n, 4,c,change_amount);
  48. fw = fopen(path_weights, "w+");
  49. if (fw == NULL) {
  50. exit(10);
  51. }
  52. for (int i = 0;i < n;i++) {
  53. fprintf(fw, "%lf", p[i]);
  54. if (i != n - 1) {
  55. fprintf(fw, "\n");
  56. }
  57. }
  58. }
  59. else {
  60. snprintf(path_poly, 100, "lab9_interpol/poly_%s_%d_%d.txt", funcc[f_i], pow, n);
  61. snprintf(path_grid, 100, "lab9_interpol/grid_%s_%d_%d.txt", funcc[f_i], pow, n);
  62. p = malloc(sizeof(double) * n);
  63. for (int i = 0;i < n;i++) {
  64. p[i] = 1;
  65. }
  66. }
  67. if (f_i == 0) {
  68. h = generate_ugrid(f1, n, a, b);
  69. }
  70. else {
  71. h = generate_ugrid(f2, n, a, b);
  72. }
  73. xh = malloc(sizeof(double) * n);
  74. yh = malloc(sizeof(double) * n);
  75. for (int i = 0; i < n;i++) {
  76. xh[i] = h[i][0];
  77. yh[i] = h[i][1];
  78. }
  79. printf("%s\n", path_poly);
  80. fg = fopen(path_grid, "w+");
  81. if (fg == NULL) {
  82. exit(10);
  83. }
  84. fp = fopen(path_poly, "w+");
  85. if (fp == NULL) {
  86. exit(10);
  87. }
  88. for (int i = 0;i < n;i++) {
  89. fprintf(fg, "%.25lf %.25lf", h[i][0], h[i][1]);
  90. if (i != n - 1) {
  91. fprintf(fg, "\n");
  92. }
  93. }
  94. fclose(fg);
  95. coefs = MLS_coef(xh, yh, p, pow, n);
  96. for (double x = a;x < b - (double)(b - a) / 10000;x += (double)(b - a) / 10000) {
  97. fprintf(fp, "%lf %lf", x, MLS(x,coefs,pow));
  98. if (x <= b - (double)(b - a) / 5000) {
  99. fprintf(fp, "\n");
  100. }
  101. }
  102. fclose(fp);
  103. free(xh);
  104. free(yh);
  105. for (int i = 0;i < n;i++) {
  106. free(h[i]);
  107. }
  108. free(h);
  109. free(p);
  110. }
  111. }
  112. }
  113. }
  114. return 0;
  115. }