If you wrote the ball throwing script for question 2 in Credit Task 1, turn it i
ID: 3757387 • Letter: I
Question
If you wrote the ball throwing script for question 2 in Credit Task 1, turn it into a function. writs a function file named DTask1 fm and maka the function dadaration that takes v and thetainputs and retums the distanc. at which the ball hits the ground distanco -Drask1 tiv. thata). The initial height of the ballis at 15 m We generaly don't want functions to plot things every time they run, so remove the figure command and any plotting commands from the function. To be able to simulate a wide fange of and t heta make the time go until 10 sec d add an r statement that i display the warning The ball does not hit the ground in 10 seconds,' if that turns out to be the case (use isempty). Also, if the bal doesn't hit the ground in 10 seconds, you shoud return N N as the distanan To test your function, wite a seript DTaskim to throw the ball with the same velociry but ditterent angles theta o:o and glt the distarce as a fun thota. The plot should look something ike the fxure below. You will need to ., . w/avei . @ ben 1,..ifeant .ti. ".«cu'llI,,4y.TTvern»-4.. -t. ,. change eo rty u m i i ia test voul program displiwt n walllings and pict the film Distance of ball thrown as a function of release angleExplanation / Answer
Matlab Code :
Function.m
function distance = DTask1_f(v, theta)
h=1.5;
g=9.8;
t=linspace(0,10,10001);
x=v*cos(theta*pi/180)*t;
y=h+v*sin(theta*pi/180)*t-.5*g*t.^2;
neg=find(y<0);
if isempty(neg)
fprintf('The ball does not hit the ground in 10 seconds. ');
distance=NaN;
else
distance=x(neg(1));
end
Script.m
clear
v=4;
theta=0:1:60;
distance=zeros(1,61);
for i = 1:61
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)])