Problem using Integral2 for functions only depending on one variable
You are now following this question
- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.
An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Show older comments
0 votes
I want to compute a lot of integrals over 2D ansatz functions and products of their derivatives using integral2.
The problem is that some integrands will be 0, constant or only depending on 1 variable.
Because of this integral2 will not work. Error Message below
But i have to use integral2 otherwise i would have to do handle way too many exceptions.
I have reduced and simplified my code to the following:
n=10; %number of ansatz functions
ansatzf=cell(n,1); %my cell array of ansatz functions
syms x y;
for i = 1:n
ansatzf{i}= @(x,y) x.^i.*y.^i; %defining the ansatz functions
end
test = matlabFunction(diff(ansatzf{1},x,1)) %test is now only dependent on y
integral2(test,0,1,0,1) %this integral2 does not work because test is only depending on y
>> TestIntegralTwo
%This is the Error message i get:
test =
function_handle with value:
@(y)y
Error using symengine>@(y)y
Too many input arguments.
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 105)
Q = integral2Calc(fun,xmin,xmax,yminfun,ymaxfun,opstruct);
Error in TestIntegralTwo (line 10)
integral2(test,0,1,0,1)
How can i change my code so that integral2 can work?
Thank you for any help
Accepted Answer
Steven Lord
on 24 Mar 2022
You can tell matlabFunction exactly what variables the generated function should accept as input arguments.
syms x y
ansatzf = x.*y.^2;
d = diff(ansatzf, x)
d = 
M_justY = matlabFunction(d)
M_justY = function_handle with value:
@(y)y.^2
M_both = matlabFunction(d, 'Vars', [x y])
M_both = function_handle with value:
@(x,y)y.^2
M_justY(1:5)
ans = 1×5
1 4 9 16 25
M_both(NaN, 1:5) % M_both doesn't use x so it can be whatever you want
ans = 1×5
1 4 9 16 25
17 Comments
Lino Marschall
on 22 Apr 2022
Hey Steven,
Thank you for your answer. This helped solve most of the problem. But there is still something that i don't know how to solve.
In your code example this works fine if you derive ansatzf only once by x.
The problem which still remains for me however is that i have to compute integrals (with integral2) over derivatives, where i know the derivative is 0 anyways.
I have to do this as it would be much more complicated in my case to find all the integrals that are 0.
In short: I want to integrate functions that are 0 everywhere with integral2
Is there a solution to this problem?
Bruno Luong
on 22 Apr 2022
Edited: Bruno Luong
on 22 Apr 2022
"I want to integrate functions that are 0 everywhere with integral2"
What's a problem?:
integral2(@(x,y) zeros(size(x)), 0,1,0,1)
ans = 0
It doesn' matter the integrant expression depends on x, y, (x,y) or none. You need to declare it as two-variables function.
Torsten
on 22 Apr 2022
For integral2, define
M_both = matlabFunction(d, 'Vars', [x y]);
M_both = @(x,y)eye(size(x))*M_both(x,y);
Lino Marschall
on 22 Apr 2022
I was too vague sorry.
I meant how can i integrate something like this: (using integral2)
syms x y
exampleFct= @(x,y) x.*y^2;
test = matlabFunction(diff(exampleFct,x,2),'Vars',[x,y]);
integral2(test,0,1,0,1)
Torsten
on 22 Apr 2022
The OP doesn't know in advance how matlabFunction (returned from a symbolic calculation) is built.
syms x y
exampleFct=x.*y^2;
ddexampleFct=diff(exampleFct,x,2)
ddexampleFct=matlabFunction(ddexampleFct,'Vars',[x,y]);
ddexampleFct=@(x,y)eye(size(x))*ddexampleFct(x,y);
integral2(ddexampleFct,0,1,0,1)
Bruno Luong
on 22 Apr 2022
We just tell you
syms x y
exampleFct= @(x,y) x.*y^2;
test = matlabFunction(diff(exampleFct,x,2),'Vars',[x,y]);
integral2(@(x,y) zeros(size(x))+test(x,y),0,1,0,1)
Bruno Luong
on 22 Apr 2022
@Torsten Do you meant ".*" rather than "*"?
Lino Marschall
on 22 Apr 2022
This works in my problem.
Thank you so much for your help Bruno, Torsten and Steven.
But i dont quite undestand why this works. Why is the way i tried not working?
Bruno Luong
on 22 Apr 2022
Edited: Bruno Luong
on 22 Apr 2022
integral2 requires a vectorized user-provided function, x and y input are vectors and it must return the vector result evaluated at (x,y). That why you need to add zeros(size(x)) to expand the matlabFunction output.
No, I meant usual matrix multiplication.
x and y are passed as nx*nx matrices. So fun(x,y) can always be replaced by eye(size(x))*fun(x,y), I guess.
Bruno Luong
on 22 Apr 2022
Edited: Bruno Luong
on 22 Apr 2022
f=@(x,y) x.*y.^2;
integral2(@(x,y) zeros(size(x))+f(x,y), 0,1,0,1)
ans = 0.1667
integral2(@(x,y) ones(size(x)).*f(x,y), 0,1,0,1)
ans = 0.1667
integral2(@(x,y) ones(size(x))*f(x,y), 0,1,0,1)
Error using *
Incorrect dimensions for matrix multiplication. Check that the number of columns in the first matrix matches the number of rows in the second matrix. To operate on each element of the matrix individually, use TIMES (.*) for
elementwise multiplication.
Incorrect dimensions for matrix multiplication. Check that the number of columns in the first matrix matches the number of rows in the second matrix. To operate on each element of the matrix individually, use TIMES (.*) for
elementwise multiplication.
Error in solution (line 4)
integral2(@(x,y) ones(size(x))*f(x,y), 0,1,0,1)
Error in integral2Calc>integral2t/tensor (line 237)
Z1 = FUN(X(VTSTIDX),Y(VTSTIDX)); 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 105)
Q = integral2Calc(fun,xmin,xmax,yminfun,ymaxfun,opstruct);
fun=@(x,y) eye(size(x))*(x.*y.^2);
q = integral2(fun,0,1,0,1)
q = 0.1667
x.*y.^2 is a matrix of the same size as x.
So why should it not be possible to multiply it with eye(size(x)) and get back x.*y.^2 ?
At least in octave, x and y are of the same size. Maybe it's different in MATLAB.
Bruno Luong
on 22 Apr 2022
fun=@(x,y) eye(size(x))*(x.*y.^2);
q = integral2(fun,0,1,0,1)
Error using *
Incorrect dimensions for matrix multiplication. Check that the number of columns in the first matrix matches the number of rows in the second matrix. To operate on each element of the matrix individually, use TIMES (.*) for
elementwise multiplication.
Incorrect dimensions for matrix multiplication. Check that the number of columns in the first matrix matches the number of rows in the second matrix. To operate on each element of the matrix individually, use TIMES (.*) for
elementwise multiplication.
Error in solution (line 1)
fun=@(x,y) eye(size(x))*(x.*y.^2);
Error in integral2Calc>integral2t/tensor (line 237)
Z1 = FUN(X(VTSTIDX),Y(VTSTIDX)); 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 105)
Q = integral2Calc(fun,xmin,xmax,yminfun,ymaxfun,opstruct);
Torsten
on 22 Apr 2022
Ok, then it's somehow different in MATLAB compared to Octave.
As said, in Octave x and y are quadratic matrices of the same size - thus no reason for this error.
Bruno Luong
on 22 Apr 2022
Edited: Bruno Luong
on 22 Apr 2022
MATLAB doc only states x and y have the same size, not necessary a square size.
Torsten
on 22 Apr 2022
Ok, this will be the reason for the error.
Then your solution or ones(size(x)).*fun(x,y) will work in MATLAB, too.
More Answers (0)
Categories
Find more on Code Performance in Help Center and File Exchange
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)