How to graph dashed reference line at 0?
조회 수: 7 (최근 30일)
이전 댓글 표시
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
채택된 답변
Walter Roberson
2016년 9월 14일
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
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Line Plots에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!