title: Numerical Integration with Trapezoidal rule c++
body: <p>I'm attempting to implement a Trapezoidal rule that utilizes previous function evaluations in order to avoid redundant computation. Two things: a) computed results are not converging and I'm a little unsure of why. I'll post of the mathematics behind why I think that the algorithm should yield convergence if it's wanted, and b) the do while loop is terminating at n=8 and I've been unable to figure that one out as well; it should be running until n>128? (n is the number of subintervals) My code is below. Thanks in advance!</p>
  
  <pre><code>void NestedTrap(int n) //Trapezoidal with reuse of function evaluations
  {
      double a,b; //interval end points
      double x[n+1]; //equally spaced nodes
      double c[n]; //midpoints
      double T; //Initial integral evaluation
      double T2; //Evaluation with reuse of previous function evaluations
      double h, h2; //step sizes for T and T2
      double temp1, temp2;
  
      std::cout &lt;&lt;"Enter interval end points (lesser first; enter 999 for pi &amp; 999.2 for pi/2 &amp; 999.4 for pi/4): ";
      std::cin &gt;&gt; a &gt;&gt; b;
  
      if (b == 999)
      {
          b = M_PI;
      }
      if (a == 999)
      {
          a = M_PI;
      }
      if (b == 999.4)
      {
          b = M_PI/4;
      }
      if (a == 999.4)
      {
          a = M_PI/4;
      }
      if (b == 999.2)
      {
          b = M_PI/2;
      }
      if (a == 999.2)
      {
          a = M_PI/2;
      }
  
      h = (b-a)/n;
      T = 0;
      temp1 = 0;
      temp2 = 0;
  
      for (int i=0; i&lt;=n; i++)
      {
          x[i] = 0;
      }
  
  
      for (int i=0; i&lt;n; i++)
      {
          x[i+1] = x[i] + h;
      }
  
      for (int i=1; i&lt;n; i++)
      {
          temp1 += I1(x[i]);
      }
      T = (h/2)*exp(x[0]) + (h/2)*exp(x[n]) + (h*temp1);
      std::cout &lt;&lt; "T_" &lt;&lt; n &lt;&lt;": " &lt;&lt; T &lt;&lt; std::endl;
  
      do
      {
      temp2 = 0;
      n = 2*n;
      h2 = (b-a)/(n);
  
      for (int i=0; i&lt;n; i++)
      {
          c[i] = 0;
      }
      for (int i=1; i&lt;=n; i++)
      {
          c[i] = a + h2*(i-0.5);
          //std::cout &lt;&lt; c[i] &lt;&lt; std::endl;
      }
  
      for (int i=0; i&lt;n; i++)
      {
          temp2 += exp(c[i]);
      }
  
      T2 = (T/2) + h2*temp2;
      std::cout &lt;&lt; "T_" &lt;&lt; n &lt;&lt;": " &lt;&lt; T2 &lt;&lt; std::endl;
  
      T = T2;
      }   while (n &lt;= 128);
  }
  </code></pre>