Error in convolution and i need someone to help me .

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

 채택된 답변

Image Analyst
Image Analyst 2013년 11월 30일
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

추가 답변 (1개)

Wayne King
Wayne King 2013년 11월 30일
편집: Wayne King 2013년 11월 30일
I'm assuming you want this:
x(t>=0 & t<=5)=exp(-t(t>=0 & t<=5));
h(t>=0 & t<=5) = exp(-2*t(t>=0 & t<=5));
Your mistake is in trying this:
h(t>=0 & t<=5) = exp(-2*t);
You can't restrict the range of the t elements and then try to assign all the elements to h.
So to fix your code:
t=-5:0.001:5;
x= zeros(size(t));
x(t>=0 & t<=5)=exp(-t(t>=0 & t<=5));
subplot(3,1,1),plot(t,x);
h= zeros(size(t));
h(t>=0 & t<=5) = exp(-2*t(t>=0 & t<=5));
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

카테고리

도움말 센터File Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

질문:

2013년 11월 30일

편집:

2013년 11월 30일

Community Treasure Hunt

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

Start Hunting!

Translated by