lSum Practice with srange()

2777 days ago by MATH4H2012

f(x)=x^2 a = 0 b = 5 n = 10 h = (b-a)/n 
       
srange(b) 
       
srange(a,b) 
       
srange(a,b,h) 
       
[f(x) for x in srange(a,b,h)] 
       
[h*f(x) for x in srange(a,b,h)] 
       
sum([h*f(x) for x in srange(a,b,h)]) 
       
sum([h*f(x) for x in srange(a,b,h)]).n() 
       
def lSum(a,b,n,f): h=(b-a)/n return sum([h*f(x) for x in srange(a,b,h)]).n() f="x**2" show(lSum(0,5,10,lambda x: eval(f))) print "n\tlSum" for n in range(5): print "10^",n,"\t",lSum(0,5,10^n,lambda x: eval(f)) 
       

n	lSum
10^ 0 	0.000000000000000
10^ 1 	35.6250000000000
10^ 2 	41.0437500000000
10^ 3 	41.6041875000000
10^ 4 	41.6604168750000
n	lSum
10^ 0 	0.000000000000000
10^ 1 	35.6250000000000
10^ 2 	41.0437500000000
10^ 3 	41.6041875000000
10^ 4 	41.6604168750000