1.Turn it into a function declaration that takes v and theta as inputs and retur
ID: 3885476 • Letter: 1
Question
1.Turn it into a function declaration that takes v and theta as inputs and return distance, distance = DTask1_f(v, theta). The inital height is 1.5m
2. Make time go for 10 seconds and add an if statement that will display the warning 'The ball does not hit the ground in 10 seconds' If that turns out to be the case (use isempty).
3. Also, if the ball doesn't hit the ground in 10 seconds, you should return NaN as the distance.
4. To test your function, write a script DTask1.m to throw the ball with the same velocity, v=4 m/s but different angles of theta = 0:60 and plot the distance as a function of angle theta. The plot should look something like Figure 1 below.
5. You will need to run DTask1_f within a loop in order to calcculate the ditance for various thetas.
6. Change velocity to v = 60m/s, test if your program dispalys the warnings and plot the figure.
Figure 1:
Script 1:
h0 = 1.5;
a = 9.8;
v0 = 4;
theta = 45;
t = linspace(0,1,1000);
x = v0*cos(theta*(pi/180))*t;
y = h0 + v0*sin(theta*(pi/180))*t-0.5*a.*t.*t;
ind = find(y > 0 & y < 0.01)
htsgrndy = y(ind)
htsgrndx = x(ind)
fprintf ('The ball hits the ground at a disance of %f meters',htsgrndx)
figure(1)
f=0;
plot(x,y,[-10 10],[1 1]*f,'k--')
xlim([0 3])
ylim([-1 2.5])
xlabel('Distance(m)')
ylabel('Ball height(m)')
title('Ball trajectory')
Explanation / Answer
function distance = DTask_f(vo, theta)
ho = 1.5;
t = 10.0;
g = 9.8;
x = vo * cos(theta*(pi/180)) * t;
y = ho + vo * sin(theta * (pi/180)) * t - (0.5 * g * t * t);
if (y > 0)
fprintf("The ball does not hit the ground in 10 seconds, for vo = %.3f and theta = %.2f ", vo, theta);
distance = NaN;
return
end
distance = x;
end
distanceVector = [];
anglesVector = [];
for theta = 0:60
distance = DTask_f(4.0, theta);
distanceVector = [distanceVector distance];
anglesVector = [anglesVector theta];
end
plot(anglesVector, distanceVector);
xlabel('Distance(m)')
ylabel('Ball height(m)')
title('Ball trajectory (vo=4m/s and ho=1.5m)')
distanceVector = [];
anglesVector = [];
for theta = 0:60
distance = DTask_f(60.0, theta);
distanceVector = [distanceVector distance];
anglesVector = [anglesVector theta];
end
plot(anglesVector, distanceVector);
xlabel('Distance(m)')
ylabel('Ball height(m)')
title('Ball trajectory (vo=60m/s and ho=1.5m)')
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.