Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cmath> // cos(), sin(), M_PI_2
- using namespace std;
- namespace{
- template <typename T, typename TRes>
- class IUnaryFunction{
- public :
- virtual ~IUnaryFunction (void) {}
- virtual TRes operator () (const T &) const noexcept = 0;
- }; // IUnaryFunction
- typedef IUnaryFunction <double, double> TypeFunctor;
- class FctorSin: public TypeFunctor{
- public :
- virtual ~FctorSin (void) {}
- virtual double operator () (const double & T) const noexcept { return sin (T);}
- }; // FctorSin
- class CFctorCos: public TypeFunctor{
- public :
- virtual ~CFctorCos (void) {}
- virtual double operator () (const double & T) const noexcept { return cos (T);}
- }; // CFctorCos
- double integrTrapezes (const TypeFunctor & f, double a, double b, unsigned n){
- double s = (f (a) + f (b)) / 2.0;
- double delta = (b - a) / double (n);
- for ( ; --n; ) s += f (a += delta);
- return s * delta;
- } // integrTrapezes
- void testIntegrTrapezes (void){
- cout << "IntegrTrapezes : \n\n";
- cout << "Methode des trapezes : \n";
- cout << "S (cos (x)) entre 0 et +Pi/2 avec 5 intervalles = "
- << integrTrapezes (CFctorCos(), 0, M_PI_2, 5) << '\n';
- cout << "S (cos (x)) entre 0 et +Pi/2 avec 10 intervalles = "
- << integrTrapezes (CFctorCos(), 0, M_PI_2, 10) << '\n';
- cout << "S (cos (x)) entre 0 et +Pi/2 avec 50 intervalles = "
- << integrTrapezes (CFctorCos(), 0, M_PI_2, 50) << '\n';
- cout << "S (cos (x)) entre 0 et +Pi/2 avec 100 intervalles = "
- << integrTrapezes (CFctorCos(), 0, M_PI_2, 100) << '\n';
- cout << '\n';
- cout << "S (sin (x)) entre -Pi/2 et 0 avec 5 intervalles = "
- << integrTrapezes (FctorSin(), -M_PI_2, 0, 5) << '\n';
- cout << "S (sin (x)) entre -Pi/2 et 0 avec 10 intervalles = "
- << integrTrapezes (FctorSin(), -M_PI_2, 0, 10) << '\n';
- cout << "S (sin (x)) entre -Pi/2 et 0 avec 50 intervalles = "
- << integrTrapezes (FctorSin(), -M_PI_2, 0, 50) << '\n';
- cout << "S (sin (x)) entre -Pi/2 et 0 avec 100 intervalles = "
- << integrTrapezes (FctorSin(), -M_PI_2, 0, 100) << '\n';
- } // TestIntegrTrapezes()
- } // namespace anonyme
- int main (void){
- testIntegrTrapezes ();
- return 0;
- } // main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement