Storing while loop results in an array and plotting

Hello, I have this simple code simulating free fall. The while loop works fine in the first case but i need to plot the results. I tried to put the y and v values in arrays but the results i get are wrong. How do i fix that?
clear, clc, close all, format long
% Initial values and constants
g = 10;
y = 100;
v = 0;
t = 0;
h = 0.1;
fprintf('\n\tt (sec)\t\ty (m)\t\tv (m/sec)\n')
t (sec) y (m) v (m/sec)
fprintf('\n\t%.2f\t\t%.2f\t\t%.2f\n',t,y,v)
0.00 100.00 0.00
while y >= 0
y = y + h*v;
v = v - h*g;
t = t + h;
fprintf('\n\t%.2f\t\t%.2f\t\t%.2f\n',t,y,v)
end
0.10 100.00 -1.00 0.20 99.90 -2.00 0.30 99.70 -3.00 0.40 99.40 -4.00 0.50 99.00 -5.00 0.60 98.50 -6.00 0.70 97.90 -7.00 0.80 97.20 -8.00 0.90 96.40 -9.00 1.00 95.50 -10.00 1.10 94.50 -11.00 1.20 93.40 -12.00 1.30 92.20 -13.00 1.40 90.90 -14.00 1.50 89.50 -15.00 1.60 88.00 -16.00 1.70 86.40 -17.00 1.80 84.70 -18.00 1.90 82.90 -19.00 2.00 81.00 -20.00 2.10 79.00 -21.00 2.20 76.90 -22.00 2.30 74.70 -23.00 2.40 72.40 -24.00 2.50 70.00 -25.00 2.60 67.50 -26.00 2.70 64.90 -27.00 2.80 62.20 -28.00 2.90 59.40 -29.00 3.00 56.50 -30.00 3.10 53.50 -31.00 3.20 50.40 -32.00 3.30 47.20 -33.00 3.40 43.90 -34.00 3.50 40.50 -35.00 3.60 37.00 -36.00 3.70 33.40 -37.00 3.80 29.70 -38.00 3.90 25.90 -39.00 4.00 22.00 -40.00 4.10 18.00 -41.00 4.20 13.90 -42.00 4.30 9.70 -43.00 4.40 5.40 -44.00 4.50 1.00 -45.00 4.60 -3.50 -46.00
subplot(2,1,1), plot(t,y), grid on
subplot(2,1,2), plot(t,v), grid on
% Alternative way - putting values in array
% Initial values and constants
g = 10;
i = 1;
y(i) = 100;
v(i) = 0;
t(i) = 0;
h = 0.1;
fprintf('\n\tt (sec)\t\ty (m)\t\tv (m/sec)\n')
t (sec) y (m) v (m/sec)
fprintf('\n\t%.2f\t\t%.2f\t\t%.2f\n',t,y,v)
0.00 100.00 0.00
while y >= 0
y(i+1) = y(i) + h*v(i);
v(i+1) = v(i) - h*g;
t(i+1) = t(i) + h;
i = i + 1;
end
fprintf('\n\t%.2f\t\t%.2f\t\t%.2f\n',t,y,v)
0.00 0.10 0.20 0.30 0.40 0.50 0.60 0.70 0.80 0.90 1.00 1.10 1.20 1.30 1.40 1.50 1.60 1.70 1.80 1.90 2.00 2.10 2.20 2.30 2.40 2.50 2.60 2.70 2.80 2.90 3.00 3.10 3.20 3.30 3.40 3.50 3.60 3.70 3.80 3.90 4.00 4.10 4.20 4.30 4.40 4.50 4.60 100.00 100.00 99.90 99.70 99.40 99.00 98.50 97.90 97.20 96.40 95.50 94.50 93.40 92.20 90.90 89.50 88.00 86.40 84.70 82.90 81.00 79.00 76.90 74.70 72.40 70.00 67.50 64.90 62.20 59.40 56.50 53.50 50.40 47.20 43.90 40.50 37.00 33.40 29.70 25.90 22.00 18.00 13.90 9.70 5.40 1.00 -3.50 0.00 -1.00 -2.00 -3.00 -4.00 -5.00 -6.00 -7.00 -8.00 -9.00 -10.00 -11.00 -12.00 -13.00 -14.00 -15.00 -16.00 -17.00 -18.00 -19.00 -20.00 -21.00 -22.00 -23.00 -24.00 -25.00 -26.00 -27.00 -28.00 -29.00 -30.00 -31.00 -32.00 -33.00 -34.00 -35.00 -36.00 -37.00 -38.00 -39.00 -40.00 -41.00 -42.00 -43.00 -44.00 -45.00 -46.00
subplot(2,1,1), plot(t,y), grid on
subplot(2,1,2), plot(t,v), grid on

 採用された回答

Torsten
Torsten 2025 年 1 月 5 日
編集済み: Torsten 2025 年 1 月 5 日

0 投票

