Advertisement
amermo

TP T-5 Z4

Mar 31st, 2015
224
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.58 KB | None | 0 0
  1. #include <iostream>
  2. #include <cmath>
  3.  
  4. double TrapeznoPravilo(double f(double), int a, int b, int n)
  5. {
  6.     double suma(0);
  7.     for(int i(1); i <= n - 1; i++)
  8.         suma+=f(a + ((double(b-a)/n)*i));
  9.     return ((double(b-a)/n)*(f(double(a))/2 + f(double(b))/2 + suma));
  10. }
  11.  
  12. int main()
  13. {
  14.     std::cout << TrapeznoPravilo([](double x) {return x*x*x;}, 0, 10, 200) << std::endl;
  15.     std::cout << TrapeznoPravilo([](double x) {return std::sin(x);}, 0, 4*atan(1), 200) << std::endl;
  16.     std::cout << TrapeznoPravilo([](double x) {return 1./x;}, 1, 2, 200) << std::endl;
  17.     return 0;
  18. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement