Clear Filters
Clear Filters

Velocity of a ball graph, Too many output arguments error

2 views (last 30 days)
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

Accepted Answer

Chunru
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
The ball does not hit the ground in 10 seconds The ball does not hit the ground in 10 seconds The ball does not hit the ground in 10 seconds The ball does not hit the ground in 10 seconds The ball does not hit the ground in 10 seconds
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

More Answers (0)

Categories

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

Products


Release

R2021a

Community Treasure Hunt

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

Start Hunting!