Error using integral2 and arrayfun

20 次查看(过去 30 天)
Hello, I'm having some problems with integral2 and the arrayfun function. In my problem, I have a function that takes as inputs vectors of size 5000x1 (lets say it uses only 3 vectors), and returns a vector of size 5000x1. This function has multiple inputs and cannot be separated, and I have to integrate over two dimensions (vectors), and the other vectors have information relative to each individual in every element. I'm trying to do write my function with arrayfun, and then integrate with integrate2, but I get the following error:
"Error using arrayfun All of the input arguments must be of the same size and shape. Previous inputs had size 5000 in dimension 1. Input #3 has size 1"
I have "tested" the function using vectors and I have no problem. Here is a minimal working example:
Y=mvnrnd(0,1,5000);
normal=@(y,f1,f2) exp(y+f1+f2);
g= @(f1,f2) arrayfun(normal,Y,f1,f2);
test=g(zeros(5000,1),zeros(5000,1));
T = integral2(@(f1,f2)g(f1,f2),-inf,inf,-inf,inf);
Does somebody knows how to solve it? I am also considering doing this using the integral function with the 'ArrayValued' option, in order to avoid using arrayfun, but I haven't found a way to do this. I also have tried using the int function, but I have problems integrating over symbolic vectors.
Thanks in advance for your answers.

采纳的回答

Walter Roberson
Walter Roberson 2017-11-18
"Integrand, specified as a function handle, defines the function to be integrated over the planar region xmin ≤ x ≤ xmax and ymin(x) ≤ y ≤ ymax(x). The function fun must accept two arrays of the same size and return an array of corresponding values. It must perform element-wise operations."
That gets you the same size (but not necessarily square) arrays for f1 and f2, to be passed to g(). The size of those arrays will change between calls: you can just count on the two being the same size as each other for any one call.
Inside g you have arrayfun(normal,Y,f1,f2) . arrayfun() requires that all of the arguments after the function be either scalars or arrays that are the same size as each other. As indicated, f1 and f2 are going to be the same size as each other, but the exact size is going to change. But your Y is fixed size 1 x 5000 and your f1 and f2 are very unlikely to be exactly that size.
It is not possible to use integral2() to do an array integral directly. You cannot get integral2() to return a 5000 x 1 result: integral2() always returns a scalar result.
Perhaps you can use
T = arrayfun(@(y) integral2(@(f1,f2) normal(y,f1,f2),-inf,inf,-inf,inf), Y);
  2 个评论
Nicolás Suárez Chavarría
Thanks for your answer. The code runs, but I get 5000 warnings. For each element I obtain:
"Warning: The integration was unsuccessful", and then I get:
"Warning: Infinite or Not-a-Number value encountered."
This lead me to a T matrix full of NaNs.
Nicolás Suárez Chavarría
Correction, it worked!! The problem was with the example of the "normal" function. Now I defined a more real example, using a normal pdf, and now the code works. Before the integral exploded because of how it was defined. Here is a working code:
Y=mvnrnd(0,1,50);
normal=@(y,f1,f2)(2*pi)^(-0.5)* exp( -(y - f1- f2).^2/(2));
T = arrayfun(@(y) integral2(@(f1,f2) normal(y,f1,f2),-inf,inf,-inf,inf), Y);
Thanks again for your answer.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Numerical Integration and Differentiation 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by