Why receive error Integrand output size does not match the input size?
Show older comments
Where is the problem?
clear
syms x y
f=0*x*y;
Hf = matlabFunction(f,'Vars',[x y])
integral2(Hf,-1,1,-1,1);
Accepted Answer
More Answers (1)
Walter Roberson
on 20 Jan 2023
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 Comments
Walter Roberson
on 20 Jan 2023
If you are looping over multiple expressions then (xhigh - xlow) .* (yhigh - ylow) could be pre-computed, making the calculation even more simple.
Walter Roberson
on 20 Jan 2023
Hf = matlabFunction(H(i),'Vars',[xx yy]);
According to your question, your variables are named x and y not xx and yy
you do not need to use the same variable names there as you use in the
wrapper = @(xx,yy) arrayfun(Hf, xx, yy);
line. The inputs to the wrapper functions will be entire arrays, and the arrayfun will cause Hf to be invoked on individual values selected out of xx and yy . So your Hf will be passed scalar values x, y, whereas wrapper will be passed arrays.
If this is not sufficiently clear you could write
wrapper = @(xx,yy) arrayfun(@(x,y)Hf(x,y), xx, yy);
but that would be less efficient than
wrapper = @(xx,yy) arrayfun(Hf, xx, yy);
Mehdi
on 20 Jan 2023
Walter Roberson
on 20 Jan 2023
Edited: Walter Roberson
on 20 Jan 2023
Please post code the reproduces the problem -- code that we can execute.
Maybe this serves your purpose since all the functions you considered until now were complicated functions of x and y.
I don't know the MATLAB internals and the cause of the error if the x and/or y argument to integral2 come into play.
kdl = ali(3)
function kdl = ali(M)
syms x y
H=[x*y;x*y^2;x^2*y];
parfor i=1:M
Hf = matlabFunction(H(i));
kdl(i)= integral2(Hf,-1,1,-1,1);
end
end
Walter Roberson
on 22 Jan 2023
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
on 22 Jan 2023
Categories
Find more on Time Series Events in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!