Advertisement
Guest User

ZMIENIENIONE LAB3

a guest
Sep 11th, 2019
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.24 KB | None | 0 0
  1. #include <iostream>
  2. #include <math.h>
  3. #include <stdlib.h>
  4. #include <cstdio>
  5.  
  6. using namespace std;
  7.  
  8. double funct_1(double x)
  9. {
  10. return sin(x);
  11. }
  12.  
  13. double funct_2(double x)
  14. {
  15. return cos(x);
  16. }
  17.  
  18. double funct_3(double x)
  19. {
  20. return -sin(x);
  21. }
  22.  
  23.  
  24. int main(void)
  25. {
  26. double a=0.0, b=18.0;
  27. int N=800;
  28. double h=(b-a)/(double)N;
  29. double pochodna_pierwsza,pochodna_druga;
  30. double x;
  31. double err1,err2;
  32. FILE *file1=fopen("pochodna_pierwsza.txt","w");
  33. FILE *file2=fopen("pochodna_druga.txt","w");
  34. fprintf(file1,"X: \t f = Cos(x): \t\t Pochodna 1: \t\t Blad: \n");
  35. fprintf(file2,"X: \t f = Cos(x): \t\t Pochodna 2: \t\t Blad: \n");
  36.  
  37. for(int i=0;i<N;i++)
  38. {
  39. x=a+(double)i*h;
  40. pochodna_pierwsza=(-funct_1(x+2.0*h)+4.0*funct_1(x+h)-3.0*funct_1(x))/(2.0*h);
  41. err1=fabs(funct_2(x)-pochodna_pierwsza);
  42. fprintf(file1,"%.5lf\t%.15lf\t%.15lf\t%.15lf\n",x,funct_2(x),pochodna_pierwsza,err1);
  43. pochodna_druga=(funct_1(x+h)-2.0*funct_1(x)+funct_1(x-h))/(h*h);
  44. err2=fabs(funct_3(x)-pochodna_druga);
  45. fprintf(file2,"%.5lf\t%.15lf\t%.15lf\t%.15lf\n",x,funct_2(x),pochodna_druga,err2);
  46. }
  47. fclose(file1);
  48. fclose(file2);
  49. return 0;
  50. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement