I need to store the values given in a matrix so I can make a graph, but I cant figure it out. Please help! I only want to graph the positive values.
clc
clear
for t = 0:2:100;
h = rocket(t);
if h>=0
disp(h)
end
end
%My function
function [h] = rocket(t)
h = (60+(2.13*(t^2))-(0.0013*(t^4))+(0.000034*(t^4.751)));
end

 採用された回答

Star Strider
Star Strider 2019 年 6 月 21 日

0 投票

If you need to do it with the loop, this works:
t = 0:2:100;
for k = 1:numel(t)
h(k) = rocket(t(k));
if h>=0
disp(h)
end
end
figure
plot(t, h)
Note that you will need to refer to every element of ‘t’ and store every value of ‘h’.
However, using element-wise operations is more efficient:
t = 0:2:100;
h = (60+(2.13*(t.^2))-(0.0013*(t.^4))+(0.000034*(t.^4.751)));
figure
plot(t, h)
See Vectorization (link) for details.

2 件のコメント

Angel Hiram Torres Mota
Angel Hiram Torres Mota 2019 年 6 月 21 日
Perfect, thanks!
Star Strider
Star Strider 2019 年 6 月 21 日
As always, my pleasure!

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

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeLoops and Conditional Statements についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by