Error with xlim and ylim?

36 次查看(过去 30 天)
Anne Nguyen
Anne Nguyen 2019-9-29
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
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