Error " index must be a positive integer or logical "
Show older comments
Hello all. My code is quite simple, but I dont get why there's a problem with the index " i ". Can you help me?
R1=47
R2=86
w1=15.18
w2=3.9
cm1=4.41
cm2=2.81
n=13
R=3
z=9
x=1
ni=13
v=0
while x<=z
i=0
Ri=R1
while i<ni
cmi=cm1-i((cm1-cm2)/ni);
wi=w1-i(w1-w2)/ni;
i=i+1
R1=R1+R
betai=asind(cmi/wi);
betaii=deg2rad(betai)+v
polar(betaii,Ri); grid on; hold on;
end
v=v+2*pi/z
x=x+1
end
and the error is: 

Answers (1)
Walter Roberson
on 27 Mar 2019
1 vote
That image of an error message does not agree with the code.
You have i((cm1-cm2)/ni) . MATLAB does not have implicit multiplication, so that is a request to index i at location (cm1-cm2)/ni . You probably want i*((cm1-cm2)/ni) and a similar fix on the next line.
There is no point in using deg2rad(betai) in that context when you could simply have used asin instead of asind and so received the answer in radians directly.
8 Comments
Cebotari Alexandru
on 28 Mar 2019
Edited: Cebotari Alexandru
on 28 Mar 2019
Walter Roberson
on 28 Mar 2019
polar() does the same thing as plot(): if you do not tell it to use a marker, then the only time it draws anything is if there are at least two consecutive finite values to plot in any one call. Your betaii and Ri are scalar when you make the polar() call, so there is not at least two consecutive finite values in the one call, so nothing visible will get drawn. If you were to change the '-k' to include a plot symbol such as '-kv' then you would see things being drawn.
Chances are that you want a single continuous line drawn. To do that, you should record the betaii and Ri values into vectors instead of plotting them immediately, and then after the loop you should polar() the entire vectors.
Cebotari Alexandru
on 28 Mar 2019
Walter Roberson
on 28 Mar 2019
betaii(i) = betai+v;
Ri(i) = Ri(end) + R;
and postpone the polar() call.
Cebotari Alexandru
on 28 Mar 2019
Edited: Cebotari Alexandru
on 28 Mar 2019
Walter Roberson
on 28 Mar 2019
polar(betaii, Ri, '-k')
"the plot is empty again"
The plot is not really empty. On each loop iteration you plot exactly one data point. Because there is only one data point and you did not specify any marker, then there is no line and no marker, and you will not be able to see anything. But if you look in the axes children you will find that data point is right there, in the middle of the plot.
Then on the next iteration you replace that point with a new point. And so on, until finally you have plotted and deleted each point, leaving only one point (with no marker and no line) on the final plot.
To plot the data you will need to make two main changes to your code:
- collect the data into vectors using indexing,
- plot the vectors after the loop.
Cebotari Alexandru
on 28 Mar 2019
Edited: Cebotari Alexandru
on 28 Mar 2019
Categories
Find more on Mathematics in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!