Clear Filters
Clear Filters

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

2 views (last 30 days)
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!

Accepted Answer

David Goodmanson
David Goodmanson on 19 Mar 2020
Edited: David Goodmanson on 19 Mar 2020
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.

More Answers (0)

Categories

Find more on Graphics Object Programming in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!