MATLAB: Overlap integral of the electric field distribution

MATLABnumerical integrationtrapz function

Hi, I am trying to calculate overlap integral (OI) of electric field distribution, using Matlab, when run the following code:
xc = linspace(0,1,M);
yc = linspace(0,1,M);
A = E1 * E2;
B = (abs(trapz(xc,trapz(yc, abs(A))) ) ).^2;
C = trapz(xc,trapz(yc, abs(E1).^2)) * trapz(xc,trapz(yc, abs(E2).^2 ));
OI = B./C;
where E1 is the electric field distribution of the desired mode and E2 is polarized mode. Both E1 and E2 are matrices of size MxM.
but, I have got this error: Error using trapz (line 57) *LENGTH(X) must equal the length of Y in dim 1.

Best Answer

xc = linspace(0,1,M);
yc = linspace(0,1,M);
A = E1 .* E2;
B = (trapz(xc,trapz(yc, abs(A), 1)) ).^2;
C = trapz(xc,trapz(yc, abs(E1).^2, 1)) * trapz(xc,trapz(yc, abs(E2).^2, 1 ));
OI = B./C;
Best wishes
Torsten.
Related Question