Unit Step Function and Subscript Indices Error
Show older comments
I have been asked to write a code to stem plot discrete-time functions. I have the values the for 'a' and 'n', but not the values for 'u'.
I have included a hint which was given to me in the code for finding the values of 'u'. When i follow the hint i get a 1x41 matrix with 0's and 1's inside.
This produces error "Subscript indices must either be real positive integers or logicals." So i set u = 1:20 and still get the same error.
I suppose my issue is obtaining the correct values for 'u' and removing the above error. I hope i have provided enough supporting information.
a1 = 0.8;
a2 = 1;
a3 = 1.5;
n = -20:20;
%I have been given hint "The unit step function y = u(n) may be computed
%in Matlab using the command y = (n>=0), where n is a vector of values of
%time indices.
u = 1:20;
aa = (a1.^n)*(u(n) - u(n-10))
ab = (a2.^n)*(u(n) - u(n-10))
ac = (a3.^n)*(u(n) - u(n-10))
2 Comments
M
on 30 Oct 2017
You get the error
"Subscript indices must either be real positive integers or logicals."
because you defined
u=1:20
which is an array from 1 to 20, such that
u(i)=i, i=1...20
but you want to access
u(n-10), with n = -20:20
so you ask MATLAB to use the value of u(-30), which does not exist.
Vaidyanathan Thiagarajan
on 2 Nov 2017
Edited: Vaidyanathan Thiagarajan
on 2 Nov 2017
Yes, but you cannot even write 'u(n)' as 21 out of 41 indices (as n = -20:20) are negative. In MATLAB, array indices can only be positive integers (i.e. >= 1).
Answers (0)
Categories
Find more on Logical in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!