function [minval,x] = LowestCriticalPoint(fun,x,minval,tol)

% [minval,x] = LowestCriticalPoint(fun,x,minval,tol)
%
% solves (interior) global minimization problem on a list
% of rectangles using interval arithmetic. 
%
%   fun         objective function. f = fun(x) should give
%               for a (2 x n)-vector of input intervals x 
%               a cell structure f.x, f.dx containing the 
%               (1 x n)-vector of f-intervals f.x and the 
%               (2 x n)-vector of df-intervls f.dx
%               
%   x           input: list of rectangles, i.e. (2 x n)-
%               vector of intervals, specifying search region
%               output: midpoint of final enclosing rectangle
%
%   minval      interval enclosing glbal minimum
%
%   tol         relative tolerance (for minima below 1e-20,
%               absolute tolerance)

while min(relerr(minval),1) > tol
    x = subdivide2D(x);
    f = feval(fun,x); 
    upper = min(minval.sup,min(f.x.sup)); 
    rem = (f.x > upper) | any(not(in(zeros(size(f.dx)),f.dx)));
    f.x(rem) = []; x(:,rem) = [];
    minval = infsup(min(f.x.inf),upper);
end
x = mid(x);

end
