Error " index must be a positive integer or logical "

1 view (last 30 days)
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: Screenshot_3.jpg

Answers (1)

Walter Roberson
Walter Roberson on 27 Mar 2019
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
Stephen23
Stephen23 on 28 Mar 2019
Edited: Stephen23 on 28 Mar 2019
"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:
  1. collect the data into vectors using indexing,
  2. plot the vectors after the loop.
Cebotari Alexandru
Cebotari Alexandru on 28 Mar 2019
Edited: Cebotari Alexandru on 28 Mar 2019
sorry, but I am a beginner in programming. If you really want to help, just send code that works.
Last modification: I moved plot outside of while, like the last codeline. You can see the plot in attachement (sure, totally wrong). Also, I have to mention that values of those doubles 13x13 betaii and Ri are good, they matches calculations I've made by hand. Then, why plot is wrong?

Sign in to comment.

Categories

Find more on Historical Contests 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!