2D line graph plotting from calculations

while s >= stop_height
t = sqrt(2*h/g)
s = u*t + 0.5*a*t*t;
bounce_height = COR*COR*h
h = bounce_height;
end
plot(bounce_height,t)
I am very new to matlab, I understand how you plot when you can choose the data points but am really unsure on how i can plot the above while loop onto a 2d line graph. PS all variabled declared earlier and werent important so I didnt include them. Any help would be amazing. Thanks in advance

回答 (2 件)

Cris LaPierre
Cris LaPierre 2021 年 4 月 13 日

0 投票

Right now, each loop replaces the previous variable values with the new values. Insteach, you need to capture the result of each loop, building up a vector. Once the loop ends, you would plot the vector.
This approach adds the new values to the bottom of a vector.
bounce_height = [];
T = [];
while s >= stop_height
t = sqrt(2*h/g);
s = u*t + 0.5*a*t*t;
h = COR*COR*h;
bounce_height = [bounce_height;h];
T = [T;t];
end
plot(bounce_height,T)

2 件のコメント

Dean Morris
Dean Morris 2021 年 4 月 13 日
Thank you for your reply, could you also tell me how i can change the axis data cause when i try to do it i get an error saying.
Error using plot
Vectors must be the same length.
This is the code that made that error
bounce_height = [0:5:50]
Cris LaPierre
Cris LaPierre 2021 年 4 月 13 日
Don't fill in the vectors. They are intentionally empty. This creates the variables so that, in the first loop, you can append the new values.

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

Star Strider
Star Strider 2021 年 4 月 13 日

0 投票

There are too many missing variables for me to run the code, however consider doing something like this:
k = 1; % Counter Variable
while s >= stop_height
t = sqrt(2*h/g)
s = u*t + 0.5*a*t*t;
bounce_height = COR*COR*h
h = bounce_height;
hv(k) = h;
tv(k) = t;
k = k + 1;
end
figure
plot(hv, tv)
grid
That sortes the results in two variable vectors to plot later.

カテゴリ

ヘルプ センター および File Exchange2-D and 3-D Plots についてさらに検索

製品

リリース

R2020b

タグ

質問済み:

2021 年 4 月 13 日

コメント済み:

2021 年 4 月 13 日

Community Treasure Hunt

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

Start Hunting!

Translated by