Why do I get "Array indices must be positive integers or logical values" error

3 views (last 30 days)
Muhammad Saad
Muhammad Saad on 27 Apr 2021
Commented: Muhammad Saad on 27 Apr 2021
t=0:0.01:4;
h1=t.*cos(2.*pi.*t);
subplot(6,1,1);
plot(t,h1);
title 'p(t)';
grid on;
h2=t.*exp(-2.*t);
subplot(6,1,2);
plot(t,h2);
title 'q(t)';
grid on;
h12=h1+h2;
subplot(6,1,3);
plot(t,h12);
title 's(t)';
grid on;
h3=u(t)-u(t-5);
th=0:0.01:8;
h=conv(h12,h3)*0.01;
subplot(6,1,4);
plot(th,h)
grid on;
legend('h(t)')
title 'overall impulse response'
tx=0:0.01:2;
x=tx.*exp(-2.*tx);
subplot(6,1,5);
plot(tx,x)
grid on;
legend('x(t)')
title 'Input signal'
ty=0:0.01:10;
y=conv(x,h)*0.01;
subplot(6,1,6);
plot(ty,y)
grid on;
legend('y(t)')
title 'System Response'
syms t;
int(y,t,-inf,inf);

Answers (1)

Dyuman Joshi
Dyuman Joshi on 27 Apr 2021
Because you are defining an array using negetive indexes.
> h3=u(t)-u(t-5); %u is undefined in your code
As you have defined t=0:0.01:4, values of t-5 become negetive numbers, which can not be used as indexes to define an array.

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by