Subscript indices must either be real positive integers or logicals.
3 ビュー (過去 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?
0 件のコメント
採用された回答
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.
0 件のコメント
その他の回答 (2 件)
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
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
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;
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/176014/image.png)
2 件のコメント
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
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 Center および File Exchange で Matrix Indexing についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!