Problem using Integral2 for functions only depending on one variable
이 질문을 팔로우합니다.
- 팔로우하는 게시물 피드에서 업데이트를 확인할 수 있습니다.
- 정보 수신 기본 설정에 따라 이메일을 받을 수 있습니다.
오류 발생
페이지가 변경되었기 때문에 동작을 완료할 수 없습니다. 업데이트된 상태를 보려면 페이지를 다시 불러오십시오.
이전 댓글 표시
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
채택된 답변
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
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
2022년 4월 22일
편집: Bruno Luong
2022년 4월 22일
"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.
For integral2, define
M_both = matlabFunction(d, 'Vars', [x y]);
M_both = @(x,y)eye(size(x))*M_both(x,y);
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)
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)
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)
@Torsten Do you meant ".*" rather than "*"?
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
2022년 4월 22일
편집: Bruno Luong
2022년 4월 22일
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
2022년 4월 22일
편집: Bruno Luong
2022년 4월 22일
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.
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);
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
2022년 4월 22일
편집: Bruno Luong
2022년 4월 22일
MATLAB doc only states x and y have the same size, not necessary a square size.
Ok, this will be the reason for the error.
Then your solution or ones(size(x)).*fun(x,y) will work in MATLAB, too.
추가 답변 (0개)
카테고리
도움말 센터 및 File Exchange에서 Code Performance에 대해 자세히 알아보기
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!웹사이트 선택
번역된 콘텐츠를 보고 지역별 이벤트와 혜택을 살펴보려면 웹사이트를 선택하십시오. 현재 계신 지역에 따라 다음 웹사이트를 권장합니다:
또한 다음 목록에서 웹사이트를 선택하실 수도 있습니다.
사이트 성능 최적화 방법
최고의 사이트 성능을 위해 중국 사이트(중국어 또는 영어)를 선택하십시오. 현재 계신 지역에서는 다른 국가의 MathWorks 사이트 방문이 최적화되지 않았습니다.
미주
- América Latina (Español)
- Canada (English)
- United States (English)
유럽
- 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)
