Search This Blog

Wednesday, January 5, 2011

Trapezoidal and Simpson's rule -Integration

#This program calculates integral of 1/1+x in the interval 0 to 1
#using both trapezodal and simpsons rule using Lists without
#importing any modules
h=.001
N=1000
x=[h*i for i in range(N)]
y=[1/(1+i) for i in x]
trapezoidal=h/2*(y[0]+y[-1]+2*
sum(y[1:-2]))
print "tapezoidal rule gives%.6f"%trapezoidal
simpsons=(h/3)*(y[0]+y[-1]+4*sum(y[1:-2:2])+2*sum(y[2:-3:2]))
print "simpsons rule gives %.6f"%simpsons
 
by Prof. Ahamed Ashraf 
 

No comments:

Post a Comment