Error with xlim and ylim?

조회 수: 39 (최근 30일)
Anne Nguyen
Anne Nguyen 2019년 9월 29일
댓글: Walter Roberson 2022년 3월 21일
Here is the function I wrote:
function [x,y,hitDistance,hitTime] = throwBallFunc(velocity,angle)
h = 1.5; % initial height of ball at release
g = 9.8; % gravitational acceleration
t = linspace(0,20,10000); % Time... 20/10000 = .002 Needed to be inclusive and sequential
x = (velocity.*cos(angle.*pi./180)).*t; % distance vector
y = h+(velocity.*sin(angle.*pi./180)).*t-(1/2)*g.*t.^2; % height vector
index = find(y<0); % finding negative elements in the vector
firstIndex = index(1); % finding the first negative element in the vector
hitDistance = x(firstIndex); % distance where the ball hits the ground
hitTime = t(firstIndex); % time where ball hits the ground
x = x(1:firstIndex-1); % override and return new distance vector
y = y(1:firstIndex-1); % override and return new height vector
end
And here is the script I have that includes the function above:
velocity = input( ' Enter the velocity of ball at release (in m/s) ');
angle = input( ' Enter the angle of vector velocity at time of release (in degrees) ');
[x,y,hitDistance,hitTime] = throwBallFunc(velocity,angle);
fprintf(' The ball hits the ground at a distance of %f meters \n', hitDistance);
fprintf(' The ball hits the ground at %f seconds ', hitTime);
plot(x, zeros(1,length(x)), 'k--')
hold on
figure(1)
plot(x,y);
xlabel('Distance (m)');
ylabel('Ball Height (m)');
title('Ball Trajectory');
xlim([0,3])
ylim([-1,2])
I am getting an error for xlim and ylim. Why is that? I have tried everything and cannot seem to figure it out. It says "When 2 input arguments are specified, the first argument must be an axes."
  댓글 수: 3
Walter Roberson
Walter Roberson 2022년 3월 20일
no, comma vs space does not matter for that.
Walter Roberson
Walter Roberson 2022년 3월 21일
That error would occur if the actual code was
xlim(0,3)
instead of
xlim([0,3])

댓글을 달려면 로그인하십시오.

답변 (1개)

Venkateshh Miryalkar
Venkateshh Miryalkar 2022년 3월 20일
syntax error, xlim([0 3) and ylim([-1 2]) should work.
  댓글 수: 1
Walter Roberson
Walter Roberson 2022년 3월 21일
That is just incorrect. In MATLAB, whitespace (with no operator context) inside [] is treated exactly the same as comma. In fact it is the comma that is the "real" delimiter
[0 3]
is the same as
[0, 3]
is the same as
horzcat(0, 3)
with the horzcat being the real underlying operation. https://www.mathworks.com/help/matlab/ref/horzcat.html
"horzcat is equivalent to using square brackets for horizontally concatenating arrays. For example, [A,B] or [A B] is equal to horzcat(A,B) when A and B are compatible arrays."

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

태그

제품

Community Treasure Hunt

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

Start Hunting!

Translated by