Kirby got the trapezoidal integration rule wrong.
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))

The interval must be divided into n equal parts, so
an arbitrary h will not do. With this version, the
error in Kirby's integration of x^2 from 0 to 3 will
decrease by a factor of 100000.

But you never use the trapezoidal rule in real life!
Simpson gives much smaller error for the same amount
of computation. In fact, it is exact for Kirby's example!

Henrik

_______________________________________________
Edu-sig mailing list
Edu-sig@python.org
http://mail.python.org/mailman/listinfo/edu-sig

Reply via email to