trapezoidal

#numerical integration by trapezoidal rule
def f(x):
    return 1/(1+x)
a=0
b=6
k=a
h=1.0
y=[]
integral=0.0
while k<=6:
    y.append(f(k))
    k=k+h
integral =integral +y[0]+(y[1]*4)+y[len(y)-1]
k=3
while k<=len(y)-2:
    integral =integral +y[k]*4+((y[k-1])*2)
    k=k+2
integral=(integral*h)/3
print"within in the limits",a,"and",b,"integral of given function"
print integral

Comments