Error with xlim and ylim?

59 visualizaciones (últimos 30 días)
Anne Nguyen
Anne Nguyen el 29 de Sept. de 2019
Comentada: Walter Roberson el 21 de Mzo. de 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 comentarios
Walter Roberson
Walter Roberson el 20 de Mzo. de 2022
no, comma vs space does not matter for that.
Walter Roberson
Walter Roberson el 21 de Mzo. de 2022
That error would occur if the actual code was
xlim(0,3)
instead of
xlim([0,3])

Iniciar sesión para comentar.

Respuestas (1)

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

Iniciar sesión para comentar.

Categorías

Más información sobre Creating and Concatenating Matrices en Help Center y File Exchange.

Etiquetas

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by