MATLAB: How to do this double integration using integral2? Thanks!

double integralintegral2joint density

Hi, I'm trying to write some code to calculate accumulated the probability of using integral2. Here is my code:
clear;
ae=-4;be=4;
az=-8;bz=8;
fune=@(e)1/(be-ae)*(e>=ae).*(e<=be); % density of e
funz=@(z)1/(bz-az)*(z>=az).*(z<=bz); % density of z
funezt=@(ez,t)fune(ez(:)-t).*funz(t);
funez=@(ez)integral(@(t)funezt(ez,t),az,bz,'ArrayValued',true); % density of ez=e+z computed via convolution
funez12=@(ez1,ez2)funez(ez1).*funez(ez2); % joint density of (ez1,ez2)
aaa=integral2(funez12,0,4,0,4); %double integration on [0,4]^2
However, this code didn't work and has the following error message:
Error using integral2Calc>integral2t/tensor (line 241)
Integrand output size does not match the input size.
Have no clue about how to fix it, and need some insights. Thanks!

Best Answer

To use a function as input to integral2, the input and output dimension must be the same. However, in your case, if we input a 2x2 matrices to funez12, the output is 4x1. Changing the line like this will correct the error
funez12 = @(ez1,ez2)reshape(funez(ez1).*funez(ez2), size(ez1)); % joint density of (ez1,ez2)
However, the correct solution depends on how you interpret the entries in the matrix.