I am trying to solve a problem of the following form:
f=@(x,y,z,w) x.*y.*z.*w; % A complicated black box function
a=1;b=1;c=1;d=1; % Integration limits
I=integral2(@(x,y)integral2(@(z,w)f(x,y,z,w),c,-c,d,-d),a,-a,b,-b);
Using this implementation I get the following error:
Error using .*
Matrix dimensions must agree.
The problem is that x, y, z, and w are not the same size. For the first function evaluation all inputs are the same size but then on the second function evaluation x and y are not the same size as z and w.
How can I resolve this error?
This question is similar to this unanswered question:
Input array size error for a quadraple integration using nested integral2
==================================================================================
In response to the answer:
I=integral(@(x)integral3(@(y,z,w)f(x,y,z,w),b,-b,c,-c,d,-d),a,-a,'ArrayValued',true);
This does solve the problem, however, it is not obvious to me why this works. I had actually seen this solution before but forgot to mention it in my question (http://www.mathworks.com/matlabcentral/answers/77571-how-to-perform-4d-integral-in-matlab).
I would like to solve using nested integral2 because I know my function is discontinuous and would like to use the iterated integration method. I could do something like this but only the inner integral is iterated so I am not sure how that affects accuracy:
I=integral(@(x)integral3(@(y,z,w)f(x,y,z,w),b,-b,c,-c,d,-d,'Method','iterated'),a,-a,'ArrayValued',true);
See Question&Answers more detail:
os