필터 지우기
필터 지우기

Velocity of a ball graph, Too many output arguments error

조회 수: 2 (최근 30일)
Harrison Miller
Harrison Miller 2021년 8월 28일
답변: Chunru 2021년 8월 28일
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