plotting results of an while loop

조회 수: 7 (최근 30일)
Fabian Höfler
Fabian Höfler 2012년 2월 9일
댓글: VBBV 2023년 1월 23일
Hi I have to plot the speed of a Basejumper in dependence of the time. For this I'm using a while loop with the plot command underneath. That's my code:
while s>0%
a=g+k/m*v.^2;
deltav=a*deltat;
v=v+deltav
deltas=v*deltat;
s=s+deltas;
t=t+deltat;
end
plot(t,v)
hold on
sprintf('Der Flug dauerte %5.3f Sekunden',t)
sprintf('Die Geschwindigkeit beim Aufschlag betrug %5.3f m/s',v)
Unfortunately Matlab shows only one point in the diagram not the whole graph. Does anybody knows what I did wrong?
Thanks Fabian

답변 (2개)

Sean de Wolski
Sean de Wolski 2012년 2월 9일
You overwrite t,v on each iteration of the for-loop. Instead you need to either plot them then or store them to plot later.
count = 0;
while ...
count = count+1;
v(count) = v(count-1)+deltav;
end
or plot as you're going;
hold on
while ...
%stuff
plot(t,v)
end
  댓글 수: 2
BHAGYASHREE WAGHULE
BHAGYASHREE WAGHULE 2018년 10월 4일
Hello Sean, I tried your first suggestion of using count, but it gives an error saying the subscript indices must either be real positive integers or logicals.
VBBV
VBBV 2023년 1월 23일
ok, i think it will give error, because Matlab uses 1 based indexing,
while ...
count = count+1;
v(count+1) = v(count)+deltav; % use this instead, (count-1) = 0 ; Matlab
end
e.g.
S = 1:10;
S(0)
Array indices must be positive integers or logical values.

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


Fabian Höfler
Fabian Höfler 2012년 2월 9일
Yeah! It's running thanks a lot.

카테고리

Help CenterFile Exchange에서 Graphics Performance에 대해 자세히 알아보기

태그

제품

Community Treasure Hunt

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

Start Hunting!

Translated by