将while循环数据保存到vector中

时间:2013-04-24 17:06:49

标签: matlab vector while-loop

我有这个while循环,它运行良好并提供正确的输出数据但是当它结束时它只保存最新的数据点,我如何将每个loopdata保存到一个向量中?

t0=0.15; % Initial time
v0=46.5285; % Initial velocity
h0=3.4896; %Initial height
dt=0.001; % Timesteps/Precision
m=0.05; %Mass
g=9.81; % The gravitational constant

Velocity2=46.5285;

t = t0;
while Velocity2>=-20
Velocity2=hastighet(acceleration(0,m,g),t,v0,t0);
Height2=hojd(acceleration(0,m,g),t,h0,v0,t0);
 t=t+dt;
end

真的很感谢你的帮助!

1 个答案:

答案 0 :(得分:0)

t = t0;
velocityData = [];
heightData = [];
timeData = [];
counter = 1;

while Velocity2>=-20
    Velocity2=hastighet(acceleration(0,m,g),t,v0,t0);
    Height2=hojd(acceleration(0,m,g),t,h0,v0,t0);
    velocityData(counter) = Velocity2;
    heightData(counter) = Height2;
    timeData(counter) = t;
    t=t+dt;
    counter = counter + 1;
end