why do I get Array indices must be positive integers or logical values?

1 回表示 (過去 30 日間)
Ahmed Alhawaj
Ahmed Alhawaj 2020 年 1 月 27 日
編集済み: Star Strider 2020 年 1 月 27 日
1-t=linspace(0,0.2);
2-wn=104;
3-uo=1;
4-zeta1=0.1;
5-zeta2=0.3;
6-zeta3=0.9;
7-wd1=wn*(sqrt(1-(zeta1^2)));
8-wd2=wn*(sqrt(1-(zeta2^2)));
9-wd3=wn*(sqrt(1-(zeta3^2)));
10-c1=exp(-zeta1*wn*t);
11-c2=exp(-zeta2*wn*t);
12-c3=exp(-zeta3*wn*t);
13-z=(cos(wd1*t));
14-y1=zeta1/(1-(zeta1^2));
15-y2=zeta2/(1-(zeta2^2));
16-y3=zeta3/(1-(zeta3^2));
17-x1(t)=uo*(1-c1.*(cos(wd1*t)-(y1*sin(wd1*t))));
18-x2(t)=uo*(1-c2.*(cos(wd2*t)-y2*sin(wd2*t)));
19-x3(t)=uo*(1-c3.*(cos(wd3*t)-y3*sin(wd3*t)));
After i run the code provided up , it shows to me in line 17 the Array indices must be positive integers or logical values
What does that mean ?
How do i fix the problem?
  1 件のコメント
Rik
Rik 2020 年 1 月 27 日
編集済み: Rik 2020 年 1 月 27 日
Please remove the line numbers and format your code properly.
The error indicates you are trying to use a value as an array index. sin(x) is a function call if sin is a function, but sin(x) is an indexing operation if sin was assigned a value.
You should check if somewhere in your previous code you assigned a value to sin, which made it a variable. (cos seems fine, as line 13 doesn't seem to cause a similar error)

サインインしてコメントする。

回答 (2 件)

Adam
Adam 2020 年 1 月 27 日
編集済み: Adam 2020 年 1 月 27 日
It means pretty much exactly what it says. In Matlab x1, as you create it, is an array of values representing your function at defined points. Those values are indexed as 1, 2, 3, 4, 5,..., not as the true t values of your equation. However, you can use vectorisation to assign all values at once:
x1 = uo*(1-c1.*(cos(wd1*t)-(y1*sin(wd1*t))));
Just like you do with c1, which also works with a full vector of t values.
Notice you are already using .* for your multiplication, which is what is needed for vectorised maths here since c1 and the quantity it is being multiplied by are both vectors, so this gives you element-wise multiplication and will assign all the results into x1, which will be the same length as t at the end, and that provides your link between t and x, by indexing into each.

Star Strider
Star Strider 2020 年 1 月 27 日
編集済み: Star Strider 2020 年 1 月 27 日
The way you have defined ‘t’, it is not an integer, and the code interprets it as a subscript in lines 17-19.
One way to fix it would be to create ‘x’ as a function, then assign the ‘x’ values appropriately with calls to it:
xfcn = @(wd,cc,yy,uu,t) uu*(1-cc.*(cos(wd*t)-(yy*sin(wd*t))));
x1 = xfcn(wd1,c1,y1,uo,t);
x2 = xfcn(wd2,c2,y2,uo,t);
x3 = xfcn(wd3,c3,y3,uo,t);
I believe that covers all the options.
EDIT — Corrected typographical errors.

カテゴリ

Help Center および File ExchangeCreating and Concatenating Matrices についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by