Advertisement
GroX24

sem2 lab1

Feb 8th, 2023
941
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.03 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <math.h>
  4.  
  5. double der1(double x, double h, double(*f)(double)){
  6.     return ((*f)(x + h) - (*f)(x - h)) / (2 * h);
  7. }
  8.  
  9. double der2(double x, double h, double(*f)(double)){
  10.     return ((*f)(x + h) + (*f)(x - h) - 2*(*f)(x)) / (h*h);
  11. }
  12.  
  13. double der3(double x, double h, double(*f)(double)){
  14.     return ((*f)(x + 2 * h) - (*f)(x - 2 * h) - 2*(*f)(x + h) + 2*(*f)(x - h)) / (2 * h*h*h);
  15. }
  16.  
  17. double der4(double x, double h, double(*f)(double)){
  18.     return ((*f)(x + 2 * h) + (*f)(x - 2 * h) - 4*(*f)(x + h) - 4*(*f)(x - h) + 6*(*f)(x)) / (h*h*h*h);
  19. }
  20.  
  21.  
  22. int main()
  23. {
  24.     FILE *dest;
  25.     double x = M_PI/4;
  26.     dest = fopen("error data.txt", "wb");
  27.     for(double h = 1e-017; h < 10; h *= 1.1){
  28.         fprintf(dest, "%e\t%e\t%e\t%e\t%e\n", h,
  29.                 fabs(der1(x, h, &sin) - cos(x)),
  30.                 fabs(der2(x, h, &sin) + sin(x)),
  31.                 fabs(der3(x, h, &sin) + cos(x)),
  32.                 fabs(der4(x, h, &sin) - sin(x)));
  33.     }
  34.     fclose(dest);
  35.     return 0;
  36. }
  37.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement