function integral2d=integrate_in_2d(f, xrange, yrange)
% we will integrate function f in 2D space
% f is handle to function of x, y i.e. f(x,y) should be valid
% xrange is a vector containing lower and upper limits of integration 
%   along the first dimension.
%   xrange = [x_lower x_upper] 
% yrange is similar but along second dimension

% we will define (Integral f(x,y) dy) as Fx(x)
% notice that it is really cumbersome and in general you will need to
% implement multidimensional integral by programming nested loops,
% since it is next to impossible to use '@' multiple times.
Fx = @(x) integrate_in_1d( @(y) f(x,y), yrange(1), yrange(2) );
%   ^^^^^ we fix 'x',      ^^^^^^^^^^^here we reuse this already fixed x so it reads as Fy(y)

integral2d = integrate_in_1d(Fx, xrange(1), xrange(2) );

% notice that matlab folks implemented integral, integral2, integral3 but they did not do
% any higher than 3 dimensions. 
% Nice part that Monte Carlo is still very easy in any number of dimensions.
end


