function [q, x] = integralglabadat (fun, a, b, tol)
%
% function [q, x] = integralglabadat (fun, a, b, tol)
%
% Formula di quadratura di Gauss-Legendre adattativa in [a,b].
q = integralglab (fun, a, b, 1);
[q1, x1] = integralglab (fun, a, (a+b)/2, 1);
[q2, x2] = integralglab (fun, (a+b)/2, b, 1);
x = [x1; x2];
if (abs(q1+q2-q) > 10 * tol)
   [q1, x1] = integralglabadat (fun, a, (a+b)/2, tol / 2);
   [q2, x2] = integralglabadat (fun, (a+b)/2, b, tol / 2);
   x = [x1; x2];
end
q = q1 + q2;
%!demo
%! fun = @(x) sin (x .^ 2);
%! a = -10;
%! b = 10;
%! [q, x] = integralglabadat (fun, a, b, 1e-2);
%! plot(x, fun (x), x, zeros (size (x)), 'o')
%! E = abs (q - quad (fun, a, b))
