Search code examples
matlabintegralindicator

Double integral with indicator function in Matlab (or R)?


I need to calculate this double integral:

where f(x) and f(y) are the density of the distribution of r.v a and y (they are distributed as normal and are independent each other)

with Matlab. I've tried this

I've tried this:

fun = @(x,y)((5-2*y)/3)*((1./sqrt(4*pi))*exp(-(x-2)^(2)/4))*((1./sqrt(8*pi))*exp(-(x-1)^(2)/8))
xmax = @(y)(5-2*y)/3
q = integral2(fun,-Inf,1,1,xmax)

but it doesn't work.

Any suggestions? (I'm not a Matlab expert). If not Matlab, with R it's ok.


Solution

  • Your function fun does not allow vectorized inputs, which means evaluating multiple inputs at once. Try for example:

    fun([1,2],[3,4])
    

    The expected output would be

    [fun(1,3),fun(2,4)]
    

    To fix this, you have to use the elementwise operations, which is already suggested to you via the error message:

    Error using ^ Inputs must be a scalar and a square matrix. To compute elementwise POWER, use POWER (.^) instead.