我在 matlab 中创建了一个函数,它返回一个像
function w = W_1D(x,pos,h)
w=zeros(1,length(x));
if (h~=0)
xmpos = x-pos;
inds1 = (-h <= xmpos) & (xmpos < 0);
w(inds1) = xmpos(inds1)./h + 1;
inds2 = (0 <= xmpos) & (xmpos <= h);
w(inds2) = -xmpos(inds2)./h + 1;
else
error('h shouldn't be 0')
end
end
因此,最后,有一个w
大小为 的向量length(x)
。现在我创建了第二个函数
function f = W_2D(x,y,pos_1,pos_2,h)
w_x = W_1D(x,pos_1,h);
w_y = W_1D(y,pos_2,h);
f = w_x'*w_y;
end
哪里length(x)=length(y)
。因此,该函数W_2D
显然返回一个矩阵。但是当我现在尝试评估矩形域上的积分时,例如
V = integral2(@(x,y) W_2D(x,y,2,3,h),0,10,0,10);
matlab 返回一些错误:
Error using integral2Calc>integral2t/tensor (line 242)
Integrand output size does not match the input size.
Error in integral2Calc>integral2t (line 56)
[Qsub,esub] = tensor(thetaL,thetaR,phiB,phiT);
Error in integral2Calc (line 10)
[q,errbnd] = integral2t(fun,xmin,xmax,ymin,ymax,optionstruct);
Error in integral2 (line 107)
Q = integral2Calc(fun,xmin,xmax,yminfun,ymaxfun,opstruct);
我还尝试在W_2D
-function 中更改某些内容:而不是f = w_x'*w_y;
尝试f = w_x.'*w_y;
or w_y = transpose(w_y); f = kron(w_x,w_y);
,但是 Integrand 输出大小的东西总是存在这个错误。谁能解释一下,我的错在哪里?
编辑:在 Werner 用键盘调试方法提示之后,我可以告诉你以下内容。第一步返回w_x
类型<1x154 double>
、w_y
is<1x192 double>
和x
are y
both <14x14 double>
。在下一步中,f
出现值为<154x192 double>
。然后一切都消失了,除了x
和y
matlab-function integral2Calc.m 出现在编辑器中并跳转到函数调用堆栈integral2t/tensor
,经过更多步骤后,错误发生在这里
Z = FUN(X,Y); NFE = NFE + 1;
if FIRSTFUNEVAL
if ~isfloat(Z)
error(message('MATLAB:integral2:UnsupportedClass',class(Z)));
end
% Check that FUN is properly vectorized. This is important here
% because we (otherwise) always pass in square matrices, which
% reduces the probability of the user generating an error by
% using matrix functions instead of elementwise functions.
Z1 = FUN(X(VTSTIDX),Y(VTSTIDX)); NFE = NFE + 1;
if ~isequal(size(Z),size(X)) || ~isequal(size(Z1),size(VTSTIDX))
% Example:
% integral2(@(x,y)1,0,1,0,1)
error(message('MATLAB:integral2:funSizeMismatch'));
end
希望信息足够详细...我不知道会发生什么,因为我的示例与 mathworks 网站上给出的完全相同integral2
,不是吗?
也许我应该更精确一点,我想做的事情:因为 W_2D 给了我一个紧凑支持的二维帽子函数的表面 w(x,y),存储在一个矩阵w
中,我想计算 ( x,y)-平面和表面 z=w(x,y)...
EDIT2:我仍然不明白如何处理这个问题,它integral2
创建矩阵作为我的W_1D
-functions 的输入,这些函数被调用W_2D
并打算有一个<1xn double>
-valued 输入并返回一个<1xn double>
输出,但至少我可以简单地使用以下内容通过使用两个一维调用求解张量积上的积分integral
,即
V = integral(@(x)integral(@(y)W_1D(y,3,h),0,10).*W_1D(x,2,h),0,10);