Subscript indices must either be real positive integers or logicals.

1 view (last 30 days)
Hi,
I get this error ,
Subscript indices must either be real positive integers or logicals. The line with this error is,
t=linspace(1,10);
F0= 100;
m= 1;
k= 10^2;
w= 11;
wn=sqrt(k./m);
x(t) = (F0/m)*(1/wn^2-w^2)*(sin(t.*w)-sin(t.*wn));
Please help me in resolving this error and what is meant by subscript indices?

Accepted Answer

pfb
pfb on 12 Apr 2015
Edited: pfb on 12 Apr 2015
I guess the line giving grief is
x(t) = (F0/m)*(1/wn^2-w^2)*(sin(t.*w)-sin(t.*wn));
x is a vector and t is the subscript of the vector. That is, x(t) is a way to write x_t, the t-th element of x. Subscript should be positive integers.
Taking a look at the vector t should clarify the error.
Also, better look at the documentation of "linspace". Type "help linspace" for that.

More Answers (2)

Star Strider
Star Strider on 12 Apr 2015
The ‘subscript indices’ error is the result of MATLAB correctly interpreting ‘x(t)’ as an array reference. That is the appropriate syntax for referencing an array, but all array index references must be integers greater than zero.
If you want ‘x’ to be a function (an anonymous function here), create it as:
x = @(t) (F0/m)*(1/wn^2-w^2)*(sin(t.*w)-sin(t.*wn));
and to evaluate it and plot it:
x = @(t) (F0/m)*(1/wn^2-w^2)*(sin(t.*w)-sin(t.*wn));
xt = x(t);
figure(1)
plot(t, xt)
  3 Comments
Image Analyst
Image Analyst on 12 Apr 2015
Well I'll also mention the FAQ, which answers this question quite well, and vote for your Answer, which minji can still do to give you reputation points.

Sign in to comment.


Image Analyst
Image Analyst on 12 Apr 2015
I'd give a different answer than the other two. I'd do just what you did but get rid of the (t) after the x :
t = linspace(1,10);
F0 = 100;
m = 1;
k = 10^2;
w = 11;
wn = sqrt(k/m);
x = (F0/m) * (1/wn^2-w^2) * (sin(t*w)-sin(t*wn));
plot(t, x, 'LineWidth', 2);
grid on;
  2 Comments
pfb
pfb on 12 Apr 2015
yes, well put! No need of the index in x. I overlooked that.
The question was about the error with subscript index, though.
Image Analyst
Image Analyst on 12 Apr 2015
If t was just integers, it would have been fine. However, t is this:
t =
Columns 1 through 5
1 1.09090909090909 1.18181818181818 1.27272727272727 1.36363636363636
Look at the second value of t - it's 1.09090909090909. Now there is an x(1) and you can have a second element of the array, x(2), but you cannot have the 1.09'th element of the array. It just doesn't make sense. Of course you can interpolate to get what x would be there, but that's a different question - you'd have to create a new vector with indices 1,2,3,4..... to hold that value.
Does that explain it better? Actually I've just paraphrased what was in the FAQ I referred you to.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!