Search code examples
matlabnumerichandlenumerical-integrationintegral

MATLAB: double numeric integration (very particular case)


I need to calculate a double integral where the inner function depends on both x and y (I integrate it on y and one of the limit depends on x), then I integrate the remaining on x. This is an example of code:

f1=@(x,y) x.^2+y; %inner function to be integrated on y

f2=@(x) sqrt(x).*integral(@(y)f1(x,y),x,3); %second function, to be integrated on x

V = integral(@(x)f2(x),0,2) %final calculation

I don't think integral2 could be used because of the form of the second function...

PS: I don't want to use symbolic functions

PPS: I know that this case could be solved by simple calculation on papar, but it's only a trivial case, what I need to do is way more complex

Thanks


Solution

  • Mathworks expected this problem while creating integral2() (after all, dependent bounds are not that uncommon), so they have a way to handle this case as well. I have selected another function to handle which is easier verified, but I assume you will not mind that much.

    f1=@(x,y) (x.*y)/2; %inner function to be integrated on y
    fy = @(x) 3-x; % Lower bound
    V = integral2(f1,0,2,fy,3) %final calculation
    

    More information on the "doc page" (doc integral2 in MATLAB terminal), or here

    Edit: In case you have an integral as

    integral(g(x) * integral( x^2*y, y=f(x), y=y1 ), x=a, x=b)
    

    where f1 = integral( x^2*y, y=f(x), y=y1 ), you can simplify as f2 = g(x)*f1 = integral( g(x)*x^2*y, y=f(x), y=y1 ).