Error " index must be a positive integer or logical "
1 visualización (últimos 30 días)
Mostrar comentarios más antiguos
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: 

0 comentarios
Respuestas (1)
Walter Roberson
el 27 de Mzo. de 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 comentarios
Stephen23
el 28 de Mzo. de 2019
Editada: Stephen23
el 28 de Mzo. de 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:
- collect the data into vectors using indexing,
- plot the vectors after the loop.
Ver también
Categorías
Más información sobre Historical Contests en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!