# 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?

1 view (last 30 days)
Abdulswamad Salim on 27 Apr 2020
Commented: Abdulswamad Salim on 27 Apr 2020
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
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)])
Abdulswamad Salim on 27 Apr 2020
Thank you so much brother Hiremath, seems it was a simple problem. You are appreciated.

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

### Categories

Find more on Morphological Operations in Help Center and File Exchange

### Community Treasure Hunt

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

Start Hunting!