integral2 error message

Suppose I use
D=120, A=3,C=1
pdf = @(x,y) D*power(x,A)*power(1-y,C);
integral2(pdf, 0, 1, 0, 1)
(note that this pdf is defined on {X<Y})
then I get an error message
Error in ABC>@(x,y)D*power(x,A)*power(1-y,C)
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 106)
Q = integral2Calc(fun,xmin,xmax,yminfun,ymaxfun,opstruct);
Error in UPA (line 85)
integral2(pdf, 0, 1, 0, 1)
Please advise.

4 comentarios

Torsten
Torsten el 28 de Mzo. de 2018

Note that your function is separable such that

int = D*integral(@(x)x.^A,0,1)*integral(@(y)(1-y).^C,0,1)

Best wishes

Torsten.

alpedhuez
alpedhuez el 28 de Mzo. de 2018
yes but the integral should be on {X<Y}. Please advise.
Torsten
Torsten el 29 de Mzo. de 2018
D=120;
A=3;
C=1;
fun = @(x,y) D*x.^A.*(1-y).^C;
ymin = @(x) x;
q = integral2(fun,0,1,ymin,1)
Best wishes
Torsten.
alpedhuez
alpedhuez el 29 de Mzo. de 2018
Thank you.

Iniciar sesión para comentar.

Respuestas (1)

Walter Roberson
Walter Roberson el 28 de Mzo. de 2018

0 votos

"The function fun must accept two arrays of the same size and return an array of corresponding values. It must perform element-wise operations."
However, the code line
pdf = @(x,y) D*power(x,A)*power(1-y,C);
does not do that. It does not do element-wise operations, and it fails if the two arrays do not happen to be square (which they typically are not.)
Remember that the * operator in MATLAB is algebraic matrix multiplication, not element-wise multiplication. Element-wise multiplication is the .* operator.

7 comentarios

alpedhuez
alpedhuez el 28 de Mzo. de 2018
Yes this is what I would like to understand. x, y, A, and C are all scalars. Why will it be a problem?
Steven Lord
Steven Lord el 28 de Mzo. de 2018
A and C are scalars by the way you defined them. The x and y inputs with which integral2 calls your function may not be scalars. As Walter quoted from the documentation, with some emphasis added:
"The function fun must accept two arrays of the same size and return an array of corresponding values. It must perform element-wise operations."
integral2 may call your function with scalars. It may call your function with non-scalar vectors. I don't think it will call the function with matrices due to the way it's implemented, but if you've used element-wise operations like .*, ./, .^, etc. it shouldn't matter if it does now or at some point in the future.
alpedhuez
alpedhuez el 28 de Mzo. de 2018
I see that the function does not see x and y are scalars at the time of definition. Then how about the restriction to the domain of the integration {X<Y}?
Steven Lord
Steven Lord el 28 de Mzo. de 2018
See the "Integrate Triangular Region with Singularity at the Boundary" example on the integral2 documentation page. It shows how to compute the integral over a non-rectangular region.
alpedhuez
alpedhuez el 28 de Mzo. de 2018
Let me work on it.
Walter Roberson
Walter Roberson el 28 de Mzo. de 2018
For X<Y you should reverse the order of integration,
pdf = @(y,x) D*power(x,A)*power(1-y,C);
and then use integral2() with
integral2(pdf, 0, 1, 0, @(y) y)
alpedhuez
alpedhuez el 28 de Mzo. de 2018
Thank you very much.

Iniciar sesión para comentar.

Productos

Etiquetas

Preguntada:

el 28 de Mzo. de 2018

Comentada:

el 29 de Mzo. de 2018

Community Treasure Hunt

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

Start Hunting!

Translated by