rec_method.c 954 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. #define _CRT_SECURE_NO_WARNINGS
  2. #include <stdlib.h>
  3. #include <stdio.h>
  4. #include <math.h>
  5. #include "header.h"
  6. double integrate_rec_l(double(*f)(double),double prev_S, long int N, double a, double b) {
  7. double S;
  8. long double H = (long double)(b - a) / N;
  9. S = 0;
  10. for (int i = 2;i <= N;i += 2) {
  11. S += f(a + (i-1)*H);
  12. }
  13. return H * S + (0.5) * prev_S;
  14. }
  15. void runge_write(int power, double(*f)(double),double a,double b) {
  16. double S, S2 = 0;
  17. long int N = 1;
  18. int counter = 0;
  19. char path_res[100];
  20. S2 = (b - a) * f(0);
  21. sprintf(path_res, "C:/Users/egorl/source/repos/num_methods_3/lab10/res_%i.txt", power);
  22. FILE* f_res;
  23. f_res = fopen(path_res, "w+");
  24. do {
  25. counter++;
  26. S = S2;
  27. N *= 2;
  28. S2 = integrate_rec_l(f, S, N, a, b);
  29. fprintf(f_res, "%.20lf %.20lf %d %ld\n", S2, (b - a) / N, counter, N);
  30. printf("N = %ld, S = %lf, counter = %d,eps = %e\n", N, S2, counter, pow(10, -power));
  31. } while (fabs(S2 - S) > pow(10, -power));
  32. }