Search code examples
matlabdoubleintegral

Double integral expressed by a second variable


I'm having trouble with implementing double integral in Matlab.

Unlike other double integrals, I need the result of the first (inside) integral to be an expression of the second variable, before going through the second (outside) integral, as it must be powered by k.

For example:

Click here

In the example above, I need the result of the inside integral to be expressed as 2y, so that I can calculate (2y)^k, before doing the second (outside) integral.

Does anyone know how to do this in Matlab?


Solution

  • I don't like doing things symbolically, because 99.9% of all problems don't have a closed form solution at all. For 99.9% of the problems that do have a closed-form solution, that solution is unwieldy and hardly useful at all. That may be because of my specific discipline, but I'm going to assume that your problem falls in one of those 99.9% sets, so I'll present the most obvious numerical way to do this.

    And that is, integrate a function which calls integral itself:

    function dbl_int()
    
        f = @(x,y) 2.*x.*y + 1;    
        k = 1;
    
        x_limits = [0 1];
        y_limits = [1 2];
    
        val = integral(@(y) integrand(f, y, k, x_limits), ...
                       y_limits(1), y_limits(2));
    
    end
    
    function val = integrand(f, y, k, x_limits)
    
        val = zeros(size(y));
        for ii = 1:numel(y)        
            val(ii) = integral(@(x) f(x,y(ii)), ...
                               x_limits(1), x_limits(2));        
        end
    
        val = val.^k;
    
    end