Jason Born
Jason Born

Reputation: 173

Overcoming multiplication of matrices with differing dimensions in Matlab integral

I am having trouble with the following integral2 operation on Matlab.

phi = @(x)(x>0).*exp(-1./x.^2);
R = @(xx,zz)phi(xx).*phi(1-xx).*phi(zz).*phi(1-zz);
omega = linspace(0,5,1000000);
theta = linspace(0,2*pi,1000000);
D = exp((10*1i*omega)./(40*pi)).*integral2(@(xx,zz)R(xx,zz).*exp(20.*omega.*1i.*((sin(theta)).*xx+(cos(theta)).*zz)),0,5,0,5);

The error message I get is that "the matrix dimensions must agree".

Matrix dimensions must agree.
Error in @(xx,zz)R(xx,zz).*exp(20.*omega.*1i.*((sin(theta)).*xx+(cos(theta)).*zz))

Error in integral2Calc>integral2t/tensor (line 228)
        Z = FUN(X,Y);  NFE = NFE + 1;
Error in integral2Calc>integral2t (line 55)
[Qsub,esub] = tensor(thetaL,thetaR,phiB,phiT);
Error in integral2Calc (line 9)
    [q,errbnd] = integral2t(fun,xmin,xmax,ymin,ymax,optionstruct);
Error in integral2 (line 106)
    Q = integral2Calc(fun,xmin,xmax,yminfun,ymaxfun,opstruct); 

I am unsure how to overcome this however. When I do numel on xx, theta, omega and zz, I find that they all have 1000000 elements.

I know this might be a "newbie" question, but I tried many things to get to this point without avail.

The equation I am trying to solve is:

enter image description here

Upvotes: 0

Views: 297

Answers (3)

AVK
AVK

Reputation: 2149

The function integral2 can only calculate one integral at once. If you need to calculate many integrals, you should use cycles (I have slightly reduced the grid size):

phi = @(x)(x>0).*exp(-1./x.^2);
R = @(xx,zz)phi(xx).*phi(1-xx).*phi(zz).*phi(1-zz);
    % generate the grid
[omega,theta]= meshgrid(linspace(0,5,200),linspace(0,2*pi,200));
D= zeros(size(omega)); % preallocate the memory
for ii= 1:size(omega,1)
    for jj= 1:size(omega,2)
        f= @(xx,zz)R(xx,zz).*exp(20.*omega(ii,jj).*1i.*...
            ((sin(theta(ii,jj))).*xx+(cos(theta(ii,jj))).*zz));
        D(ii,jj)= exp((10*1i*omega(ii,jj))./(40*pi)).*integral2(f,0,5,0,5);
    end
end
    % convert to Cartesian coordinates
[x,y] = pol2cart(theta,omega);
    % draw the results
figure;
surface(x,y,abs(D))
shading interp; axis square; view(3)
figure;
surface(x,y,real(D))
shading interp; axis square; view(3)
figure;
surface(x,y,imag(D))
shading interp; axis square; view(3)

The sizes of xx and zz in the integrand R(xx,zz) are not necesserily equal to 1x1000000. The MATLAB documentation only says that

The function fun must accept two arrays of the same size and return an array of corresponding values. It must perform element-wise operations.

On my system, for example, its size was 14x14.

Upvotes: 3

m7913d
m7913d

Reputation: 11064

integral2(fun, xmin, xmax, ymin, ymax) calculates the volume under the plane described by the function z = fun(x, y) inside the given x and y limits.

So, for one value of (x, y), fun should return only one z value, which is not the case in your example. You should really check the meaning of your function and determine which integral you want to calculate.

Upvotes: 2

yar
yar

Reputation: 1905

You cannot do numel on xx, since it's a function parameter that is passed by the integration function. You actually do not know its size and cannot multiply it with a constant size matrix.

Upvotes: 0

Related Questions