MATLAB Answers

Please someone tell me what's wrong with my code, it keeps on returning the message, ''Unrecognized variable of v''. The image below the code is the problem?

5 views (last 30 days)
function distance = DTask1_f(v, theta);
h0 = 1.8;
g = 9.8;
v = 60;
t = linspace(0,1,1000);
x=v*cos(theta*pi/180)*t;
y=h+v*sin(theta*pi/180)*t-.5*g*t.^2;
c = find(y<0);
if isempty (c);
disp('The ball does not hit the ground in 10 seconds.');
distance = Nan
else
distance = x(c(1));
end
clear
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= ' num2str(v)])

Accepted Answer

Mrutyunjaya Hiremath
Mrutyunjaya Hiremath on 27 Apr 2020
Hello
There is a problem in the calculation of 'y' in function 'DTask1_f'
Calculated value first value is around 2.4, but it should come around 2.2 according to your graph.
I have done small corrections in you function code.
function distance = DTask1_f(v, theta)
h = 1.5;
g = 9.8;
% v = 60;
t = linspace(0,1,1000);
x = v * cos(theta*pi/180) * t;
y = h + v * sin(theta*pi/180) * t -.5*g*t.^2;
c = find(y<0);
if isempty (c);
disp('The ball does not hit the ground in 10 seconds.');
distance = NaN;
else
disp('The ball hit the ground in 10 seconds.');
distance = x(c(1));
end

  0 Comments

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!