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?

 채택된 답변

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개)

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

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.

댓글을 달려면 로그인하십시오.

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

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.

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Performance and Memory에 대해 자세히 알아보기

질문:

2015년 4월 12일

댓글:

2015년 4월 12일

Community Treasure Hunt

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

Start Hunting!

Translated by