Error in ball throwing function (Too many output arguments)

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

Answers (1)

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

Doing this causes another error, there is an infinite loop in the command, Any idea how to fix it?
v = 60;
theta = 1:1:60;
distance = zeros(1,60);
for i = 1:60
distance(i) = DTask1_f(v,theta(i));
end
The ball hits the ground at a distance of 48.4775 meters. The ball hits the ground at a distance of 54.5122 meters. The ball hits the ground at a distance of 60.5230 meters. The ball hits the ground at a distance of 72.5501 meters. The ball hits the ground at a distance of 84.5256 meters. The ball hits the ground at a distance of 96.4385 meters. The ball hits the ground at a distance of 102.2623 meters. The ball hits the ground at a distance of 114.0309 meters. The ball hits the ground at a distance of 125.7058 meters. The ball hits the ground at a distance of 137.2762 meters. The ball hits the ground at a distance of 148.7314 meters. The ball hits the ground at a distance of 160.0605 meters. The ball hits the ground at a distance of 171.2529 meters. The ball hits the ground at a distance of 182.2980 meters. The ball hits the ground at a distance of 193.1852 meters. The ball hits the ground at a distance of 203.9040 meters. The ball hits the ground at a distance of 214.4441 meters. The ball hits the ground at a distance of 224.7952 meters. The ball hits the ground at a distance of 234.9470 meters. The ball hits the ground at a distance of 244.8896 meters. The ball hits the ground at a distance of 254.6128 meters. The ball hits the ground at a distance of 264.1069 meters. The ball hits the ground at a distance of 273.3620 meters. The ball hits the ground at a distance of 282.3686 meters. The ball hits the ground at a distance of 285.6243 meters. The ball hits the ground at a distance of 294.1508 meters. The ball hits the ground at a distance of 302.4022 meters. The ball hits the ground at a distance of 310.3695 meters. The ball hits the ground at a distance of 318.0435 meters. The ball hits the ground at a distance of 325.4156 meters. The ball hits the ground at a distance of 332.4770 meters. The ball hits the ground at a distance of 334.0796 meters. The ball hits the ground at a distance of 340.5511 meters. The ball hits the ground at a distance of 346.6884 meters. The ball hits the ground at a distance of 352.4836 meters. The ball hits the ground at a distance of 353.0256 meters. The ball hits the ground at a distance of 358.1759 meters. The ball hits the ground at a distance of 362.9625 meters. The ball hits the ground at a distance of 362.6681 meters. The ball hits the ground at a distance of 366.7728 meters. The ball hits the ground at a distance of 365.9198 meters. The ball hits the ground at a distance of 369.3205 meters. The ball hits the ground at a distance of 372.3255 meters. The ball hits the ground at a distance of 370.5690 meters. The ball hits the ground at a distance of 372.8381 meters. The ball hits the ground at a distance of 370.4845 meters. The ball hits the ground at a distance of 371.9991 meters. The ball hits the ground at a distance of 369.0357 meters. The ball hits the ground at a distance of 365.8026 meters. The ball hits the ground at a distance of 366.1942 meters. The ball hits the ground at a distance of 362.3360 meters. The ball hits the ground at a distance of 358.2030 meters. The ball hits the ground at a distance of 357.4417 meters. The ball hits the ground at a distance of 352.6712 meters. The ball does not hit the ground in 10 seconds The ball hits the ground at a distance of NaN meters. The ball does not hit the ground in 10 seconds The ball hits the ground at a distance of NaN meters. The ball does not hit the ground in 10 seconds The ball hits the ground at a distance of NaN meters. The ball does not hit the ground in 10 seconds The ball hits the ground at a distance of NaN meters. The ball does not hit the ground in 10 seconds The ball hits the ground at a distance of NaN meters. The ball does not hit the ground in 10 seconds The ball hits the ground at a distance of NaN meters.
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

Sign in to comment.

Categories

Find more on 2-D and 3-D Plots in Help Center and File Exchange

Products

Release

R2021a

Asked:

on 10 Sep 2021

Commented:

on 10 Sep 2021

Community Treasure Hunt

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

Start Hunting!