Advertisement
sellmmaahh

tut5-zad4

Jul 22nd, 2015
234
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.63 KB | None | 0 0
  1. #include <iostream>
  2. #include <algorithm>
  3. #include <functional>
  4. #include <cmath>
  5.  
  6. const double PI{4*atan(1)};
  7.  
  8.  
  9. double TrapeznoPravilo (double f (double), double a, double b, int n)
  10. {
  11.     double s1{1/2*f(a)+1/2*f(b)};
  12.     for (int k=1; k<=n-1; k++) {
  13.             s1+=f(a+((b-a)*k)/n);
  14.     }
  15.     return s1*(b-a)/n;
  16. }
  17.  
  18. int main () {
  19.  
  20.  
  21. std::cout<<"Za f-ju sin(x): "<<TrapeznoPravilo(std::sin,0, PI, 1000)<<std::endl;
  22.  
  23.  
  24. std::cout<<"Za f-ju x^3: "<<TrapeznoPravilo([] (double x) {return x*x*x; },0, 10, 1000)<<std::endl;
  25.  
  26. std::cout<<"Za f-ju 1/x: "<<TrapeznoPravilo([] (double x) {return 1/x; },1, 2, 10000);
  27. return 0;
  28.  
  29. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement