Borrar filtros
Borrar filtros

How to graph dashed reference line at 0?

5 visualizaciones (últimos 30 días)
Jake Marvin
Jake Marvin el 14 de Sept. de 2016
Respondida: Walter Roberson el 14 de Sept. de 2016
I can't figure out how to graph a dashed line at 0 through the end of the graph (which would represent the ground). Currently the script I have is:
clear
clc
h=1.5; %m, height of ball at release
g=9.8; %m/s/s, gravity
%ask users for v and angle inputs
v=input('Enter velocity of ball at release:');
theta=input('Enter angle of ball at release:');
t=linspace(0,1,1000);%interval for 1 sec
x=v*cos(theta*(pi/180))*t; %formula for distance
y=h+(v*(sin(theta*(pi/180))))*t-(.5*g*t.^2); %formula for when height=0
x1=find(y<0);%assigns x1 the value of the vector list for when y<0
distance=x(x1(1)); %calculates x at when y=0
%Display answer to user
fprintf('The ball hits the ground at a distance of %d meters.\n', distance)
xlabel('Distance travelled (m)')
ylabel('Height travelled (m)')
title('Trajectory of Ball')
plot(x,y)
hold on
plot([0,0],[v,0], '--')

Respuesta aceptada

Walter Roberson
Walter Roberson el 14 de Sept. de 2016
xL = get(gca, 'XLim');
plot(xL, [0 0], '--')
Remember, the ground is a height and height is Y rather than X.
The bit about getting the XLim is finding the left and right margins of what you currently have plotted; then the plot command plots a line across from the left to right margin.

Más respuestas (0)

Categorías

Más información sobre Line Plots en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by