MATLAB: How to create a function as the convolution of two functions? Thanks

convolutionintegrationprobability density

Hi guys, I want to compute the convolution of two probability densities, and as an experiment I tried two uniform [0,1] densities:
fune=@(e)(e>=0).*(e<=1);
funz=@(z)(z>=0).*(z<=1);
funezt=@(ez,t)fune(ez-t).*funz(t);
funez=@(ez)integral(@(t)funezt(ez,t),0,2); % calculation using the definition of convolution
vecez=0:0.01:2;
vecfunez=funez(vecez);
plot(vecez,vecfunez)
However, this didn't work and the error message I got is:
Matrix dimensions must agree.
Error in convolutionexperiment>@(ez,t)fune(ez-t).*funz(t) (line 4)
funezt=@(ez,t)fune(ez-t).*funz(t);
Need some insignts and guidance on fixing this. Thanks!

Best Answer

Hello James,
fune=@(e)(e>=0).*(e<=1);
funz=@(z)(z>=0).*(z<=1);
funezt=@(ez,t)fune(ez-t).*funz(t);
funez=@(ez)integral(@(t)funezt(ez,t),0,2,'ArrayValued',true); % <--
vecez=0:0.01:2;
vecfunez=funez(vecez);
plot(vecez,vecfunez)
A useful Matlab option.