/* calculation of derivative central method*/ #include #include #include using namespace std; double f(double x) { return x*x+exp(x)+log(x)+sin(x); } double fp(double x){ return 2.*x+exp(x)+1./x+cos(x); } int main() { double x = 0.5; double h = 1E-2; double fp_exact = fp(x); cout << "Exacto: " << setprecision(18) << fp_exact << endl; cout << " h \t Valor aprox. \t\t diferencia" << endl; while(h>1E-15) { double fp_numeric = (f(x+h/2.) - f(x-h/2.))/h; cout << setprecision(8) << h << "\t" << setprecision(18) << fp_numeric << " \t" << fabs(fp_numeric-fp_exact) << endl; h /= 10.; } }