# MATLAB: Efficient way to compute a double integral within a double integral

integralintegrationnumerical integration

Hi all,
I would like to know what is the easiest and most efficient way to solve the integration shown in the figure attached. I tried the following code, but it doesn't work:
``R1 = @(x1,x2) integral2(@(x3,x4) fun_g(x1,x2,x3,x4),l3,u3,l4,u4);R = integral2(R1.*@fun_f,l1,u1,l2,u2);function f = fun_f(x1,x2)% here we define f function (it is a rather complex function)endfunction g = fun_g(x1,x2,x3,x4)% here we define g function (it is a rather complex function)end``
• ``function main  l1 = ...;  u1 = ...;  l2 = ...;  u2 = ...;  value_outer_integral = integral2(@fun,l1,u1,l2,u2)endfunction value_inner_integral = fun(x1,x2)  l3 = ...;  u3 = ...;  l4 = ...;  u4 = ...;  for i = 1:numel(x1)    value_inner_integral(i) = fun_f(x1(i),x2(i))*integral2(@(x3,x4)driver_fun_g(x1(i),x2(i),x3,x4),l3,u3,l4,u4);  endendfunction g = driver_fun_g(x1,x2,x3,x4)  for i = 1:numel(x3)    g(i) = fun_g(x1,x2,x3(i),x4(i));  endendfunction f = fun_f(x1,x2)  % return f(x1,x2) for scalar values of x1 and x2endfunction g = fun_g(x1,x2,x3,x4)  % return g(x1,x2,x3,x4) for scalar values of x1, x2, x3 and x4end``