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)])
Best Answer