Why do you think the results you get from the second part of your code are wrong ?
g = 10;
y(1) = 100;
v(1) = 0;
t(1) = 0;
h = 0.1;
i = 1;
while y(i) >= 0
i = i + 1;
y(i) = y(i-1) + h*v(i-1);
v(i) = v(i-1) - h*g;
t(i) = t(i-1) + h;
fprintf('\n\t%.2f\t\t%.2f\t\t%.2f\n',t(i),y(i),v(i))
end
0.10 100.00 -1.00 0.20 99.90 -2.00 0.30 99.70 -3.00 0.40 99.40 -4.00 0.50 99.00 -5.00 0.60 98.50 -6.00 0.70 97.90 -7.00 0.80 97.20 -8.00 0.90 96.40 -9.00 1.00 95.50 -10.00 1.10 94.50 -11.00 1.20 93.40 -12.00 1.30 92.20 -13.00 1.40 90.90 -14.00 1.50 89.50 -15.00 1.60 88.00 -16.00 1.70 86.40 -17.00 1.80 84.70 -18.00 1.90 82.90 -19.00 2.00 81.00 -20.00 2.10 79.00 -21.00 2.20 76.90 -22.00 2.30 74.70 -23.00 2.40 72.40 -24.00 2.50 70.00 -25.00 2.60 67.50 -26.00 2.70 64.90 -27.00 2.80 62.20 -28.00 2.90 59.40 -29.00 3.00 56.50 -30.00 3.10 53.50 -31.00 3.20 50.40 -32.00 3.30 47.20 -33.00 3.40 43.90 -34.00 3.50 40.50 -35.00 3.60 37.00 -36.00 3.70 33.40 -37.00 3.80 29.70 -38.00 3.90 25.90 -39.00 4.00 22.00 -40.00 4.10 18.00 -41.00 4.20 13.90 -42.00 4.30 9.70 -43.00 4.40 5.40 -44.00 4.50 1.00 -45.00 4.60 -3.50 -46.00
y = y(1:end-1);
v = v(1:end-1);
t = t(1:end-1);
subplot(2,1,1), plot(t,y), grid on
subplot(2,1,2), plot(t,v), grid on

1 件のコメント

Torsten
Torsten 2025 年 1 月 5 日
編集済み: Torsten 2025 年 1 月 5 日
The below code linearly interpolates the time point t0 when y becomes 0 between the last two values for y, namely (t(end-1),y(end-1)) and (t(end),y(end)) with y(end-1) > 0 and y(end) <= 0:
g = 10;
y(1) = 100;
v(1) = 0;
t(1) = 0;
h = 0.1;
i = 1;
while y(i) > 0
i = i + 1;
y(i) = y(i-1) + h*v(i-1);
v(i) = v(i-1) - h*g;
t(i) = t(i-1) + h;
fprintf('\n\t%.2f\t\t%.2f\t\t%.2f\n',t(i),y(i),v(i))
end
0.10 100.00 -1.00 0.20 99.90 -2.00 0.30 99.70 -3.00 0.40 99.40 -4.00 0.50 99.00 -5.00 0.60 98.50 -6.00 0.70 97.90 -7.00 0.80 97.20 -8.00 0.90 96.40 -9.00 1.00 95.50 -10.00 1.10 94.50 -11.00 1.20 93.40 -12.00 1.30 92.20 -13.00 1.40 90.90 -14.00 1.50 89.50 -15.00 1.60 88.00 -16.00 1.70 86.40 -17.00 1.80 84.70 -18.00 1.90 82.90 -19.00 2.00 81.00 -20.00 2.10 79.00 -21.00 2.20 76.90 -22.00 2.30 74.70 -23.00 2.40 72.40 -24.00 2.50 70.00 -25.00 2.60 67.50 -26.00 2.70 64.90 -27.00 2.80 62.20 -28.00 2.90 59.40 -29.00 3.00 56.50 -30.00 3.10 53.50 -31.00 3.20 50.40 -32.00 3.30 47.20 -33.00 3.40 43.90 -34.00 3.50 40.50 -35.00 3.60 37.00 -36.00 3.70 33.40 -37.00 3.80 29.70 -38.00 3.90 25.90 -39.00 4.00 22.00 -40.00 4.10 18.00 -41.00 4.20 13.90 -42.00 4.30 9.70 -43.00 4.40 5.40 -44.00 4.50 1.00 -45.00 4.60 -3.50 -46.00
t0 = (y(end)*t(end-1)-y(end-1)*t(end))/(y(end)-y(end-1));
t0 - t(end-1)
ans = 0.0222
y(end) = 0;
v(end) = v(end-1) - (t0-t(end-1))*g;
t(end) = t0;
subplot(2,1,1), plot(t,y), grid on
subplot(2,1,2), plot(t,v), grid on

サインインしてコメントする。

その他の回答 (0 件)

製品

リリース

R2016a

質問済み:

2025 年 1 月 5 日

編集済み:

2025 年 1 月 5 日

Community Treasure Hunt

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

Start Hunting!

Translated by