Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <math.h>
- double der1(double x, double h, double(*f)(double)){
- return ((*f)(x + h) - (*f)(x - h)) / (2 * h);
- }
- double der2(double x, double h, double(*f)(double)){
- return ((*f)(x + h) + (*f)(x - h) - 2*(*f)(x)) / (h*h);
- }
- double der3(double x, double h, double(*f)(double)){
- return ((*f)(x + 2 * h) - (*f)(x - 2 * h) - 2*(*f)(x + h) + 2*(*f)(x - h)) / (2 * h*h*h);
- }
- double der4(double x, double h, double(*f)(double)){
- return ((*f)(x + 2 * h) + (*f)(x - 2 * h) - 4*(*f)(x + h) - 4*(*f)(x - h) + 6*(*f)(x)) / (h*h*h*h);
- }
- int main()
- {
- FILE *dest;
- double x = M_PI/4;
- dest = fopen("error data.txt", "wb");
- for(double h = 1e-017; h < 10; h *= 1.1){
- fprintf(dest, "%e\t%e\t%e\t%e\t%e\n", h,
- fabs(der1(x, h, &sin) - cos(x)),
- fabs(der2(x, h, &sin) + sin(x)),
- fabs(der3(x, h, &sin) + cos(x)),
- fabs(der4(x, h, &sin) - sin(x)));
- }
- fclose(dest);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement