Error " index must be a positive integer or logical "

조회 수: 1 (최근 30일)
Cebotari Alexandru
Cebotari Alexandru 2019년 3월 27일
편집: Cebotari Alexandru 2019년 3월 28일
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

답변 (1개)

Walter Roberson
Walter Roberson 2019년 3월 27일
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
Stephen23
Stephen23 2019년 3월 28일
편집: Stephen23 2019년 3월 28일
"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 2019년 3월 28일
편집: Cebotari Alexandru 2019년 3월 28일
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?

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Historical Contests에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by