> 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
Here's my implementation of Simpson's, except it divides the interval into
2n segments.
>>> def simpson(f,a,b,n):
h = float(b-a)/(2*n)
sum1 = sum(f(a + 2*k *h) for k in range(1,n))
sum2 = sum(f(a + (2*k-1)*h) for k in range(1,n+1))
return (h/3)*(f(a)+f(b)) + (2*h/3)*sum1 + (4*h/3)*sum2
>>> def g(x): return x*x
>>> simpson(g,0,3,10000)
8.9999999999999893
>>> simpson(g,0,3,5000)
8.9999999999999787
Kirby
_______________________________________________
Edu-sig mailing list
[email protected]
http://mail.python.org/mailman/listinfo/edu-sig