//
// source code: integral_t.cc 
// purpose:     approximates 1-dim integral numerically, 
//              trapezoidal method
// author:      T. Hebbeker   
// version:     1.0, 2006-10-26
//
//  note: this is a primitive version with fixed parameters and 
//        fixed function.
//

#include <iostream>
#include <math.h>
using namespace std;

// function to be integrated

double fun (double x)
{
  return exp(x);
}


int main()
{
  cout << endl 
       << " program   === integral_t ===   version 1.0 " 
       << endl;

  const int       n = 100;
  const double    a = -10.;
  const double    b =  3.;
  
  double          h = (b-a)/n; 

  double          factor;
  
  double          integral = 0.;
  double          x = a;

  for (int i=1; i<=n+1; i++) // variable i is defined only inside loop !
  {
    if(1==i || (n+1)==i)
    {
      factor = 0.5;
    }
    else
    {
      factor = 1.;
    }

    integral += fun(x)*factor;
    x += h;
  }

  integral *= h; 

  cout << endl 
       << " approximate integral = " << integral << endl;
  cout << " correct value        = " << exp(b) - exp(a) << endl;

  return 0;
}





