#LRTE f(x) from x=a to x=b for n rectangles!
f(x)=exp(x^2)
a=1
b=2
n=1000
h=(b-a)/n
l=sum([f(a+i*h)*h for i in range(n)]).n(digits=6)
r=(l-f(a)*h+f(b)*h).n(digits=6)
t=(r+l)/2
e=(r-l)/2
print "lSum\t\trSum\t\ttrapSum\t\ttrapError"
print l,"\t",r,"\t",t,"\t",e
show(integrate(f(x),x,a,b).n())
plot(f(x),a,b)
|
lSum rSum trapSum trapError
14.9641 15.0159 14.9900 0.0259399

lSum rSum trapSum trapError
14.9641 15.0159 14.9900 0.0259399

|