Velocity of a ball graph, Too many output arguments error

2 次查看(过去 30 天)
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

采纳的回答

Chunru
Chunru 2021-8-28
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

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 2-D and 3-D Plots 的更多信息

产品


版本

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by