function hitball(v, theta) % simulate trajectory of a baseball hit with speed v (m/s) and angle (degrees) % make a plot and determine if it clears the 17 ft outfield fence at 390 ft x = 0.0; % horizontal position of home plate, meters y = 1.0; % height of ball over strike zone % initial position and speed of ball u0 = [x, y, v*cosd(theta), v*sind(theta)]; % solve for the first ten seconds of flight tspan = [0 10]; % solve the baseball equations numerically [t, u] = ode45(@f_withdrag, tspan, u0); % get the x,y coordinates from the solutions and plot figure(1); clf() x = u(:,1); y = u(:,2); plot(x, y, 'r-') hold on plot([120, 120], [0, 5.2], 'k-', 'linewidth',2) legend('baseball path', 'outfield fence') grid on xlim([-10, 130]) ylim([0, 110]) xlabel('x, meters') ylabel('y, meters') title('baseball trajectory') % find height of ball at x=120 by interpolation y120 = interp1(x, y, 120); if (y120 > 5.2) disp(strcat('hooray! home run! height of ball at outfield fence is ', num2str(y120), ' meters')) elseif (y120 >= 0.0) disp(strcat('boo! fly ball! ball hit outfield fence at height y = ', num2str(y120), ' meters')) elseif (y120 < 0.0) disp('boo! fly ball! ball didn''t reach fence') end end