title: Computing integral with the Trapezoidal Rule (the approximate value of the integral, and the number of iterations)
body: <p>The program needs to compute define integral with a predetermined
  accuracy (eps) with the Trapezoidal Rule and my function needs to return: </p>
  
  <p>1.the approximate value of the integral.</p>
  
  <p>2.the number of iterations.</p>
  
  <p>My code:</p>
  
  <pre><code>    from math import *
      def f1(x):
         return (x ** 2 - 1)**(-0.5)
  
      def f2(x):
         return (cos(x)/(x + 1))
  
      def integral(f,a,b,eps):
         n = 2
         x = a
         h = (b - a) / n
         sum = 0.5 * (f(a) + f(b))
         for i in range(n):
            sum = sum + f(a + i * h)
         sum_2 = h * sum
         k = 0
         flag = 1
  
         while flag == 1:
            n = n * 2
            sum = 0
            k = k + 1
            x = a
            h = (b - a) / n
            sum = 0.5 * (f(a) + f(b))
            for i in range(n):
               sum = sum + f(a + i * h)
            sum_new = h * sum
            if eps &gt; abs(sum_new - sum_2):
               t1 = sum_new
               t2 = k
               return t1, t2
            else:
               sum_2 = sum_new
  
     x1 = float(input("First-begin: "))
     x2 = float(input("First-end: "))
     y1 = float(input("Second-begin: "))
     y2 = float(input("Second-end: "))
     int_1 = integral(f1,x1,y1,1e-6)
     int_2 = integral(f2,x2,y2,1e-6)
     print(int_1)
     print(int_2) 
  </code></pre>
  
  <p>It doesn't work correct. Help, please!</p>