[Edu-sig] RE: Integration correction

Kirby Urner urnerk at qwest.net
Tue Mar 29 20:49:09 CEST 2005


> Kirby got the trapezoidal integration rule wrong.

Right, I was just doing a simple average, not any trapezoid.  My rectangles
took the mean between f(x-h) and f(x+h), nothing more.  Not the best
approximation, I agree, but simple to think about, and some text books show
it.

> This is the corrected version.
> 
> def integrate(f,a,b,n=1000):
>     sum = 0
>     h = (b-a)/float(n)
>     for i in range(1,n):
>            sum += f(a+i*h)
>     return h*(0.5*f(a)+sum+0.5*f(b))
> 

So now we should come up with the @integral decorator to wrap any function
in its integral.  How would that look? 

Kirby




More information about the Edu-sig mailing list