MATLAB: Error in convolution and i need someone to help me .

convolutionexponential

Now this is the code but when i run this code it keeps give me this error :- In an assignment A(I) = B, the number of elements in B and I must be the same. so please help me to find the problem. and thanks in advanced .
clear all;
t=-5:0.001:5;
x= zeros(size(t));
x(t>=0 & t<=5)=exp(-t);
subplot(3,1,1),plot(t,x);
h= zeros(size(t));
h(t>=0 & t<=5)=exp(-2*t);
subplot(3,1,2),plot(t,h);
a=conv(x,h);
tt=t(1)+t(1):0.001:t(end)+t(end);
subplot(3,1,3),plot(tt,a*0.001)
hold on
b=conv(h,x);
plot(tt,b*0.001)
hold off

Best Answer

Try this:
clc;
clear all;
t=-5:0.01:5;
x= zeros(size(t));
decay = exp(-t);
indexes = t>=0 & t<=5;
x(indexes) = decay(indexes);
subplot(3,1,1);
plot(t,x);
h= zeros(size(t));
decay = exp(-2*t);
h(indexes) = decay(indexes);
subplot(3,1,2),plot(t,h);
a=conv(x,h, 'same');
% Use linspace to get the same number of elements.
tt=linspace(t(1), t(end), length(a));
subplot(3,1,3);
plot(tt,a*0.001)
hold on
b=conv(h,x, 'same');
plot(tt,b*0.001)
hold off