Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <math.h>
- #include <stdlib.h>
- #include <cstdio>
- using namespace std;
- double funct_1(double x)
- {
- return sin(x);
- }
- double funct_2(double x)
- {
- return cos(x);
- }
- double funct_3(double x)
- {
- return -sin(x);
- }
- int main(void)
- {
- double a=0.0, b=18.0;
- int N=800;
- double h=(b-a)/(double)N;
- double pochodna_pierwsza,pochodna_druga;
- double x;
- double err1,err2;
- FILE *file1=fopen("pochodna_pierwsza.txt","w");
- FILE *file2=fopen("pochodna_druga.txt","w");
- fprintf(file1,"X: \t f = Cos(x): \t\t Pochodna 1: \t\t Blad: \n");
- fprintf(file2,"X: \t f = Cos(x): \t\t Pochodna 2: \t\t Blad: \n");
- for(int i=0;i<N;i++)
- {
- x=a+(double)i*h;
- pochodna_pierwsza=(-funct_1(x+2.0*h)+4.0*funct_1(x+h)-3.0*funct_1(x))/(2.0*h);
- err1=fabs(funct_2(x)-pochodna_pierwsza);
- fprintf(file1,"%.5lf\t%.15lf\t%.15lf\t%.15lf\n",x,funct_2(x),pochodna_pierwsza,err1);
- pochodna_druga=(funct_1(x+h)-2.0*funct_1(x)+funct_1(x-h))/(h*h);
- err2=fabs(funct_3(x)-pochodna_druga);
- fprintf(file2,"%.5lf\t%.15lf\t%.15lf\t%.15lf\n",x,funct_2(x),pochodna_druga,err2);
- }
- fclose(file1);
- fclose(file2);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement