Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

To resolve issues starting MATLAB on Mac OS X 10.10 (Yosemite) visit: http://www.mathworks.com/matlabcentral/answers/159016

plotting results of an while loop

Asked by Fabian Höfler on 9 Feb 2012

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

0 Comments

Fabian Höfler

Tags

Products

2 Answers

Answer by Sean de Wolski on 9 Feb 2012

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

0 Comments

Sean de Wolski
Answer by Fabian Höfler on 9 Feb 2012

Yeah! It's running thanks a lot.

0 Comments

Fabian Höfler

Contact us