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
0 个评论
采纳的回答
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
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 个评论
更多回答(0 个)
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!