Numerical integration tolerance pitfalls

444 Views Asked by At

Consider that we want to estimate $$\int_{\pi/2}^{\pi/2+8\pi}sin(x)dx$$ (the value of this integrate is obviously zero) with the Midpoint rule. We start with the endpoints $a=\pi/2$ and $b=\pi/2+8\pi$ and suppose we start our numerical integration using the whole interval as our first in estimating the integrate. Then we get the first estimate to be $8\pi$ since the midpoint of the whole interval is $\pi/2+4\pi$ where $sin(x)$ takes the value 1 and we multiply by the length of the interval, call this value $I_1=8\pi$.

For the second iteration we get the same value since now we have two intervals where the but here the midpoints are $\pi/2+2\pi$ and $\pi/2+6\pi$ and the sine function takes the value 1 at both these points. Here we doubled the number of intervals as is suggested by many textbooks on the matter. Hence $I_2 =8\pi$

In many pseudocodes it is used that if we want to estimate the integration by less than $\epsilon$ than we should stop the iteration when $|I_{n}-I_{n-1}|<\epsilon/2$. Here this method for any reasonable small $\epsilon$'s fails and also the method of using relative error since $|I_{n}-I_{n-1}|=0$ for $n=2$ but the true error is $|I-I_2|=8\pi$. Is there any way to prevent this to happen? Perhaps one should run many numerical estimation concurrently and compare the values and stop when all the conditions are fullfilled.

2

There are 2 best solutions below

0
On

If I were going to estimate this integral with the midpoint rule (or any other quadrature method), I would a priori choose my step size to get a satisfactory error. Luckily, for such things at the midpoint rule, there are known error estimates (see this page, for example). You can find the derivations of such error bounds in any good numerical analysis text.

Regarding adaptive quadrature, which you seem to be alluding to here: you would still choose a starting step size $dx$ that gets you close to the tolerance. You most certainly would not start with $dx = b-a$. Not only would it lead to problems like the one you've pointed out here, it would also take a really long time to converge for a problem that doesn't have issues like this one.

Also, you wouldn't use adaptive quadrature on this problem. $\sin (x)$ is a pretty boring function-- it doesn't have sharp gradients or vary much from one interval to the next. Adaptive quadrature is useful for functions like this, that is pretty boring on $[3,8]$-- where you'd use a large mesh-- but a little weird on $[1,3]$-- where you'd need to adaptively refine the mesh in order to pick up the weird features.

0
On

A lot of numeric methods rely on restricting them to regions in which the function in question is not too "crazy." For example, it's tricky to search for a global maximum in a region where the function has multiple local maxima. For this method of integration, you would want to start with a step size small enough so that you don't have more than one point of inflection in the function within one step. (Even if there are not multiple local maxima or minima as in this case, you can also run into trouble if the function has too many "bumps.")