Error in ball throwing function (Too many output arguments)
1 view (last 30 days)
Show older comments
I Have writen a function that gives the distance a ball will travel given a velocity and angle.
After this i am trying to graph the distance for a range of angles but i keep running into the same error:
Error using DTask1_f
Too many output arguments.
Error in DTask1_f (line 20)
distance(i) = DTask1_f(v,theta(i));
Here is my current function i have written, can sombody help me fix this error?
function DTask1_f(v, theta)
h0 = 1.8; %Inital height
A = 9.8; %Acceleration
t = linspace(0,10,100);
x = v.*cos(theta.*(pi./180)).*t;
y = h0+(v*(sin(theta*(pi/180))))*t-(0.5*A*t.^2);
n = find(y<0);
if isempty(n) == 1
disp('The ball does not hit the ground in 10 seconds')
d = NaN;
elseif isempty(n) == 0
d = x(n(1));
end
fprintf('The ball hits the ground at a distance of %1.4f meters.\n',d)
v = 60;
theta = 1:1:60;
distance = zeros(1,61);
for i = 1:60
distance(i) = DTask1_f(v,theta(i));
end
figure
plot(theta,distance);
xlabel('Initial angle (deg)');
ylabel('Distance thrown (m)');
title('Distance of ball thrown as a function of release angle');
legend('v= 60 m/s')
end
0 Comments
Answers (1)
Walter Roberson
on 10 Sep 2021
function DTask1_f(v, theta)
That function declaration indicates that the function does not return any values. If it returned a value, there would have to be at least one variable named on the left side, such as
function h0 = DTask1_f(v, theta)
if it were the value of h0 that you wanted to return.
2 Comments
Walter Roberson
on 10 Sep 2021
v = 60;
theta = 1:1:60;
distance = zeros(1,60);
for i = 1:60
distance(i) = DTask1_f(v,theta(i));
end
figure
plot(theta,distance);
xlabel('Initial angle (deg)');
ylabel('Distance thrown (m)');
title('Distance of ball thrown as a function of release angle');
legend('v= 60 m/s')
function d = DTask1_f(v, theta)
h0 = 1.8; %Inital height
A = 9.8; %Acceleration
t = linspace(0,10,100);
x = v.*cos(theta.*(pi./180)).*t;
y = h0+(v*(sin(theta*(pi/180))))*t-(0.5*A*t.^2);
n = find(y<0);
if isempty(n) == 1
disp('The ball does not hit the ground in 10 seconds')
d = NaN;
elseif isempty(n) == 0
d = x(n(1));
end
fprintf('The ball hits the ground at a distance of %1.4f meters.\n',d)
end
See Also
Categories
Find more on 2-D and 3-D Plots in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!