フィルターのクリア

How to plot two equations on two different plots

2 ビュー (過去 30 日間)
RetiredCheetoXI
RetiredCheetoXI 2022 年 2 月 4 日
編集済み: Arif Hoq 2022 年 2 月 4 日
I need to plot Altitude vs Time and Velocity vs Time. But there are two possible equations for each graph, depending on the value of "t". How do I get both of the correct equations to show up on two different graphs?
prompt = 'Time of flight? (s) ';
t = input(prompt)
if (t <= 45)
h = 15*t.^2;
fprintf('Altitude: %dm\n\n', h)
v = 30*t;
fprintf('Velocity: %dm/s\n\n', v);
plot(t, h), xlabel('Time (s)'), ylabel('Altitude (m)'), title('Altitude vs Time')
plot(t, v), xlabel('Time (s)'), ylabel('Velocity (m/s)'), title('Velocity vs Time')
elseif (t > 45)
h = 30375 + (1350*t) + 0.5*(-11.5)*t.^2;
fprintf('Altitude: %dm\n\n', h )
v = 1350 - 11.5*t;
fprintf('Velocity: %dm/s\n\n', v);
plot(t, h), xlabel('Time (s)'), ylabel('Altitude (m)'), title('Altitude vs Time')
plot(t, v), xlabel('Time (s)'), ylabel('Velocity (m/s)'), title('Velocity vs Time')
end

回答 (1 件)

Arif Hoq
Arif Hoq 2022 年 2 月 4 日
編集済み: Arif Hoq 2022 年 2 月 4 日
try this code.
as your output is only a single integer so it's better specify Line style. here i used 'o'. for different plots use subplot.
prompt = 'Time of flight? (s) ';
t = input(prompt)
if (t <= 45)
h = 15*t.^2;
fprintf('Altitude: %dm\n\n', h)
v = 30*t;
fprintf('Velocity: %dm/s\n\n', v);
figure(1)
subplot(2,1,1)
plot(t, h,'o'), xlabel('Time (s)'), ylabel('Altitude (m)'), title('Altitude vs Time')
subplot(2,1,2)
plot(t, v,'o'), xlabel('Time (s)'), ylabel('Velocity (m/s)'), title('Velocity vs Time')
elseif (t > 45)
h = 30375 + (1350*t) + 0.5*(-11.5)*t.^2;
fprintf('Altitude: %dm\n\n', h )
v = 1350 - 11.5*t;
fprintf('Velocity: %dm/s\n\n', v);
figure(2)
subplot(2,1,1)
plot(t, h,'o'), xlabel('Time (s)'), ylabel('Altitude (m)'), title('Altitude vs Time')
subplot(2,1,2)
plot(t, v,'o'), xlabel('Time (s)'), ylabel('Velocity (m/s)'), title('Velocity vs Time')
end
  2 件のコメント
RetiredCheetoXI
RetiredCheetoXI 2022 年 2 月 4 日
Thanks! Do you know of any easy way to make it also display the equation used in addition to the point?
Arif Hoq
Arif Hoq 2022 年 2 月 4 日
編集済み: Arif Hoq 2022 年 2 月 4 日
Follow this code:
prompt = 'Time of flight? (s) ';
t = input(prompt)
if t <= 45
h = 15*t.^2;
fprintf('Altitude: %dm\n\n', h)
v = 30*t;
fprintf('Velocity: %dm/s\n\n', v);
figure(1)
subplot(2,1,1)
plot(t, h,'o'), xlabel('Time (s)'), ylabel('Altitude (m)'), title('Altitude vs Time')
equation = sprintf('h = 15*t^2');
text(t, h, equation, 'FontSize', 12, 'Color', 'r','HorizontalAlignment','center','VerticalAlignment','top');
subplot(2,1,2)
plot(t, v,'o'), xlabel('Time (s)'), ylabel('Velocity (m/s)'), title('Velocity vs Time')
equation = sprintf('v = 30*t');
text(t, v, equation, 'FontSize', 12, 'Color', 'r', 'HorizontalAlignment','center','VerticalAlignment','top');
elseif t > 45
h = 30375 + (1350*t) + 0.5*(-11.5)*t.^2;
fprintf('Altitude: %dm\n\n', h )
v = 1350 - 11.5*t;
fprintf('Velocity: %dm/s\n\n', v);
figure(2)
subplot(2,1,1)
plot(t, h,'o'), xlabel('Time (s)'), ylabel('Altitude (m)'), title('Altitude vs Time')
equation = sprintf('h = 30375 + (1350*t) + 0.5*(-11.5)*t^2');
text(t, h, equation, 'FontSize', 12, 'Color', 'r', 'HorizontalAlignment','center','VerticalAlignment','top');
subplot(2,1,2)
plot(t, v,'o'), xlabel('Time (s)'), ylabel('Velocity (m/s)'), title('Velocity vs Time')
equation = sprintf('v = 1350 - 11.5*t');
text(t, v, equation, 'FontSize', 12, 'Color', 'r', 'HorizontalAlignment','center','VerticalAlignment','top');
end

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

カテゴリ

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

タグ

製品


リリース

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by