MATLAB: How accurate is the numerical integrator, cumtrapz

numerical integration

How accurate is the numerical integrator, cumtrapz?

Best Answer

There is no bound to the error of cumtrapz()
Consider f = x^3-sin(x) over 0 to 5, with step size 1/1000, then the numeric error from the trapazoid rule is about 6E-6.
Now consider f = 1000*cot(Pi*x-2) over 0 to 1/2, with step size 1/1000, then the numeric error from the trapazoid rule is about 1E-4 . This is 1/10th of the range that produced the 6E-6 error for the first function, so if it were range alone that made the difference than you would expect around 6E-7 error. If you were to then examine and say "Oh but the multiplier is 1000 times more" then that would at get you to about 6E-4 error, more than the 1E-4 observed. We can thus determined that there must be more factors than just constant multipliers or length of the range: the numeric error must depend upon the shape of the function itself.
Now consider f = cot(Pi*x-2) over 0 to 5, with step size 1/1000, then the numeric error from the trapazoid rule is undefined, as the calculation would involve adding values that range from -inf to +inf and adding -inf to +inf is undefined. So it definitely depends upon the function being evaluated.
You can calculate the error symbolically. The trapazoid rule estimate of the integral of F from A to B step size H is
(symsum(F(k*H), k, 1, B/H-1)+(1/2)*F(A)+(1/2)*F(B))*H
from which you can subtract int(F,A,B) to get the error.
Related Question