Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <algorithm>
- #include <functional>
- #include <cmath>
- const double PI{4*atan(1)};
- double TrapeznoPravilo (double f (double), double a, double b, int n)
- {
- double s1{1/2*f(a)+1/2*f(b)};
- for (int k=1; k<=n-1; k++) {
- s1+=f(a+((b-a)*k)/n);
- }
- return s1*(b-a)/n;
- }
- int main () {
- std::cout<<"Za f-ju sin(x): "<<TrapeznoPravilo(std::sin,0, PI, 1000)<<std::endl;
- std::cout<<"Za f-ju x^3: "<<TrapeznoPravilo([] (double x) {return x*x*x; },0, 10, 1000)<<std::endl;
- std::cout<<"Za f-ju 1/x: "<<TrapeznoPravilo([] (double x) {return 1/x; },1, 2, 10000);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement