Subscript indices must either be real positive integers or logicals.

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

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)

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

Thank you so much!!! It helps me alot!
My pleasure!
I wish you had Accepted my Answer, though.
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.

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

yes, well put! No need of the index in x. I overlooked that.
The question was about the error with subscript index, though.
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.

Categories

Asked:

on 12 Apr 2015

Commented:

on 12 Apr 2015

Community Treasure Hunt

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

Start Hunting!