Velocity of a ball graph, Too many output arguments error
3 views (last 30 days)
Show older comments
I am trying to graph the relationship between the angle that the ball is launched at and its distance but i keep getting the error ' too many output arguments, below is attached the code and the task
function DTask1_f(v, theta)
h0 = 1.8; %Inital height
A = 9.8; %Acceeleration
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 = 0: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= 4 m/s')
end
0 Comments
Accepted Answer
Chunru
on 28 Aug 2021
Put the function at the end of the script or in a different file. Add the out put "d=.." in your function header.
v = 60;
theta = 0: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= 4 m/s')
function d=DTask1_f(v, theta)
h0 = 1.8; %Inital height
A = 9.8; %Acceeleration
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
0 Comments
More Answers (0)
See Also
Categories
Find more on Graph and Network Algorithms 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!