Error with xlim and ylim?

32 views (last 30 days)
Anne Nguyen
Anne Nguyen on 29 Sep 2019
Commented: Walter Roberson on 21 Mar 2022
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 Comments
Walter Roberson
Walter Roberson on 21 Mar 2022
That error would occur if the actual code was
xlim(0,3)
instead of
xlim([0,3])

Sign in to comment.

Answers (1)

Venkateshh Miryalkar
Venkateshh Miryalkar on 20 Mar 2022
syntax error, xlim([0 3) and ylim([-1 2]) should work.
  1 Comment
Walter Roberson
Walter Roberson on 21 Mar 2022
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."

Sign in to comment.

Products

Community Treasure Hunt

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

Start Hunting!