/*
    This file is defines the library functions, they are supposed to supply useful functions
    that can be reused in many other programs.

    Note because its a library it does NOT have a main function.

    Trapetz integration is discussed Nyhoff 181
*/


#include <cmath>
using namespace std;

double func1(double x)
{
   return cos(x)*sin(x);
}


double TrapetzIntegrate(int N, double a,double b, double (*f)(double))
//  
//  double (*f)(double) shows f is a pointer to a function.
//  Thus we can transfer other functions as argments if we want.
//
{
   if (b<a)
    { // Swap
      double tmp=a; a=b; b=tmp;
    }

   double dx=(b-a)/N;
   double sum=0.0;

   sum= dx*(f(a)+f(b))/2;

   for (int i=1;i<N;i++)
     {
        double xi=(b-a)*i/N+a;   // Linear spaced points with x[0]=a and x[N]=b
        sum+= f(xi)*dx;
     }

   return sum;
}

