%% Multiple Numerical Integrations
% Create a grid of domain values.

x = -3:.1:3; 
y = -5:.1:5; 
[X,Y] = meshgrid(x,y);

%
% Calculate the function f(x,y) = x^2 + y^2 on the grid.

F = X.^2 + Y.^2;

% trapz integrates numeric data rather than functional expressions, so in general the expression does not need to be known to use trapz on a matrix of data. In cases where the functional expression is known, you can instead use integral, integral2, or integral3.
%
% Use trapz to approximate the double integral
% I = \int_{-5}^5 \int_{-3}^3 \left(x^2 + y^2 \right) dx \: dy
% To perform double or triple integrations on an array of numeric data, nest function calls to trapz.

I = trapz(y,trapz(x,F,2))

% trapz performs the integration over x first, producing a column vector. Then, the integration over y reduces the column vector to a single scalar. trapz slightly overestimates the exact answer of 680 because f(x,y) is concave up.
% Copyright 2012 The MathWorks, Inc.