Why receive error Integrand output size does not match the input size?

27 ビュー (過去 30 日間)
Mehdi
Mehdi 2023 年 1 月 20 日
コメント済み: Mehdi 2023 年 1 月 22 日
Where is the problem?
clear
syms x y
f=0*x*y;
Hf = matlabFunction(f,'Vars',[x y])
Hf = function_handle with value:
@(x,y)0.0
integral2(Hf,-1,1,-1,1);
Error using integral2Calc>integral2t/tensor
Integrand output size does not match the input size.

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 105)
Q = integral2Calc(fun,xmin,xmax,yminfun,ymaxfun,opstruct);

採用された回答

Torsten
Torsten 2023 年 1 月 20 日
編集済み: Torsten 2023 年 1 月 20 日
x and y that are inputs to Hf from integral2 are usually matrices (of the same size).
The output of Hf is expected to be of the same size as x (or y).
Thus if you only return 0 (a scalar value which is usually not of the same size as x (or y)), you get an error message.
Since I saw the typical functions you want to integrate are that complicated that this problem would never occur, I didn't include this simple case in my answer.
But if you want to cover the special case of a constant function, too, use
syms x y
f=0*x*y;
Hf = matlabFunction(f,'Vars',[x y])
Hf = function_handle with value:
@(x,y)0.0
Hf = @(x,y)Hf(x,y).*ones(size(x))
Hf = function_handle with value:
@(x,y)Hf(x,y).*ones(size(x))
integral2(Hf,-1,1,-1,1)
ans = 0
  4 件のコメント
Torsten
Torsten 2023 年 1 月 20 日
編集済み: Torsten 2023 年 1 月 20 日
I wonder why you go into these specialities and make things so complicated.
If you want to apply integral2 to each component of a vector-values function, use a (parfor) loop over its components.
Mehdi
Mehdi 2023 年 1 月 20 日
I used a (parfor) loop over its components, but receved error when the components are zero valued.

サインインしてコメントする。

その他の回答 (1 件)

Walter Roberson
Walter Roberson 2023 年 1 月 20 日
If you have f, a symbolic expression nominally in x and y, but which might in practice turn out to be independent of both x and y and so matlabFunction() will not vectorize, then you have three options.
First, you can do what @Torsten showed, of multiplying the output by ones() to do implicit expansion.
Second, you can use
Hf = matlabFunction(f,'Vars',[x y]);
wrapper = @(X,Y) arrayfun(Hf, X, Y);
result = integral2(wrapper, -1, 1, -1, 1);
Third, you can use
xlow = -1; xhigh = 1;
ylow = -1; yhigh = 1;
if isempty(symvar(f))
result = double(f) .* (xhigh - xlow) .* (yhigh - ylow);
else
Hf = matlabFunction(f, 'vars', [x, y]);
result = integral2(Hf, xlow, xhigh, ylow, yhigh);
end
as there is no need to call an integration function for a result so simple.
  9 件のコメント
Walter Roberson
Walter Roberson 2023 年 1 月 22 日
Do not use syms within a parfor loop. syms is not a "keyword", it is a MATLAB function, and it creates variables by using assignin('caller'), not through MATLAB having any special knowledge. That is a problem in parfor because parfor needs to see clearly where variables are created, but parfor does not know that syms creates variables.
ali()
function kdl = ali()
x = sym('x');
y = sym('y');
H=[0*x*y;0*x;0*y;0;x*y];
parfor i=1:length(H)
Hf = matlabFunction(H(i), 'Vars' ,[x y]);
wrapper = @(x,y) arrayfun(Hf, x, y);
kdl(i)= integral2(wrapper,-1,1,-1,1);
end
end
Mehdi
Mehdi 2023 年 1 月 22 日
Nice!

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeParallel for-Loops (parfor) についてさらに検索

製品


リリース

R2021b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by