why do I get Array indices must be positive integers or logical values?
4 views (last 30 days)
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.