MATLAB: Does the QUAD function generate erroneous answers when the integration interval is large relative to the region where the integrand is most “active” in MATLAB 7.5 (R2007b)

MATLAB

When I implement the QUAD function the integration interval is large relative to the region where the integrand is most "active", I recieve erroneous. For example, while using the NCX2PDF function with the QUAD function, answers for higher values of DELTA are incorrect.
for delta=600:605
p=quad(@(x)ncx2pdf(x,2,2),0,delta),end
The value of p changes drastically when x=603.
p =
0.999991378892210
p =
0.999991357559579
p =
0.999991336349130
p =
1.579395375717856e-005
p =
1.540064694779559e-005
p =
1.501701084682311e-005

Best Answer

This is common behavior for numerical quadrature codes. They sample at a finite number of points using two different formulas, approximate the definite integral over the interval, and estimate the error in the approximation using a different formula. When the integration interval is sufficiently large relative to the region where the integrand is most "active", the formula may fail to detect any area where the function is substantially non-zero, in which case the quadrature routine supposes that the integrand is negligible everywhere and terminates.
If you want to integrate over an infinite interval, use the QUADGK. function. It automatically transforms the integral prior to integrating it.
quadgk(@(x)ncx2pdf(x,2,2),0,inf)
ans =
1.000000000000001e+000
Or you can transform the integral yourself to an equivalent problem on a finite interval.
For integrals over a large finite interval where there is a long tail, it helps to split the interval up into pieces and add them. How best to split it up depends on the problem. For example:
quad(@(x)ncx2pdf(x,2,2),0,600)+ quad(@(x)ncx2pdf(x,2,2),600,605)
ans =
9.999913788922105e-001