克服Matlab积分中不同维度矩阵的乘法

Overcoming multiplication of matrices with differing dimensions in Matlab integral

我在 Matlab 上进行以下 integral2 操作时遇到问题。

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 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); 

不过我不确定如何克服这个问题。当我对 xxthetaomegazz 执行 numel 时,我发现它们都有 1000000 个元素。

我知道这可能是一个 "newbie" 问题,但我尝试了很多方法都无济于事。

我要解的方程是:

您不能对 xx 执行 numel,因为它是集成函数传递的函数参数。 你实际上不知道它的大小,也不能将它与一个常数大小的矩阵相乘。

integral2(fun, xmin, xmax, ymin, ymax) 计算函数 z = fun(x, y) 在给定的 xy 范围内描述的平面下的体积。

因此,对于 (x, y) 的一个值,fun 应该 return 只有一个 z 值,在您的示例中并非如此。您应该真正检查函数的含义并确定要计算的积分。

函数integral2一次只能计算一个积分。如果你需要 要计算很多积分,你应该使用循环(我稍微缩小了网格大小):

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)

被积函数 R(xx,zz)xxzz 的大小不一定等于 1x1000000。 MATLAB 文档只说

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

例如,在我的系统上,它的大小是 14x14。