Advertisement
Guest User

Untitled

a guest
May 20th, 2019
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.70 KB | None | 0 0
  1. #include <iostream>
  2. #include <cmath>
  3.  
  4. using namespace std;
  5.  
  6. double f (double x)
  7. {
  8. return sin (x);
  9. }
  10.  
  11. double derivative (double (*f) (double), double x)
  12. {
  13. const double h = 1E-4;
  14.  
  15. return (f (x + h) - f (x - h)) / (2 * h);
  16. }
  17.  
  18. double integral (double (*f) (double), double a, double b)
  19. {
  20. double Delta = 1E-4, output = 0;
  21.  
  22. for (double x = a; x < b - Delta; x += Delta)
  23. output += Delta * (f (x) + f (x + Delta)) / 2;
  24.  
  25. return output;
  26. }
  27.  
  28. int main (int argc, char** argv)
  29. {
  30. double x = 0;
  31.  
  32. cout << "f (" << x << ") = " << f (x) << endl;
  33. cout << "f' (" << x << ") = " << derivative (&f, x) << endl;
  34.  
  35. cout << "int (f, 0, M_PI) = " << integral (&f, 0, M_PI) << endl;
  36.  
  37. return 0;
  38.  
  39. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement