I am trying to perform multidimensional integration, using the following algorithm:
y= @(a,b,c) a+b+c; %function - 3 dim
y1=@(a,b) integral(@(c) y(a,b,c),-20,20); % L,H - limits for c`
y2=@(a) integral(@(b) y1(a,b),-20,20); % L,H - limits for b
y3=integral(@(a) y2(a),-20,20); % L,H - limits for a
but this gives the following error:
Error using integralCalc/finalInputChecks (line 515)
Output of the function must be the same size as the input.
If FUN is an array-valued integrand, set the 'ArrayValued' option to true.
Error in integralCalc/iterateScalarValued (line 315)
finalInputChecks(x,fx);
Error in integralCalc/vadapt (line 132)
[q,errbnd] = iterateScalarValued(u,tinterval,pathlen);
Error in integralCalc (line 75)
[q,errbnd] = vadapt(@AtoBInvTransform,interval);
Error in integral (line 88)
Q = integralCalc(fun,a,b,opstruct);
Error in @(a)integral(@(b)y1(a,b),-20,20)
Error in integralCalc/iterateScalarValued (line 314)
fx = FUN(t);
Error in integralCalc/vadapt (line 132)
[q,errbnd] = iterateScalarValued(u,tinterval,pathlen);
Error in integralCalc (line 75)
[q,errbnd] = vadapt(@AtoBInvTransform,interval);
Error in integral (line 88)
Q = integralCalc(fun,a,b,opstruct);
Can somebody please help me understand and fix my mistake, or suggest a better method?
P.S.
I know about the integral3
function, but I need this method, because I am going to try 4,5,6.... dimensions later.
I am not sure if this will work in any possible case, but it works quite well for this simple case. Just use symbolic maths.
syms a b c
y=a+b+c;
y1=int(y,c,-20,20)
y2=int(y1,b,-20,20)
y3=int(y2,a,-20,20)
However, careful creating the variables. Don't create yn
dynamically!