1

我在 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_yis<1x192 double>xare yboth <14x14 double>。在下一步中,f出现值为<154x192 double>。然后一切都消失了,除了xymatlab-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);
4

1 回答 1

1

这第一个功能是完全错误的。当你在w = x里面做的时候,你没有索引数组位置。

此外,如果这可行,您将返回一个线向量,即大小1xlength(x),当您执行 w_x'*w_y 时,您正在执行length(x)x1times 1xlength(y),这将为您提供一个 matrix length(x)*length(y)

考虑更正您的功能:

function w = W_1D(x,pos)
   w = zeros(length(x),1); % Allocate w as column vector, so that the product gives a scalar (as I suppose that it is what you want.
   for ii=1:length(x) % Here, so that is indexes w and x elements as you need
       w(ii)=x(ii) - pos; % I changed your code to something that makes sense, but I don't know if that is what you want to do, you have to adapt it to work correctly.
   end
end

您可能还想调试您的函数,考虑keyboard在您的操作之前添加并使用dbstep. IE:

function f = W_2D(x,y,pos_1,pos_2)
  w_x = W_1D(x,pos_1);
  w_y = W_1D(y,pos_2);
  keyboard
  f = w_x'*w_y;
end

执行将在 停止keyboard,然后您可以检查w_x大小,w_y大小,并执行dbstepf = w_x'*w_y查看它返回的内容。完成调试后,您可以这样dbcont做,它将继续执行。

这个答案是草稿,因为很难帮助您提供您提供的信息。但我认为你可以开始解决这个问题。如果您有更多疑问,请随时提问。

于 2013-09-17T19:25:43.647 回答