%% Examine Absolute Error
% Use quadgk to evaluate an oscillatory integrand that is difficult to evaluate.
%
% Evaluate the integral
% Q=\int_0^{\pi } \sin \left(20000\pi x\right)\textrm{dx}\ldotp
% The integrand oscillates very quickly, so it is difficult to evaluate. Use quadgk to evaluate the integral, and specify two outputs to examine how close the error tolerances are to being met.

fun = @(x) sin(2e4*pi*x);
[Q,errbnd] = quadgk(fun,0,pi)

% The warning message indicates how to adjust MaxIntervalCount to allow for another iteration in the solution process.
%
% Solve the integral again, but specify MaxIntervalCount as 1e5. With many more intervals, quadgk is able to meet the absolute error tolerance for the problem (1e-10 for double precision).

[Q,errbnd] = quadgk(fun,0,pi,'MaxIntervalCount',1e5)

% Copyright 2019 The MathWorks, Inc.