How to create matrix array using while loop and time increment?

8 ビュー (過去 30 日間)
Kenny Gee
Kenny Gee 2022 年 2 月 16 日
コメント済み: Kenny Gee 2022 年 2 月 16 日
I have a problem where i want to solve a problem where an apple falls from a tree from 3m before hitting the ground. Assuming there is no air resistance, how long will it take to hit the ground? I have to create an array with time increments until the distance becomes 0 at the final time. So first column should show my time and second column will show the distance going down at every increment.
y0=2;
g=-9.81;
t = 0;
i=0;
y1=y0;
a = zeros([],2);
while y1>=0
i=i+1;
t=t+0.10;
y1=y0-0.5*g*t.^2;
a(i,:) = [t,y1]
end
disp(a)
0.0065 2.0726 %sample results
0.0065 2.0789
0.0065 2.0853
0.0065 2.0917
0.0065 2.0981
0.0066 2.1046
0.0066 2.1110
0.0066 2.1174
But when I run my code, it won't stop running and instead of 0.10 increments, the time looks off. How do I use the while loop (must use) to find the time it takes for the final y value to be 0.

採用された回答

DGM
DGM 2022 年 2 月 16 日
編集済み: DGM 2022 年 2 月 16 日
Well, that's what happens when you drop an anti-gravity apple.
y0 = 2;
g = -9.81;
tstep = 0.01;
t = 0;
i = 0;
y1 = y0;
a = zeros([],2);
while y1 >= 0
i = i + 1;
t = t + tstep;
y1 = y0 + 0.5*g*t.^2;
a(i,:) = [t,y1];
end
plot(a(:,1),a(:,2))
  1 件のコメント
Kenny Gee
Kenny Gee 2022 年 2 月 16 日
okay, i see where i made my mistake. sorry about the actual equation. I wanted to learn to do while loop. Thank you :)

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

その他の回答 (1 件)

David Hill
David Hill 2022 年 2 月 16 日
y1=y0+0.5*g*t.^2;%needs to be + (gravity is negative)

カテゴリ

Help Center および File ExchangeProgramming についてさらに検索

製品


リリース

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by