# When I run the file I get an error

3 views (last 30 days)
Hernan Reyes on 29 Sep 2020
Answered: Ameer Hamza on 29 Sep 2020
Hello when I run the following code, I get an error of
Error using plot
Vectors must be the same length.
Error in convolution (line 8) plot (t, f_pulse);
Can you help me please Thank you
t=-pi:0.01:pi;
f_pulso=[zeros(1,1000*3.14+1),ones(1,1000*3.14+1),zeros(1,1000*3.14+1)];
a=2*pi;
b=cos(a*50*t);
f_conv=conv(f,f_pulso);
figure(1);
subplot(3,1,1);
plot(t,f_pulso);
title('Señal Pulso');
xlabel('Tiempo(t)');ylabel('e(t)');
subplot(3,1,2);
plot(t,b);
title('cos(2.pi.t)');
xlabel('Tiempo(t)');ylabel('cos(2*pi*100*t)');
%Convolucion
f_conv=conv(f,f_pulso);
subplot(3,1,3);
plot(t,f_conv(l:length(f)),'r')
title('Convolucion');
xlabel('Tiempo(t)');

Xingwang Yong on 29 Sep 2020
You should provide a reproducible snippet. I copy your code and run, it turns out "Unrecognized function or variable 'f'." at line 5.
By the way, you can use "Insert a line of code" tool in the toolbar for better readability.

Ameer Hamza on 29 Sep 2020
As the error message indicates, the x and y vectors must have equal lengths. For example,
f_pulso=[zeros(1,1000*3.14+1),ones(1,1000*3.14+1),zeros(1,1000*3.14+1)];
t=linspace(pi, pi, numel(f_pulso)); % instead of t=-pi:0.01:pi;
plot(t, f_pulse)