MATLAB: How to find the double integral using Trapezoidal rule

double integralintegraltrapezoidal rule

I want to find the double integral of the following function (sin(x+3*y))^2*exp(x-2*y)/(x^2+2). Limits are x and y from -1 to 1.
N = 5;
x = linspace(-1, 1,N);
y = linspace(-1, 1,N);
dx = diff(x(1:2));
dy = diff(y(1:2));
[x,y] = meshgrid(x,y);
mat = (sin(x+3.*y)).^2.*exp(x-2.*y)/(x.^2+2);
mat(2:end-1,:) = mat(2:end-1,:)*2;
mat(:,2:end-1) = mat(:,2:end-1)*2;
out = sum(mat(:))*dx*dy/4
I edited this code to suit my function but it shows result as NaN with the following message:
In doubletraptest (line 7)
Warning: Matrix is singular to working precision

Best Answer

x=-1:0.1:1;
y=-1:0.1:1;
[X,Y] = meshgrid(x,y);
F=(sin(X+3*Y)).^2.*exp(X-2*Y)./(X.^2+2);
I=trapz(y,trapz(x,F,2))