dmkozyrev

diff_func.cpp

Dec 2nd, 2016
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.59 KB | None | 0 0
  1. #include <iostream>
  2. #include <cmath>
  3. #include <iomanip>
  4.  
  5. using namespace std;
  6.  
  7. inline double f(double x) { return log(x+1)-2*x*x+1;    /* f(x) */ }
  8. inline double df(double x) { return 1/fabs(x+1)-4*x;    /* f'(x) */ }
  9.  
  10. int main() {
  11.     // left
  12.     int count = 0;
  13.     double x = 2.0, dx = 1.0;
  14.     for (int i = 0; i < 20; ++i, dx *= 0.1) {
  15.         double dy = (f(x) - f(x-dx)) / dx;
  16.         cout << i << ":\t";
  17.         cout << "dx = " << setw(9) << dx << "\t";
  18.         cout << "y'(x0) = " << dy << endl;
  19.     }
  20.    
  21.    
  22.     printf("check: %0.9f\n", df(2.0));
  23.    
  24.     return 0;
  25. }
Add Comment
Please, Sign In to add comment