How to plot this line and specified point

1 visualización (últimos 30 días)
RetiredCheetoXI
RetiredCheetoXI el 4 de Feb. de 2022
Comentada: Davide Masiello el 4 de Feb. de 2022
In addition to the point on the graph that shows up, I'd like to also have the line of the equation show up on the graph as well. I have tried using hold on and hold off and a couple other ways, but I cannot figure it out
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 < 117.5)
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

Respuesta aceptada

Davide Masiello
Davide Masiello el 4 de Feb. de 2022
prompt = 'Time of flight? (s) ';
t = input(prompt);
tx = t-5:t+5;
if (t <= 45)
h = @(x) 15*x.^2;
fprintf('Altitude: %dm\n\n', h(t))
v = @(x) 30*x;
fprintf('Velocity: %dm/s\n\n', v(t));
figure(1)
subplot(2,1,1)
plot(t,h(t),'o',tx,h(tx)), xlabel('Time (s)'), ylabel('Altitude (m)'), title('Altitude vs Time')
subplot(2,1,2)
plot(t,v(t),'o',tx,v(tx)), xlabel('Time (s)'), ylabel('Velocity (m/s)'), title('Velocity vs Time')
elseif (t < 117.5)
h = @(x) 30375 + (1350*x) + 0.5*(-11.5)*x.^2;
fprintf('Altitude: %dm\n\n', h(t))
v = @(x) 1350 - 11.5*x;
fprintf('Velocity: %dm/s\n\n', v(t));
figure(2)
subplot(2,1,1)
plot(t,h(t),'o',tx,h(tx)), xlabel('Time (s)'), ylabel('Altitude (m)'), title('Altitude vs Time')
subplot(2,1,2)
plot(t,v(t),'o',tx,v(tx)), xlabel('Time (s)'), ylabel('Velocity (m/s)'), title('Velocity vs Time')
end
  2 comentarios
RetiredCheetoXI
RetiredCheetoXI el 4 de Feb. de 2022
Awesome! Thanks for your help. Would you be able to explain what you did on line 3?
Davide Masiello
Davide Masiello el 4 de Feb. de 2022
I just created a vector of times that spans around your input time.
If the code works and you are satisfied with the answer, please accept it. I will reply to any further questions anyways.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre 2-D and 3-D Plots en Help Center y File Exchange.

Etiquetas

Productos


Versión

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by