MATLAB: How does the QUAD function handle improper integrals in MATLAB 7.14 (R2012a)

improperintegralMATLABquad

I use the QUAD function to perform integration and would like to know how MATLAB handles improper integrals.
An improper integral is one in which the function cannot be evaluated at one of the end points.

Best Answer

The following code snippet creates an example:
>> myfun = @(x) 3*exp(x)./sqrt(1-x.^2);
>> quad(myfun,0,1)
ans =
9.3132
The function value at the end points are
>> myfun(0)
ans =
3
>> myfun(1)
ans =
Inf
Before beginning the integration, MATLAB checks the end points for infinities. If found, the offending point is purturbed slightly so that it moves away from the singularity.
You can see this by looking at the code for QUAD:
>> edit quad
The code snippet below from QUAD shows how this is done:
% Fudge endpoints to avoid infinities.
if ~isfinite(y(1))
y(1) = f(a+eps(superiorfloat(a,b))*(b-a),varargin{:});
fcnt = fcnt+1;
end
if ~isfinite(y(7))
y(7) = f(b-eps(superiorfloat(a,b))*(b-a),varargin{:});
fcnt = fcnt+1;
end
In the above code snippet, y(1) and y(7) are the interval end points.
Related Question