Serving the Quantitative Finance Community

 
User avatar
reaverprog
Topic Author
Posts: 0
Joined: October 28th, 2009, 8:53 am

Triple integral in Matlab

July 24th, 2013, 8:12 pm

Hi all,After searchin in the internet, we can use the function triplequad to compute a triple integral.However it's not working for the following example :>> S = [1 1/sqrt(2) 1/sqrt(3); 1/sqrt(2) 1 sqrt(2)/sqrt(3); 1/sqrt(3) sqrt(2/3) 1]>> F = @(x,y,z)exp([x y z]*inv(S)*transpose([x y z]));>> Q = triplequad(F,0,1,0,1,-1,1);??? Error using ==> mtimesInner matrix dimensions must agree.Error in ==> @(x,y,z)exp([x,y,z]*inv(S)*transpose([x,y,z]))Error in ==> quad at 77y = f(x, varargin{:});Error in ==> triplequad>innerintegral at 69 Q(i) = quadf(intfcn, xmin, xmax, tol, trace, y(i), z, varargin{:});Error in ==> dblquad>innerintegral at 80fcl = intfcn(xmin, y(1), varargin{:}); %evaluate only to get the class belowError in ==> quad at 77y = f(x, varargin{:});Error in ==> dblquad at 60Q = quadf(@innerintegral, ymin, ymax, tol, trace, intfcn, ...Error in ==> triplequad at 53Q = dblquad(@innerintegral, ymin, ymax, zmin, zmax, tol, quadf, intfcn, ...Can someone help please to understand why it's not working for this example ? does triple quad not support vectors included in functions ?Thanks
 
User avatar
LeonAtWilmott
Posts: 0
Joined: January 14th, 2009, 7:01 pm

Triple integral in Matlab

July 27th, 2013, 4:23 pm

This is probably because Matlab uses entry-by-entry operation when applying Gaussian quadrature. In this case, your expression in terms of matrix becomes problematic. Try to define in entrywise sense as below: InvS = inv(S);FF = @(x,y,z) exp( InvS(1,1)*(x.*x)+InvS(1,2)*(x.*y)+InvS(1,3)*(x.*z)+ InvS(2,1)*(y.*x)+InvS(2,2)*(y.*y)+InvS(2,3)*(y.*z)+ InvS(3,1)*(z.*x)+InvS(3,2)*(z.*y)+InvS(3,3)*(z.*z) );And then run QQ = triplequad(FF,0,1,0,1,-1,1);I got QQ = 698.5426 .