34 views (last 30 days)

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?

Adam
on 27 Jan 2020

Edited: Adam
on 27 Jan 2020

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
on 27 Jan 2020

Edited: Star Strider
on 27 Jan 2020

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.

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

Start Hunting!
## 1 Comment

## Direct link to this comment

https://au.mathworks.com/matlabcentral/answers/502189-why-do-i-get-array-indices-must-be-positive-integers-or-logical-values#comment_790525

⋮## Direct link to this comment

https://au.mathworks.com/matlabcentral/answers/502189-why-do-i-get-array-indices-must-be-positive-integers-or-logical-values#comment_790525

Sign in to comment.