plotting for every single loop

1 Ansicht (letzte 30 Tage)
Ali Roshanzamir
Ali Roshanzamir am 5 Aug. 2020
Kommentiert: Ali Roshanzamir am 6 Aug. 2020
Hi everyone.
I got this code and I have a question about it. Is it work for every single "v"? like 1, 2 ,3 ,4 .......
If not how can I make it for every "v" ?
N=45;
teta=0:0.02:5*pi;
y=zeros(10,length(teta));
x=zeros(size(teta));
for v=1:2:30
x=x+(0.085*N/pi)*sin(v*teta)/v;
y((v+1)/2,:)=x;
end
plot(teta,x)

Akzeptierte Antwort

Walter Roberson
Walter Roberson am 5 Aug. 2020
for v=1:2:30
means that the loop should execute for 1, 1+2, 1+2+2, 1+2+2+2, 1+2+2+2+2 and so on -- which is to say, 1, 3, 5, 7, 9, and so on.
y((v+1)/2,:)=x;
Because your v values are presently odd integers, then v+1 becomes the next even integer, and /2 will be an integer. So y(1) then y(2), y(3) and so on will be assigned to.
If you were to switch to
for v=1:30
then you would be including even v such as 2, and (2+1)/2 would not be an integer, and you would not be able to use (v+1)/2 as a subscript.
  3 Kommentare
Walter Roberson
Walter Roberson am 5 Aug. 2020
Yes. If you look more carefully at your code,
for v=1:2:30
x=x+(0.085*N/pi)*sin(v*teta)/v;
y((v+1)/2,:)=x;
end
plot(teta,x)
You are plotting x, not y. So you do not need to record y: you could simplify the code to
for v=1:2:30
x=x+(0.085*N/pi)*sin(v*teta)/v;
end
plot(teta,x)
and then change to
for v=1:30
The indexing at a non-integer stops being a problem if you stop indexing that way ;-)
Ali Roshanzamir
Ali Roshanzamir am 6 Aug. 2020
Yes, I see. thank you so much.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by