Search code examples
matlabnumerical-methodsnumerical-integrationcalculus

Numeric integration for two variables and a list of parameters?


In the MATLAB documentation, they have an example for numeric integration for a single variable with one parameter c as:

fun = @(x,c) 1./(x.^3-2*x-c);
q = integral(@(x)fun(x,5),0,2)

What if I want to do numeric integration with two variables and maybe two parameters?


Solution

  • If you want to do integration with two variables you need to use integral2.

    An example with two variables:

    fun = @(x,y) 1./( sqrt(x + y) .* (1 + x + y).^2 );
    ymax = @(x) 1 - x;
    q = integral2(fun,0,1,0,ymax)
    
    q =
        0.2854
    

    If you want several parameters, and two variables do:

    fun = @(x,y,c,d) c./(sqrt(x + d*y) .* (1 + x + y).^2);
    ymax = @(x) 1 - x;
    q = integral2(@(x,y) fun(x,y,3,4),0,1,0,ymax)
    
    q =
    0.5708
    

    Or simply:

    c = 3; d = 4;
    fun = @(x,y) c./( sqrt(x + d*y) .* (1 + x + y).^2 )
    ymax = @(x) 1 - x;
    q = integral2(fun,0,1,0,ymax)
    
    q = 
        0.5708