How to graph dashed reference line at 0?
5 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Jake Marvin
le 14 Sep 2016
Réponse apportée : Walter Roberson
le 14 Sep 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], '--')
0 commentaires
Réponse acceptée
Walter Roberson
le 14 Sep 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.
0 commentaires
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Line Plots dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!