import math def fun(x): return x+math.sin(2**x) # in python need ** instead ^ from scipy.integrate import quad a=-1 b=2 intV,errV = quad(fun,a,b) # first element is integral estimate, second element is # an estimate of an upper bound on the error # intV = quad(fun,a,b)[0] # errV = quad(fun,a,b)[1] #print(intV) N=[2,4,8,16,32,64] left = [] right = [] trap = [] simp = [] exact = [intV for n in N] for n in N: h = (b-a)/n x = [a+j*h for j in range(n+1)] y = [fun(bubba) for bubba in x] left += [h*sum(y[:-1])] # y[:-1] means all indices up through the 2nd last index # y[A:B] means indices A through B-1. right += [h*sum(y[1:])] trap += [h/2*(y[0]+y[-1]+2*sum(y[1:-1]))] # note y[1:-1]=y[1:n] simp += [h/3*(y[0]+y[-1]+4*sum(y[1:-1:2])+2*sum(y[2:-1:2]))] #y[1:-1:2] means indices 1,3,5,...,stop before the last # #print(n,left,right,trap,simp,intV) # to make a nice chart, I will make a data frame import pandas as pd dict = {'n':N,'left':left,'right':right,'trap':trap,'simp':simp,'exact':exact} # The above is called a 'dictionary' df = pd.DataFrame(data=dict) print(df) # prints nice table