title: Adaptive Quadrature (C++)
body: <p>I'm having issues with my adaptive trapezoidal rule algorithm in C++ -- basically, regardless of the tolerance specified, I get the same exact approximation. The recursion is supposed to stop very early for large tolerances (since abs(coarse-fine) is going to be smaller than 3.0*large tolerance and minLevel of recursion is about 5).</p>
  
  <p>However, what this function does is run the maximum number of times regardless of choice of tolerance. Where did I mess up?
  EDIT: Perhaps there are issues in my helper functions? </p>
  
  <pre><code>double trap_rule(double a, double b, double (*f)(double),double tolerance, int count)
  {
      double coarse = coarse_helper(a,b, f); //getting the coarse and fine approximations from the helper functions
      double fine = fine_helper(a,b,f);
  
      if ((abs(coarse - fine) &lt;= 3.0*tolerance) &amp;&amp; (count &gt;= minLevel))
          //return fine if |c-f| &lt;3*tol, (fine is "good") and if count above
          //required minimum level
      {
          return fine;
      }
      else if (count &gt;= maxLevel)
          //maxLevel is the maximum number of recursion we can go through
      {
          return fine;
  
      }
      else
      {
          //if none of these conditions are satisfied, split [a,b] into [a,c] and [c,b] performing trap_rule
          //on these intervals -- using recursion to adaptively approach a tolerable |coarse-fine| level
          //here, (a+b)/2 = c
          ++count;
          return  (trap_rule(a, (a+b)/2.0, f, tolerance/2.0, count) + trap_rule((a+b)/2.0, b, f, tolerance/2.0, count));
      }
  }
  
  
  EDIT: Helper and test functions:
  
  //test function
  double function_1(double a)
  {
      return pow(a,2);
  }
  
  //"true" integral for comparison and tolerances
  
  
  
  
  //helper functions
  
  double coarse_helper(double a, double b, double (*f)(double))
  {
      return 0.5*(b - a)*(f(a) + f(b)); //by definition of coarse approx
  }
  
  
  double fine_helper(double a, double b, double (*f)(double))
  {
      double c = (a+b)/2.0;
      return 0.25*(b - a)*(f(a) + 2*f(c) + f(b)); //by definition of fine approx
  }
  
  double helper(double a, double b, double (*f)(double x), double tol)
  {
      return trap_rule(a, b, f, tol, 1);
  }
  </code></pre>
  
  <p>And here's what's in main():</p>
  
  <pre><code>std::cout &lt;&lt; "First we approximate the integral of f(x) = x^2 on [0,2]" &lt;&lt; std::endl;
  
      std::cout &lt;&lt; "Enter a: ";
      std::cin &gt;&gt; a;
  
      std::cout &lt;&lt; "Enter b: ";
      std::cin &gt;&gt; b;
  
      true_value1 = analytic_first(a,b);
  
      for (int i = 0; i&lt;=8; i++)
      {
          result1 [i] = helper(a, b, function_1, tolerance[i]);
          error1 [i] = fabs(true_value1 - result1 [i]);
      }
  
      std::cout &lt;&lt; "(Approximate integral of x^2, tolerance, error )" &lt;&lt; std::endl;
      for (int i = 0; i&lt;=8; i++)
      {
          std::cout &lt;&lt; "(" &lt;&lt; result1 [i] &lt;&lt; "," &lt;&lt; tolerance[i] &lt;&lt; "," &lt;&lt; error1[i] &lt;&lt; ")" &lt;&lt; std::endl;
      }
  </code></pre>