Hello. Im currently creating a code in Matlab and I am running into this error that I will attach in a screenshot. I am new to Matlab so I am having trouble fixing it.

1 次查看(过去 30 天)
clear all
clc
t=0:0.01:5;
h=12.0 * exp(-2.0*t).*(t>= 0);
x=5* (t >= 0)-5* (t >=1.0);
y=conv(x,h,'full')*0.01;
figure;
subplot(3,1,1);
plot(t,h,'r','Linewidth',2);
title('Impulse Response h(t)');
xlabel('time (s)');
ylabel('amplitude');
subplot(3,1,2);
plot(t,x,'b','Linewidth',2);
title('input signal x(t)');
xlabel('time (s)');
ylabel('amplitude');
subplot(3,1,3);
plot(t(1:length(y)),y,'g','Linewidth',2);
Index exceeds the number of array elements. Index must not exceed 501.
title('Analytical System Response y(t)');
xlabel('time (s)');
ylabel('amplitude');
  3 个评论
John D'Errico
John D'Errico 2024-2-7
编辑:John D'Errico 2024-2-7
When you attach a picture of code, you make it far more difficult for someone to help you, since we are forced to retype your code completely.
What is worse, if you don't even show the error you got! Can we read your mind? I'm tryign, but the MATLAB crystal ball is foggy today.
Is there a good reason why you want to make it more difficult to get help, since it is trivial to paste in the text itself?
When you do give us the text, also paste in the COMPLETE ERROR MESSAGE. So EVERYTHING in red.
Logan
Logan 2024-2-7
here is the code that is in the screenshot. Im sorry, this is the first question I have asked and did not know the exact specifics on how to post. The error that I am getting is "Index exceeds the number of array elements. Index must not exceed 501."
clear all
clc
t=0:0.01:5;
h=12.0 * exp(-2.0*t).*(t>= 0);
x=5* (t >= 0)-5* (t >=1.0);
y=conv(x,h,'full')*0.01;
figure;
subplot(3,1,1);
plot(t,h,'r','Linewidth',2);
title('Impulse Response h(t)');
xlabel('time (s)');
ylabel('amplitude');
subplot(3,1,2);
plot(t,x,'b','Linewidth',2);
title('input signal x(t)');
xlabel('time (s)');
ylabel('amplitude');
subplot(3,1,3);
plot(t(1:length(y)),y,'g','Linewidth',2);
Index exceeds the number of array elements. Index must not exceed 501.
title('Analytical System Response y(t)');
xlabel('time (s)');
ylabel('amplitude');

请先登录,再进行评论。

回答(2 个)

Walter Roberson
Walter Roberson 2024-2-7
y=conv(x,h,'full')*0.01;
The length of a 'full' convolution is longer than than the individual inputs: it is (length(First) + length(Second) - 1)
plot(t(1:length(y)),y,'g','Linewidth',2);
You use the length of the full convolution to index something that is the length of the original signal.

Image Analyst
Image Analyst 2024-2-7
If you want to clip off the ends, use the 'same' option instead of 'full' in conv.

标签

产品


版本

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by