Subscript indices must either be real positive integers or logicals.

2 次查看(过去 30 天)
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?

采纳的回答

pfb
pfb 2015-4-12
编辑:pfb 2015-4-12
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.

更多回答(2 个)

Star Strider
Star Strider 2015-4-12
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 个评论
Image Analyst
Image Analyst 2015-4-12
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.

请先登录,再进行评论。


Image Analyst
Image Analyst 2015-4-12
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 个评论
pfb
pfb 2015-4-12
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 2015-4-12
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.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Logical 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by