MATLAB Answers

Error with xlim and ylim?

29 views (last 30 days)
Anne Nguyen
Anne Nguyen on 29 Sep 2019
Edited: Adam Danz on 29 Sep 2019
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
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
xlabel('Distance (m)');
ylabel('Ball Height (m)');
title('Ball Trajectory');
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."

  1 Comment

Adam Danz
Adam Danz on 29 Sep 2019
That error message doesn't make sense since xlim and ylim are only provided 1 input argument in your code. Please supply the entire copy-pasted error message. Also, just to cover some basics, please share the result of this:
which xlim -all
which ylim -all

Sign in to comment.

Answers (0)


Community Treasure Hunt

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

Start Hunting!